GUVI
Back

my snake game runs for some seconds and then it crashes, can you find the error to it

Created 2 years ago
34 Views
0 Comments
Debashish
@Debashish
Debashish
@DebashishProfile is locked. Login
import pygame import sys import time import random pygame.init() white = (255, 255, 255) black = (100, 0, 0) red = (255, 0, 0) # window size window_width = 900 window_height = 700 gameDisplay = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("Slither") font = pygame.font.SysFont(None, 25, bold=True) # First Function def myquit(): pygame.quit() sys.exit(0) # clock use in order to keep track of events based on the timing clock = pygame.time.Clock() FPS = 5 blockSize = 20 # size o snake head noPixel = 0 # second function to describe the chunks(food) eaten by the snake def snake(blockSize, snakelist): for size in snakelist: pygame.draw.rect(gameDisplay, black, [size[0]+5, size[1], blockSize, blockSize], 2) # 3rd function to indicate the message to display on screen when the snake hits any boundary or waNTS to continue the g def message_to_screen(msg, color): screen_text = font.render(msg, True, color) gameDisplay.blit(screen_text, [window_width/2, window_height/2]) # 4th unction to use key Q to exit and C to continue def gameLoop(): gameExit = False gameOver = False lead_x = window_width/2 lead_y = window_height/2 change_pixels_of_x = 0 change_pixels_of_y = 0 snakelist = [] # first the snake size is 1 heAd snakeLength = 1 # random positions of apple appearing on screen after the snake eats apple randomAppleX = round(random.randrange(0, window_width-blockSize)/10.0)*10.0 randomAppleY = round(random.randrange(0, window_height-blockSize)/10.0)*10.0 while not gameExit: while gameOver == True: gameDisplay.fill(white) message_to_screen("Game Over,Press C to Play Again or Q to quit", red) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: gameOver = False gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: gameExit = True gameOver = False if event.key == pygame.K_c: gameLoop() # loop 1 to check the snake movement according to the keys pressed and to adjust the snake according to the directions for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: myquit() leftArrow = event.key == pygame.K_LEFT rightArrow = event.key == pygame.K_RIGHT upArrow = event.key == pygame.K_UP downArrow = event.key == pygame.K_DOWN if leftArrow: change_pixels_of_x = -blockSize change_pixels_of_y = noPixel elif rightArrow: change_pixels_of_x = blockSize change_pixels_of_y = noPixel elif upArrow: change_pixels_of_y = -blockSize change_pixels_of_x = noPixel elif downArrow: change_pixels_of_y = blockSize change_pixels_of_x = noPixel # loop 2 to check the snake resides within the boundary if lead_x >= window_width or lead_x or lead_y >= window_height or lead_y < 0: gameOver = True lead_x += change_pixels_of_x lead_y += change_pixels_of_y gameDisplay.fill(white) AppleThickness = 20 # logic 3 apple creation print([int(randomAppleX), int(randomAppleY), AppleThickness, AppleThickness]) pygame.draw.rect(gameDisplay, red, [randomAppleX, randomAppleY, AppleThickness, AppleThickness]) allspriteslist = [] allspriteslist.append(lead_x) allspriteslist.append(lead_y) snakelist.append(allspriteslist) if len(snakelist) > snakeLength: del snakelist[0] for eachSegment in snakelist[-1]: if eachSegment == allspriteslist: gameOver = True snake(blockSize, snakelist) pygame.display.update() if lead_x>= randomAppleX and lead_x<= randomAppleX + AppleThickness: if lead_y>= randomAppleY and lead_y<= randomAppleY + AppleThickness: randomAppleX = round(random.randrange(0, window_width-blockSize)/10.0)*10.0 randomAppleY = round(random.randrange(0, window_height-blockSize)/10.0)*10.0 snakeLength += 1 clock.tick(FPS) pygame.quit() quit() gameLoop()
Comments
Please login to comment.
 
Powered by habitate.io Habitate