May-05-2020, 09:47 AM
I'm a beginner and I'm coding "Snake game". This is what I copied paste from the Internet and self-coded. This system did run but the scores didn't count and I have no idea why is it. I hope someone will show me the mistakes and help to fix this
import time,random,sys,pygame pygame.init() gameSurface = pygame.display.set_mode((735,475)) m = 20 Imgbody = pygame.transform.scale(pygame.image.load('Body.jpg'),(m,m)) Imghead = pygame.transform.scale(pygame.image.load('Head.png'),(m,m)) Imgfood = pygame.transform.scale(pygame.image.load('Covide.jpg'),(m,m)) red = pygame.Color(255,0,0) Blue = pygame.Color(65,105,255) Black = pygame.Color(0,0,0) White = pygame.Color(255,255,255) Grey = pygame.Color(128,128,128) snakepos=[100,60] snakebody =[[100,60],[80,60],[60,60]] foodx = random.randrange(1,71) foody = random.randrange(1,45) if foodx % 2 !=0: foodx +=1 if foody % 2 !=0: foody +=1 foodpos = [foodx * 10, foody * 10] foodflat = True direction ='RIGHT' changeto = direction score = 0 def show_score(choice = 1): sfont = pygame.font.SysFont('conlaso',40) ssurf = sfont.render('Score: {0}'.format(score),True,Black) srect = ssurf.get_rect() if choice == 1: srect.midtop = (70,20) else: srect.midtop = (360,230) gameSurface.blit(ssurf,srect) def game_over(): gfont = pygame.font.SysFont('conlaso',40) gsurf = gfont.render('Game Over!',True,red) grect = gsurf.get_rect() grect.midtop = (360, 150 ) gameSurface.blit(gsurf,grect) show_score(0) pygame.display.flip() time.sleep(5) pygame.quit() sys.exit() def game_loop(): m = 20 snakepos = [100,60] snakebody =[[100,60], [80,60],[60,60]] foodx = random.randrange(1,71) foody = random.randrange(1,45) if foodx % 2 !=0: foodx +=1 if foody % 2 !=0: foody +=1 foodpos = [foodx * 10, foody * 10] score = 0 direction = "RIGHT" changeto =direction running = True gameOver= False foodflat = True Imgbody = pygame.transform.scale(pygame.image.load('Body.jpg'),(m,m)) Imghead = pygame.transform.scale(pygame.image.load('Head.png'),(m,m)) Imgfood = pygame.transform.scale(pygame.image.load('Covide.jpg'),(m,m)) while running: if gameOver==True: message_to_screen("Game over",red,-50,size="large") message_to_screen("Press C to play again or Q to quit",Black,50,size="medium") pygame.display.update() while gameOver == True: for event in pygame.event.get(): if event.type==pygame.QUIT: gameOver=False running=False if event.type==pygame.KEYDOWN: if event.key==pygame.K_q: running=False gameOver=False if event.key==pygame.K_c: game_loop() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT or event.key == ord('d') : changeto = "RIGHT" if changeto == 'RIGHT' and not direction == 'LEFT': direction = 'RIGHT' elif event.key == pygame.K_LEFT or event.key == ord('a'): changeto = "LEFT" if changeto == 'LEFT' and not direction == 'RIGHT': direction = 'LEFT' elif event.key == pygame.K_DOWN or event.key == ord('s'): changeto = "DOWN" if changeto == 'DOWN' and not direction == 'UP': direction = 'DOWN' elif event.key == pygame.K_UP or event.key == ord('w'): changeto = "UP" if changeto == 'UP' and not direction == 'DOWN': direction = 'UP' elif event.key == pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT)) if direction == "RIGHT": snakepos[0] += m elif direction == "UP": snakepos[1] -= m elif direction == "DOWN": snakepos[1] += m elif direction == "LEFT": snakepos[0] -= m snakebody.insert(0, list(snakepos)) if snakepos[0] == foodpos[0] and foodpos[1] == snakepos[1]: score += 1 foodflat = False else: snakebody.pop() pygame.display.update() if foodflat == False: foodx = random.randrange(1,71) foody = random.randrange(1,45) if foodx % 2 !=0: foodx +=1 if foody % 2 !=0: foody +=1 foodpos = [foodx * 10, foody * 10] foodflat = True for pos in snakebody: gameSurface.blit(Imgbody,pygame.Rect(pos[0],pos[1],m,m)) gameSurface.blit(Imghead, pygame.Rect(snakebody[0][0],snakebody[0][1],m,m)) gameSurface.blit(Imgfood, pygame.Rect(foodpos[0],foodpos[1],m,m)) pygame.draw.rect(gameSurface,Grey,(10,10,715,455),2) if snakepos[0] > 710 or snakepos[0] < 0: game_over() if snakepos[1] > 450 or snakepos[1] < 0: game_over() for block in snakebody[1:]: if snakepos[0] == block[0] and snakepos[1] == block[1]: game_over() pygame.display.flip() gameSurface.fill(White) pygame.time.delay(200) show_score() game_loop()