Hi there, i'm new to python and just started Pygame this week, learning via working on a Pong project.
Long story short, i have the pong game working and i have a menu before that, all working as i want them.
I added a function that when pressing escape and the game is idle, the main loop will stop and the game menu will pop up again
Startin menu:
Hope someone cold help (and improvements on the code would be happily accepted as well :))
Edit:
worth mentioning no error is occuring, the screen is just frozen and keys won't work (thats on the IDLE, regular file just exists)
Long story short, i have the pong game working and i have a menu before that, all working as i want them.
I added a function that when pressing escape and the game is idle, the main loop will stop and the game menu will pop up again
Startin menu:
def game_intro(): intro = True bgm = pygame.mixer.music.load("bg.mp3") pygame.mixer.music.play(-1) while intro: for event in pygame.event.get(): print(pygame.event.get()) if event.type == pygame.QUIT: pygame.quit() quit if event.type == pygame.MOUSEBUTTONUP: pos = pygame.mouse.get_pos() if (pos[0] >= 200) and (pos[0] <= 283) and (pos[1] >= 387) and (pos[1] <= 438): intro = False run = True game = True elif (pos[0] >=200) and (pos[0] <= 300) and (pos[1] >= 460) and (pos[1] <= 511): pygame.quit() quit pygame.draw.rect(win, grey, (width/3, 387, 85, 50), 5) pygame.draw.rect(win, grey, (width/3, 461, 100, 50), 5) message_to_screen(font1,"Play",green,[width/3, 375]) message_to_screen(font1,"Quit",red,[width/3, 450]) pygame.display.update() clock.tick(13) win.fill(black) line_rain() message_to_screen(font2,"Welcome to Pong 1.0!",orange,[width /4, height/10]) message_to_screen(font3,"1VS1 Only",orange,[width /4, height/5]) message_to_screen(font3,"Player 1: W -> UP , S -> Down",orange,[width /4, height/3]) message_to_screen(font3,"Player 2: Key Up -> Up , Key Down -> Down",orange,[width /4, height - height/2])Escape pressed:
keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE] and round_start == 0: run = False intro = True game_intro()Whole code:
import pygame import random import time import sys pygame.init() ######### Sound ############ bgm = pygame.mixer.music.load("bg.mp3") round_sound = 0 def sound_mix(x): if x == "hit": sfx8 = pygame.mixer.Sound("phaserUp" + str(random.randint(1, 7)) + ".ogg") pygame.mixer.Sound.play(sfx8) elif x == "score": sfx2 = pygame.mixer.Sound("score.ogg") pygame.mixer.Sound.play(sfx2) return; elif x == "start": sfx1 = pygame.mixer.Sound("fight" + str(random.randint(1, 4)) + ".ogg") pygame.mixer.Sound.play(sfx1) return; elif x == "end": sfx3 = pygame.mixer.Sound("end.ogg") pygame.mixer.Sound.play(sfx3) return; ######### Screen Vars ######### width = 600 height = 600 ######## Colors ######## white = 255,255,255 red = 255,0,0 blue = 0,0,255 green = 0,255,0 yellow = 255,255,0 black = 0,0,0 grey = 128,128,128 orange = 255,231,60 ######### Ping's Vars ######### H = 130 W = 25 Vel = 20 player1x = W player1y = random.randint(H, height - H) player2x = width - (W*2) player2y = random.randint(H, height - H) player1 = "Player 1" player2 = "Player 2" random_player = random.randint(1,2) ######### Pong's Vars ######### R = 6 T = 3 ballx = int(width/2) bally = int(height/2) ballv = 24 ballvy = random.randint(1,20 ) balldx = ballv balldy = ballvy round_start = 0 ballcolor = white ######## Window Start ########## win = pygame.display.set_mode([width, height]) pygame.display.set_caption("Pong") clock = pygame.time.Clock() game = True keys = pygame.key.get_pressed() ###### Graphics ######### backg = pygame.image.load("bg.jpg").convert() bgx = 0 ######### Score ########### player1score = 0 player2score = 0 maxscore = 5 ######### Ball-Placement ########## def random_acc(x,y): if x == "x": if y == 1: random_user = W + eval("player" + str(y) + x) balldx = ballv else: random_user = eval("player" + str(y) + x) - W balldx = -ballv else: random_user = int(H / 2) + eval("player" + str(y) + x) return random_user; ### Fonts font1 = pygame.font.SysFont('Comic Sans MS', 45) font2 = pygame.font.SysFont('Comic Sans MS', 35) font3 = pygame.font.SysFont('Comic Sans MS', 20) ### Text function def message_to_screen(font,msg,color,where): screen_text = font.render(msg, True, color) win.blit(screen_text, where) pygame.display.update() ### Rain function def line_rain(): gravity = 1 rain_size = 2 rain_number = 50 rain_height = height rain_color = white rain_flake = [] for q in range(rain_number): x = random.randrange(0,width) y = random.randrange(0,height) rain_flake.append([x,y]) for i in rain_flake: i[1] += gravity rain_length = [i[0] + 1,i[1] + 1] pygame.draw.circle(win, rain_color, i, rain_size) if i[1] > rain_height: i[1] = random.randrange(-50,-5) i[0] = random.randrange(width) def game_intro(): intro = True bgm = pygame.mixer.music.load("bg.mp3") pygame.mixer.music.play(-1) while intro: for event in pygame.event.get(): print(pygame.event.get()) if event.type == pygame.QUIT: pygame.quit() quit if event.type == pygame.MOUSEBUTTONUP: pos = pygame.mouse.get_pos() if (pos[0] >= 200) and (pos[0] <= 283) and (pos[1] >= 387) and (pos[1] <= 438): intro = False run = True game = True elif (pos[0] >=200) and (pos[0] <= 300) and (pos[1] >= 460) and (pos[1] <= 511): pygame.quit() quit pygame.draw.rect(win, grey, (width/3, 387, 85, 50), 5) pygame.draw.rect(win, grey, (width/3, 461, 100, 50), 5) message_to_screen(font1,"Play",green,[width/3, 375]) message_to_screen(font1,"Quit",red,[width/3, 450]) pygame.display.update() clock.tick(13) win.fill(black) line_rain() message_to_screen(font2,"Welcome to Pong 1.0!",orange,[width /4, height/10]) message_to_screen(font3,"1VS1 Only",orange,[width /4, height/5]) message_to_screen(font3,"Player 1: W -> UP , S -> Down",orange,[width /4, height/3]) message_to_screen(font3,"Player 2: Key Up -> Up , Key Down -> Down",orange,[width /4, height - height/2]) game_intro() run = True class game_run(): while run: bgm = pygame.mixer.music.load("bg.mp3") pygame.mixer.music.play(-1) if (game == True) and (round_start == 0): message_to_screen(font1,"Score " + str(player1score) + " - " + str(player2score),orange,[width / 4, height / 10]) pygame.time.delay(70) clock.tick(200) for event in pygame.event.get(): print(pygame.event.get()) if event.type == pygame.QUIT: run = False ### Pause menu (which is start menu) ### keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE] and round_start == 0: run = False intro = True game_intro() ### Player 1&2 Moving sequence ### if (game == True): if keys[pygame.K_s] and player1y < (height - H): player1y += qdis(player1y, "down") round_start = 1 if keys[pygame.K_w] and player1y > 1: player1y -= qdis(player1y, "up") round_start = 1 if keys[pygame.K_DOWN] and player2y < (height - H): player2y += qdis(player2y, "down") round_start = 1 if keys[pygame.K_UP] and player2y > 1: player2y -= qdis(player2y, "up") round_start = 1 ### Ball Mechanics ### # Movment if round_start == 0: ballx = random_acc("x",random_player) bally = random_acc("y",random_player) ballcolor = white if (round_sound == 0): round_sound = 1 sound_mix("start") if round_start == 1: ballx = ballx + int(balldx) bally = bally + int(balldy) #Collision player1max = player1x player1min = player1x + W + R player1m = player1y player1b = player1y + H player2max = player2x player2min = player2x - W - R player2m = player2y player2b = player2y + H center = H / 2 ballvy = random.randint(1,20 ) if (ballx >= player1max) and (ballx <= player1min) and (player1m <= bally) and (player1b >= bally) and round_start == 1: balldx = ballv ballcolor = red sound_mix("hit") if (bally <= player1m + center) and (bally >= player1m): balldy = - ballvy else: balldy = ballvy elif (ballx >= player2min) and (ballx <= player2max) and (player2m <= bally) and (player2b >= bally) and round_start == 1: balldx = - ballv ballcolor = green sound_mix("hit") if (bally <= player2m + center) and (bally >= player2m): balldy = - ballvy else: balldy = ballvy # Borders if ballx <= R: balldx = ballv player2score = player2score + 1 random_player = 2 sound_mix("score") round_start = 0 round_sound = 0 elif ballx >= width - R: balldx = - ballv player1score = player1score + 1 random_player = 1 sound_mix("score") round_start = 0 round_sound = 0 if bally <= R: balldy = ballv elif bally >= height - R: balldy = - ballv # Round End # Background function rel_bgx = bgx % backg.get_rect().width win.blit(backg, (bgx,0)) if rel_bgx < width: win.blit(backg, (rel_bgx, 0)) bgx -= 1 # Draw functions pygame.draw.rect(win, red, (player1x, player1y, W, H)) pygame.draw.rect(win, green, (player2x, player2y, W, H)) pygame.draw.circle(win, ballcolor, (ballx,bally), R, T) pygame.display.update() ## Boundaries function def qdis(x, y): movment = height - H if y == "down": if x < movment: return Vel; else: sum = int(movment) - int(x) return sum; elif y == "up": if x > Vel: return Vel; else: return int(x); pygame.quitThe problem is not with the main menu, as it is the returning to the game (pressing the button) after visiting the main menu the second time.
Hope someone cold help (and improvements on the code would be happily accepted as well :))
Edit:
worth mentioning no error is occuring, the screen is just frozen and keys won't work (thats on the IDLE, regular file just exists)