Nov-16-2021, 07:44 PM
Hello guys, my game has certain components (functions) not working. I had someone help me modify some parts and now some things dont work. Ive tried my best but it just wont run. please help me out.
The parts i'm having problems with are:
- Timer: doesn't start from 3 (starts from 1)
- Ball: doesn't move once timer times out
- Opponent ai: doesn't start (possibly linked to the ball_start function
please help guys. Code Below:
The parts i'm having problems with are:
- Timer: doesn't start from 3 (starts from 1)
- Ball: doesn't move once timer times out
- Opponent ai: doesn't start (possibly linked to the ball_start function
please help guys. Code Below:
import pygame, sys, random from pygame.locals import * # dispaly.setup screen_width, screen_height = 600, 400 FPS = 60 pygame.mixer.pre_init(44100,-16,1, 1024) pygame.init() screen = pygame.display.set_mode((screen_width, screen_height), 0, 32) pygame.display.set_caption('MUSTAPHA MOHAMMEDS PING PONG GAME') # game.loop clock = pygame.time.Clock() # here, i'll make the main menu with funcs def main(): def ball_animation(ball_speed_x, ball_speed_y, player_score, opponent_score, score_time): ball.x += ball_speed_x ball.y += ball_speed_y if ball.top <= 0 or ball.bottom >= screen_height: pygame.mixer.Sound.play(pong_sound) ball_speed_y *= -1 # Player Score if ball.left <= 0: pygame.mixer.Sound.play(score_sound) score_time = pygame.time.get_ticks() player_score += 1 # Opponent Score if ball.right >= screen_width: pygame.mixer.Sound.play(score_sound) score_time = pygame.time.get_ticks() opponent_score += 1 if ball.colliderect(player) and ball_speed_x > 0: pygame.mixer.Sound.play(pong_sound) if abs(ball.right - player.left) < 10: ball_speed_x *= -1 elif abs(ball.bottom - player.top) < 10 and ball_speed_y > 0: ball_speed_y *= -1 elif abs(ball.top - player.bottom) < 10 and ball_speed_y < 0: ball_speed_y *= -1 if ball.colliderect(opponent) and ball_speed_x < 0: pygame.mixer.Sound.play(pong_sound) if abs(ball.left - opponent.right) < 10: ball_speed_x *= -1 elif abs(ball.bottom - opponent.top) < 10 and ball_speed_y > 0: ball_speed_y *= -1 elif abs(ball.top - opponent.bottom) < 10 and ball_speed_y < 0: ball_speed_y *= -1 def player_animation(player, player_speed): player.y += player_speed if player.top <= 0: player.top = 0 if player.bottom >= screen_height: player.bottom = screen_height def opponent_ai(opponent, opponent_speed): if opponent.top < ball.y: opponent.y += opponent_speed if opponent.bottom > ball.y: opponent.y -= opponent_speed if opponent.top <= 0: opponent.top = 0 if opponent.bottom >= screen_height: opponent.bottom = screen_height def ball_start(score_time, ball_speed_x, ball_speed_y, ball_moving): ball.center = (screen_width/2, screen_height/2) current_time = pygame.time.get_ticks() # Timer if current_time - score_time < 700: number_three = basic_font.render("3",False,light_grey) screen.blit(number_three,(screen_width/2 - 10, screen_height/2 + 20)) if 700 < current_time - score_time < 1400: number_two = basic_font.render("2",False,light_grey) screen.blit(number_two,(screen_width/2 - 10, screen_height/2 + 20)) if 1400 < current_time - score_time < 2100: number_one = basic_font.render("1",False,light_grey) screen.blit(number_one,(screen_width/2 - 10, screen_height/2 + 20)) if current_time - score_time < 2100: ball_speed_y, ball_speed_x = 0,0 else: ball_speed_x = 7 * random.choice((1,-1)) ball_speed_y = 7 * random.choice((1,-1)) score_time = None # Colors black = (0,0,0) white = (255,255,255) green = (0,128,0) red = (255,0,0) light_grey = (200,200,200) teal = (0,128,128) bg_color = pygame.Color('#008080') # Game Rectangles ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) player = pygame.Rect(screen_width - 3 -10, screen_height / 2 - 70, 10 +100,140) opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) # Game Variables ball_speed_x = 7 * random.choice((1,-1)) ball_speed_y = 7 * random.choice((1,-1)) player_speed = 0 opponent_speed = 7 ball_moving = False score_time = True # Score Text player_score = 0 opponent_score = 0 basic_font = pygame.font.Font('freesansbold.ttf', 32) # sound pong_sound = pygame.mixer.Sound("pong.ogg") score_sound = pygame.mixer.Sound("score.ogg") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player_speed -= 8 if event.key == pygame.K_DOWN: player_speed += 8 if event.type == pygame.KEYUP: if event.key == pygame.K_UP: player_speed += 8 if event.key == pygame.K_DOWN: player_speed -= 8 #Game Logic ball_animation(ball_speed_x, ball_speed_y, player_score, opponent_score, score_time) player_animation(player, player_speed) opponent_ai(opponent, opponent_speed) # Visuals screen.fill(bg_color) pygame.draw.rect(screen, black, player) pygame.draw.rect(screen, red, opponent) pygame.draw.ellipse(screen, white, ball) pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) if score_time: ball_start(score_time, ball_speed_x, ball_speed_y, ball_moving) player_text = basic_font.render(f'{player_score}',False,black) screen.blit(player_text,(260,20)) opponent_text = basic_font.render(f'{opponent_score}',False,white) screen.blit(opponent_text,(320,20)) pygame.display.flip() clock.tick(60) def main_menu(): run = True selection = 1 basic_font = pygame.font.Font('freesansbold.ttf', 32) start = basic_font.render("START", True, (255, 2, 255)) credits = basic_font.render("CREDITS", True, (255, 2, 255)) quit_txt = basic_font.render("QUIT", True, (255, 2, 255)) selector = pygame.Rect(50, 50, 50, 50) # option rects sr = pygame.Rect(screen_width//2 - (start.get_width()//2), 100, start.get_width(), start.get_height()) cr = pygame.Rect(screen_width//2 - (credits.get_width()//2), 150, credits.get_width(), credits.get_height()) qr = pygame.Rect(screen_width//2 - (quit_txt.get_width()//2), 200, quit_txt.get_width(), quit_txt.get_height()) def draw(): screen.fill((146, 244, 200)) pygame.draw.rect(screen, (0, 0, 0), selector) screen.blit(start, (sr.x, sr.y)) screen.blit(credits, (cr.x, cr.y)) screen.blit(quit_txt, (qr.x, qr.y)) pygame.display.update() while run: clock.tick(FPS) draw() if selection == 1: selector.x = sr.x - 5 selector.y = sr.y - 5 selector.width = sr.width + 10 selector.height = sr.height + 10 elif selection == 2: selector.x = cr.x - 5 selector.y = cr.y - 5 selector.width = cr.width + 10 selector.height = cr.height + 10 elif selection == 3: selector.x = qr.x - 5 selector.y = qr.y - 5 selector.width = qr.width + 10 selector.height = qr.height + 10 for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == K_DOWN: selection += 1 if event.key == K_UP: selection -= 1 if event.key == K_RETURN: if selection == 1: main() elif selection == 2: pass elif selection == 3: quit() if selection > 3: selection = 1 elif selection < 1: selection = 3 if __name__ == "__main__": main_menu()