如何修复pygame中分数的自动更新



我想修复以下代码的自动更新:

import pygame
import time
import random
pygame.init()
pygame.mixer.music.load('song.mp3')
pygame.mixer.music.play(0)
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
display_width = 800
display_height  = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('KALM Creation')

#Score
score=0
#img=pygame.image.load("1.png")
#intro=pygame.image.load("intro.png")
foodimg=pygame.image.load("food.png")
#Our Icon For The Game
icon=pygame.image.load('icon1.jpg')
pygame.display.set_icon(icon)

clock = pygame.time.Clock()
AppleThickness=30
block_size = 10
FPS = 30
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
#The score function
def scoredisplay(scoredef=0):
    text=smallfont.render("Score :%s" %(scoredef) ,True ,black)
    gameDisplay.blit(text,[0,0])
#The random function of apple
def randAppleGen():
    randAppleX = round(random.randrange(0, display_width-AppleThickness))
    randAppleY = round(random.randrange(0, display_height-AppleThickness))
    return randAppleX,randAppleY
randAppleX,randAppleY=randAppleGen()
#Starting Of the game
def game_intro():
    intro = True
    while intro:    
      for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key ==pygame.K_c:
                intro = False
            if event.key ==pygame.K_q:
                pygame.quit()
                quit()
        gameDisplay.fill(white)
        message_to_screen("Welcome To  Eat it Game",
                          green,
                          -200,
                          size="medium")
        message_to_screen("This Game is created by KALM Creations",
                          black,
                          -30,
                          size="small")
        message_to_screen("Eat the food to gain a score!",
                          black,
                          10,
                          size="small")
        message_to_screen("Avoid the rock , if you hit them you lose!",
                          black,
                          50,
                          size="small")
        message_to_screen("Press 'C' to play the game or 'Q' to quit.",
                          black,
                          150,
                          size="small")
        pygame.display.update()
        clock.tick(15)
#Snake aka the object which is moving (in green colour)
def snake(lead_x,lead_y,block_size):
    pygame.draw.rect(gameDisplay, green, [lead_x,lead_y,block_size,block_size])
#Text Size
def text_objects(text,color, size):
    if size=="small":
        textSurface=smallfont.render(text, True ,color)
    elif size=="medium":
        textSurface=medfont.render(text, True ,color)
    elif size=="large":
        textSurface=largefont.render(text, True ,color)

    return textSurface,textSurface.get_rect()
#Message to screen
def message_to_screen(msg,color,y_displace=0,size="small"):
    textSurf,textRect=text_objects(msg,color,size)
    textRect.center = (display_width / 2),(display_height / 2)+y_displace
    gameDisplay.blit(textSurf,textRect)
#   The game run up
def gameLoop():
    score=0
    gameExit = False
    gameOver = False
    lead_x = display_width/2
    lead_y = display_height/2
    lead_x_change = 0
    lead_y_change = 0
    randAppleX,randAppleY=randAppleGen()

    while not gameExit:
        while gameOver == True:
            gameDisplay.fill(white)
            message_to_screen("Game over",
                              red,
                              y_displace=-50,
                              size="large")
            message_to_screen("Press C to play again or Q to quit",
                              black,
                              y_displace=50,
                              size="medium")
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    lead_x_change = -block_size
                    lead_y_change = 0
                elif event.key == pygame.K_RIGHT:
                    lead_x_change = block_size
                    lead_y_change = 0
                elif event.key == pygame.K_UP:
                    lead_y_change = -block_size
                    lead_x_change = 0
                elif event.key == pygame.K_DOWN:
                    lead_y_change = block_size
                    lead_x_change = 0
        if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
            gameOver = True

        gameDisplay.blit(foodimg,(randAppleX,randAppleY))
        lead_x += lead_x_change
        lead_y += lead_y_change
        gameDisplay.fill(white)
        gameDisplay.blit(foodimg,(randAppleX,randAppleY))
        snake(lead_x,lead_y,block_size)
        scoredisplay()
        pygame.display.update()

        if lead_x > randAppleX and lead_x < randAppleY + AppleThickness or lead_x + block_size>randAppleX and lead_x + block_size< randAppleX + AppleThickness:
             if lead_y > randAppleY and lead_y< randAppleY + AppleThickness or lead_y + block_size>randAppleY and lead_y + block_size< randAppleY + AppleThickness:
                 randAppleX,randAppleY=randAppleGen()
                 score+=1
                 scoredisplay(score)
             elif lead_y +block_size >randAppleY and lead_y +block_size < randAppleY:
                 randAppleX,randAppleY=randAppleGen()
                 score+=1
                 scoredisplay(score)
            '''
         Now when i run this game , the score keeps on updating.I want it to do like when the snake collides with 'foodimg',
         it should add +1 to the score.Also sometimes even if the snake does not touch the food , the food is moved to a random
         position.How do i fix this bug?
         '''
        scoredisplay(score)
        pygame.display.update()
        clock.tick(FPS)
    pygame.quit()
    quit()
game_intro()
gameLoop()
#Episode 36

在这种情况下,分数总是处于上升状态,我想解决的问题是,当蛇没有与食物碰撞时,食物有时会移动到一个随机的位置。我该如何修复这两个错误?这是代码中使用的2个图像
icon1
食品

您调用scoredisplay()时有一点没有输入,而在其他地方调用它是不必要的。以下是修改后的代码:

import pygame
import time
import random
pygame.init()
pygame.mixer.music.load('song.mp3')
pygame.mixer.music.play(0)
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
display_width = 800
display_height  = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('KALM Creation')

#Score
score=0
#img=pygame.image.load("1.png")
#intro=pygame.image.load("intro.png")
foodimg=pygame.image.load("food.png")
#Our Icon For The Game
icon=pygame.image.load('icon1.jpg')
pygame.display.set_icon(icon)

clock = pygame.time.Clock()
AppleThickness=30
block_size = 10
FPS = 30
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
#The score function
def scoredisplay(scoredef=0):
    text=smallfont.render("Score :%s" %(scoredef) ,True ,black)
    gameDisplay.blit(text,[0,0])
#The random function of apple
def randAppleGen():
    randAppleX = round(random.randrange(0, display_width-AppleThickness))
    randAppleY = round(random.randrange(0, display_height-AppleThickness))
    return randAppleX,randAppleY
randAppleX,randAppleY=randAppleGen()
#Starting Of the game
def game_intro():
    intro = True
    while intro:    
      for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key ==pygame.K_c:
                intro = False
            if event.key ==pygame.K_q:
                pygame.quit()
                quit()
        gameDisplay.fill(white)
        message_to_screen("Welcome To  Eat it Game",
                          green,
                          -200,
                          size="medium")
        message_to_screen("This Game is created by KALM Creations",
                          black,
                          -30,
                          size="small")
        message_to_screen("Eat the food to gain a score!",
                          black,
                          10,
                          size="small")
        message_to_screen("Avoid the rock , if you hit them you lose!",
                          black,
                          50,
                          size="small")
        message_to_screen("Press 'C' to play the game or 'Q' to quit.",
                          black,
                          150,
                          size="small")
        pygame.display.update()
        clock.tick(15)
#Snake aka the object which is moving (in green colour)
def snake(lead_x,lead_y,block_size):
    pygame.draw.rect(gameDisplay, green, [lead_x,lead_y,block_size,block_size])
#Text Size
def text_objects(text,color, size):
    if size=="small":
        textSurface=smallfont.render(text, True ,color)
    elif size=="medium":
        textSurface=medfont.render(text, True ,color)
    elif size=="large":
        textSurface=largefont.render(text, True ,color)

    return textSurface,textSurface.get_rect()
#Message to screen
def message_to_screen(msg,color,y_displace=0,size="small"):
    textSurf,textRect=text_objects(msg,color,size)
    textRect.center = (display_width / 2),(display_height / 2)+y_displace
    gameDisplay.blit(textSurf,textRect)
#   The game run up
def gameLoop():
    score=0
    gameExit = False
    gameOver = False
    lead_x = display_width/2
    lead_y = display_height/2
    lead_x_change = 0
    lead_y_change = 0
    randAppleX,randAppleY=randAppleGen()

    while not gameExit:
        while gameOver == True:
            gameDisplay.fill(white)
            message_to_screen("Game over",
                              red,
                              y_displace=-50,
                              size="large")
            message_to_screen("Press C to play again or Q to quit",
                              black,
                              y_displace=50,
                              size="medium")
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    lead_x_change = -block_size
                    lead_y_change = 0
                elif event.key == pygame.K_RIGHT:
                    lead_x_change = block_size
                    lead_y_change = 0
                elif event.key == pygame.K_UP:
                    lead_y_change = -block_size
                    lead_x_change = 0
                elif event.key == pygame.K_DOWN:
                    lead_y_change = block_size
                    lead_x_change = 0
        if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
            gameOver = True

        gameDisplay.blit(foodimg,(randAppleX,randAppleY))
        lead_x += lead_x_change
        lead_y += lead_y_change
        gameDisplay.fill(white)
        gameDisplay.blit(foodimg,(randAppleX,randAppleY))
        snake(lead_x,lead_y,block_size)
        pygame.display.update()

        if lead_x > randAppleX and lead_x < randAppleY + AppleThickness or lead_x + block_size>randAppleX and lead_x + block_size< randAppleX + AppleThickness:
             if lead_y > randAppleY and lead_y< randAppleY + AppleThickness or lead_y + block_size>randAppleY and lead_y + block_size< randAppleY + AppleThickness:
                 randAppleX,randAppleY=randAppleGen()
                 score+=1
             elif lead_y +block_size >randAppleY and lead_y +block_size < randAppleY:
                 randAppleX,randAppleY=randAppleGen()
                 score+=1
        scoredisplay(score)
        pygame.display.update()
        clock.tick(FPS)
    pygame.quit()
    quit()
game_intro()
gameLoop()
#Episode 36

如果你想在没有收集的情况下移动目标,请设置一个计时器,对每个刻度进行计数。如果计数器超过某个数字,则移动目标,如果玩家及时收集目标,则重置计数器。

希望这有帮助:)

最新更新