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:
Escape pressed:
Whole code:
The 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)
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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 ]) |
1 2 3 4 5 |
keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE] and round_start = = 0 : run = False intro = True game_intro() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
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.quit |
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)