Jan-18-2020, 05:15 AM
(This post was last modified: Jan-18-2020, 05:15 AM by ElevenDecember.)
I"m currently working on a small project for a computer class, in which we need to make a basic game. I'm using Thonny as well as the PyGame module.
The problem I'm facing is that the program starts fine, however when I click the "Play" button on the start menu (not in editor), instead of entering into the main game loop, the program closes instead.
Feedback would be appreciated, as even the teacher had no solution.
Thank you!
Code:
#-------------------------
#Snake Game Final Project
#Steven Bailey
#January 8th 2020
#-------------------------
The problem I'm facing is that the program starts fine, however when I click the "Play" button on the start menu (not in editor), instead of entering into the main game loop, the program closes instead.
Feedback would be appreciated, as even the teacher had no solution.
Thank you!
Code:
#-------------------------
#Snake Game Final Project
#Steven Bailey
#January 8th 2020
#-------------------------
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 |
#------------------------- #Snake Game Final Project #Steven Bailey #January 8th 2020 #------------------------- import pygame import time import random #game initilization pygame.init() pygame.font.init() clock = pygame.time.Clock() def text_objects(text, font,): textSurface = font.render(text, True , white) return textSurface, textSurface.get_rect() # Screen size size = width, height = 500 , 500 win = pygame.display.set_mode((size)) pygame.display.set_caption( "Snake by Steven Bailey" ) #how to make the colours I want to use red = pygame.Color( 255 , 0 , 0 ) bright_green = pygame.Color( 0 , 255 , 0 ) white = pygame.Color( 255 , 255 , 255 ) black = pygame.Color( 0 , 0 , 0 ) green = pygame.Color( 0 , 200 , 0 ) brown = pygame.Color( 165 , 42 , 42 ) #how to make a start menu def game_intro(): intro = True while intro: for event in pygame.event.get(): if event. type = = pygame.QUIT: pygame.quit() quit() win.fill(black) largeText = pygame.font.Font( 'freesansbold.ttf' , 115 ) textSurf, textRect = text_objects( "Snake" , largeText) win.blit(textSurf, textRect) button( "Start" , 150 , 250 , 100 , 50 ,green,bright_green, "play" ) pygame.display.update() clock.tick( 15 ) #how to make a start button def button(msg, x,y,w,h,ic,ac,action = None ): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() print (click) if x + w > mouse[ 0 ] > x and x and y + h > mouse[ 1 ] > y: pygame.draw.rect(win, ac, (x,y,w,h)) if click[ 0 ] = = 1 and action ! = None : if action = = "play" : game_loop() if x + w > mouse[ 0 ] > x and y + h > mouse[ 1 ] > y: pygame.draw.rect(win, ac, (x,y,w,h)) else : pygame.draw.rect(win, ic, (x,y,w,h)) smallText = pygame.font.Font( "freesansbold.ttf" , 20 ) textSurf, textRect = text_objects(msg, smallText) textRect.center = ( (x + (w / 2 )), y + (h / 2 ) ) win.blit(textSurf, textRect) #game over font and gameover function def game_over(): gameoverText = pygame.font.Font( 'freesansbold.ttf' , 75 ) textSurf, textRect = text_objects( "GAME OVER" , gameoverText,) win.blit(textSurf, textRect) time.sleep( 4 ) pygame.quit() exit() #how to show the score def show_score(choice = 1 ): pygame.font.init() SFont = pygame.font.SysFont( 'freesansbold.ttf' , 32 ) Ssurf = SFont.render( "Score : {0}" . format (score), True , black) Srect = Ssurf.get_rect() if choice = = 1 : Srect.midtop = ( 80 , 10 ) else : Srect.midtop = ( 320 , 100 ) win.blit(Ssurf, Srect) # FPS controller, controls the frames per second of the game fpsController = pygame.time.Clock() # Game settings delta = 10 snakePos = [ 100 , 50 ] snakeBody = [[ 100 , 50 ], [ 90 , 50 ], [ 80 , 50 ]] foodPos = [ 400 , 50 ] foodSpawn = True change_to = '' direction = 'RIGHT' score = 0 def game_loop(): win.fill(black) while True : pygame.init() for event in pygame.event.get(): if event. type = = pygame.QUIT: pygame.quit() elif event. type = = pygame.KEYDOWN: changeto = '' if event.key = = pygame.K_RIGHT or event.key = = pygame.K_d: change_to = 'RIGHT' if event.key = = pygame.K_LEFT or event.key = = pygame.K_a: change_to = 'LEFT' if event.key = = pygame.K_UP or event.key = = pygame.K_w: change_to = 'UP' if event.key = = pygame.K_DOWN or event.key = = pygame.K_s: change_to = 'DOWN' if event.key = = pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT)) # Validate direction change_to = '' direction = 'RIGHT' if change_to = = 'RIGHT' and direction ! = 'LEFT' : direction = changeto if change_to = = 'LEFT' and direction ! = 'RIGHT' : direction = changeto if change_to = = 'UP' and direction ! = 'DOWN' : direction = changeto if change_to = = 'DOWN' and direction ! = 'UP' : direction = change_to # Update snake position delta = 10 snakePos = [ 100 , 50 ] if direction = = 'RIGHT' : snakePos[ 0 ] + = delta if direction = = 'LEFT' : snakePos[ 0 ] - = delta if direction = = 'DOWN' : snakePos[ 1 ] + = delta if direction = = 'UP' : snakePos[ 1 ] - = delta # Snake body mechanism snakeBody = [[ 100 , 50 ], [ 90 , 50 ], [ 80 , 50 ]] snakePos = [ 100 , 50 ] foodSpawn = True foodPos = [ 400 , 50 ] snakeBody.insert( 0 , list (snakePos)) if snakePos = = foodPos: foodSpawn = False score + = 1 else : snakeBody.pop() if foodSpawn = = False : foodPos = [random.randrange( 1 , width / / 10 ) * delta, random.randrange( 1 , height / / 10 ) * delta] foodSpawn = True for pos in snakeBody: pygame.draw.rect(win, green, pygame.Rect(pos[ 0 ], pos[ 1 ], delta, delta)) pygame.draw.rect(win, brown, pygame.Rect(foodPos[ 0 ], foodPos[ 1 ], delta, delta)) show_score # Bounds if snakePos[ 0 ] > = width or snakePos[ 0 ] < 0 : game_over() if snakePos[ 1 ] > = height or snakePos[ 1 ] < 0 : game_over() # Self hit for block in snakeBody[ 1 :]: if snakePos = = block: game_over() fpsController.tick( 20 ) game_intro() game_loop() |