python - 如何设置不等式来检查 "consumption"(基本上是冲突)

标签 python pygame inequality

我正在尝试使用 pygame 在 python 中制作一个蛇游戏...它会四处移动并吃食物。但每当“蛇”经过它时,我似乎无法让 cookies 移动。

我尝试设置一个不等式来比较食物的位置和蛇的位置,但它不起作用......

import pygame, sys, random, time
screenWidth = 500
gameDisplay = pygame.display.set_mode((screenWidth,screenWidth))
#this begins the game
pygame.init()
#This sets a caption
pygame.display.set_caption("Picnic")

#Variables:
snakeFace = pygame.image.load('morrison.png')
x= 50
y= 50
snakewidth = 30
snakeheight = 30
food1width = 40
food1height = 17
food2width = 25
food2height = 26
food3width = 27
food3height = 23
vel = 5
run = True
lastKey = None
food1 = pygame.image.load("thinMint.png")
food2 = pygame.image.load("cookie.png")
food3 = pygame.image.load("triscuit.png")
randFoodX1 = 0
randFoodY1 = 0
randFoodX2 = 0
randFoodY2 = 0
#randFoodX1 = random.randrange(1, 50, 1) * 10
#randFoodY1 = random.randrange(1, 50, 1) * 10

def generateFoodPosition():
    randFoodX1 = random.randrange(1, 40, 1) * 10
    randFoodY1 = random.randrange(1, 40, 1) * 10
    randFoodX2 = random.randrange(1, 40, 1) * 10
    randFoodY2 = random.randrange(1, 40, 1) * 10
    randFoodX3 = random.randrange(1, 40, 1) * 10
    randFoodY3 = random.randrange(1, 40, 1) * 10
    if randFoodX1 == randFoodX2 or randFoodY1 == randFoodY2 or 
        randFoodY2==randFoodY3 or randFoodY1== randFoodY3 or 
        randFoodX2 == randFoodX3 or randFoodX3 == randFoodX1:
        generateFoodPosition()
    else:
        return [randFoodX1, randFoodY1, randFoodX2, 
                randFoodY2, randFoodX3, randFoodY3]
foodPositions = generateFoodPosition()

def checkForConsumption():
    if ((x + snakewidth) < (foodPositions[0] + food2width + 5 
  and (x + snakewidth) > (foodPositions[0] + food2width - 5)))  or 
((y + snakeheight) < (foodPositions[1] + food2height + 5 and (x + 
snakeheight) > (foodPositions[1] + food2height - 5))):
        foodPositions[0] = random.randrange(1, 40, 1) * 10
    foodPositions[1] = random.randrange(1, 40, 1) * 10
    if ((x + snakewidth) < (foodPositions[2] + food2width + 5 and (x 
+ snakewidth) > (foodPositions[2] + food2width - 5)))  or ((y + 
 snakeheight) < (foodPositions[3] + food2height + 5 and (x + 
 snakeheight) > (foodPositions[3] + food2height - 5))):
    foodPositions[2] = random.randrange(1, 40, 1) * 10
    foodPositions[3] = random.randrange(1, 40, 1) * 10
if ((x + snakewidth) < (foodPositions[4] + food2width + 5 and (x + 
 snakewidth) > (foodPositions[4] + food2width - 5)))  or ((y + 
snakeheight) < (foodPositions[5] + food2height + 5 and (x + 
 snakeheight) > (foodPositions[5] + food2height - 5))):
    foodPositions[4] = random.randrange(1, 40, 1) * 10
    foodPositions[5] = random.randrange(1, 40, 1) * 10

while run:
    pygame.time.delay(10) #1/2 milisecond delay
     #this controls the "X" button
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            lastKey = event.key

#this controls the movement of the snake
if lastKey == pygame.K_LEFT and x > vel:
    x-=vel
if lastKey == pygame.K_RIGHT and x< screenWidth - snakewidth - vel:
    x+=vel
if lastKey == pygame.K_DOWN and y < screenWidth - snakeheight - vel:
    y+= vel
if lastKey == pygame.K_UP and y > vel:
    y-=vel

#if x == (foodPositions[0] or foodPositions[2] or foodPositions[4]) or y== (foodPositions[1] or foodPositions[3] or foodPositions[5]):
checkForConsumption()
gameDisplay.fill((0,0,0))
gameDisplay.blit(snakeFace,(x, y))
gameDisplay.blit(food1, (foodPositions[0], foodPositions[1]))
gameDisplay.blit(food2, (foodPositions[2], foodPositions[3]))
gameDisplay.blit(food3, (foodPositions[4], foodPositions[5]))
pygame.display.update()
print(x)
print(y)


pygame.display.set_mode((screenWidth,screenWidth))
pygame.quit()

再次,我预计当蛇移动到 cookies 上时它会移动到不同的位置

最佳答案

正如评论中提到的,Pygame 有一个方法 detecting collision between rectangles 。幸运的是,blit 方法 returns a Rect object 。这意味着,当您将蛇脸和食物图像绘制到屏幕上时,您可以保留这些返回值并使用它们来检测蛇和食物之间的碰撞。

这是一个简短的片段来说明我正在谈论的内容。

您可以将主循环中的绘图代码更改为:

gameDisplay.fill((0,0,0))
snakeRect = gameDisplay.blit(snakeFace,(x, y))
food1Rect = gameDisplay.blit(food1, (foodPositions[0], foodPositions[1]))
food2Rect = gameDisplay.blit(food2, (foodPositions[2], foodPositions[3]))
food3Rect = gameDisplay.blit(food3, (foodPositions[4], foodPositions[5]))
checkForConsumption()  # Note: It is important that you check for consumption after the food and snake have been drawn, as drawing updates their rectangles
pygame.display.update()

然后,您的 checkForConclusion 方法变得更加简单:

def checkForConsumption():
    if snakeRect.coliderect(food1Rect):
        # Recalculate the new position of the food just as you did
        foodPositions[0] = random.randrange(1, 40, 1) * 10
        foodPositions[1] = random.randrange(1, 40, 1) * 10

        # Additionally, redraw the food at its new location
        gameDisplay.blit(food1, (foodPositions[0], foodPositions[1]))

    # Check each of the other pieces of food just as you did the first

此方法允许您利用 Pygame 中内置的方法,这样您就不必迷失在碰撞检测方法的实现细节中,同时仍然使用蛇头和食物的自定义图像。虽然编写自己的碰撞检测方法是可能的,而且不太难,但这种方法更加干净,并且使用 Pygame 提供的工具。

关于python - 如何设置不等式来检查 "consumption"(基本上是冲突),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54545550/

相关文章:

python - GttS 等待单词完整 Python

python和pygame射击

python - pandas 中类别缺失值的插补

python - Pandas:加速 groupby

python - pygame.错误: mixer not initialized How To Fix?

python - 求解最小值的不等式

python - Python 中的不等式和括号

big-o - 查找函数的顺序

python - num_epochs 和 steps 有什么区别?

php - FreeBSD PHP 执行权限被拒绝