当我的蛇碰到苹果时,苹果不会消失

时间:2018-09-02 06:40:48

标签: python pygame game-development

这是我的第一篇文章!我正在尝试在Pygame中制作蛇的克隆,但是当我的蛇碰到苹果时,它不会传送到另一个地方。这是我的代码:

import pygame          
import time
import random

pygame.init()          

wh = (255,255,255)     #white
bk = (0,0,0)           #black
ylw = (255,255,0)      #yellow 
prpl = (127,0,127)     #purple

snakeXChange = 0
snakeYChange = 0
dWidth = 800
dHeight = 600 
clock = pygame.time.Clock()
fps = 25
blockSize = 15
appleX = round(random.randrange(0, dWidth - blockSize)/ 10.0)* 10.0
appleY = round(random.randrange(0, dHeight - blockSize) / 10.0)* 10.0
snakeX = dWidth / 2           
snakeY = dHeight / 2          
font = pygame.font.SysFont(None, 25)
def message(msg, color):
    screenText = font.render(msg, True, color)
    gameDisplay.blit(screenText, [dWidth / 2, dHeight / 2])

gameDisplay = pygame.display.set_mode((dWidth,dHeight))     
pygame.display.set_caption('Snek')                   #shoutout to Hopson


gameExit = False                                    
gameOver = False


while not gameExit:                                  
    for event in pygame.event.get():                 
        if event.type == pygame.QUIT:                
            gameExit = True                         
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                snakeXChange = -blockSize
                snakeYChange = 0
            if event.key == pygame.K_RIGHT:
                snakeXChange = blockSize
                snakeYChange = 0
            if event.key == pygame.K_UP:
                snakeYChange = -blockSize
                snakeXChange = 0
            if event.key == pygame.K_DOWN:
                snakeYChange = blockSize
                snakeXChange = 0


    snakeX += snakeXChange

    snakeY += snakeYChange

    gameDisplay.fill(prpl)                          

    pygame.draw.rect(gameDisplay, wh, [snakeX,snakeY,blockSize,blockSize])  #snake

    pygame.draw.rect(gameDisplay, ylw, [appleX,appleY,blockSize,blockSize]) #apple

    if snakeX == appleX and snakeY == appleY:
        appleX = round(random.randrange(0, dWidth - blockSize)/ 10.0)* 10.0
        appleY = round(random.randrange(0, dHeight - blockSize) / 10.0)* 10.0

    clock.tick(fps)

    pygame.display.update()  

message("GAME OVER NIBBA", ylw)
pygame.display.update()                              #updates the display
time.sleep(5)
pygame.quit()                                        
quit()    

原始来源:Customizing p2 metadata

我正在遵循NewBoston的教程作为参考。

欢呼

SalmonSan

1 个答案:

答案 0 :(得分:3)

您要将appleXappleY的坐标四舍五入到最接近的10的倍数,但是以15 px的步长移动,这意味着可能发生蛇无法触摸的情况苹果。例如,appleX可能是410,而蛇的起点是400,因此它将永远无法到达苹果。

只需将所有位置四舍五入至最接近的15的倍数(blockSize和速度):

appleX = round(random.randrange(0, dWidth - blockSize) / 15.0) * 15.0
appleY = round(random.randrange(0, dHeight - blockSize) / 15.0) * 15.0
snakeX = round(dWidth / 2. / 15.) * 15
snakeY = round(dHeight / 2. / 15.) * 15

您也可以将15作为step的参数random.randrange传递给随机数15。

appleX = random.randrange(0, dWidth-blockSize, 15)

也可以使用pygame.Rects进行碰撞检测并存储坐标。