Я пытаюсь сделать игру на змею в python с pygame ... той, которая движется и ест еду.Но я не могу заставить печенье двигаться, когда над ним движется «змея».
Я пытался установить неравенство для сравнения положения пищи и положения змеи, но оно не работает...
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()
Опять же, я ожидал, что когда змея переместится над печеньем, она переместится в другое место