gpt4 book ai didi

python - pygame.Rect.colliderect() 函数未执行

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:56 24 4
gpt4 key购买 nike

food.rectsnake.rect 发生碰撞时,我尝试向我的蛇克隆中的玩家添加一段片段。但是函数内的代码将不会执行。

def add_segment(self,food):
if self.rect.colliderect(food.rect):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
self.body.append(snake_segment)
print('this code works!')

如果您需要的话,这是我的其余代码。我已经为这个问题苦苦挣扎了好几天,所以任何帮助将不胜感激!

import pygame,sys,random
from pygame.locals import *

pygame.init()

WINDOW_HEIGHT = 500
WINDOW_WIDTH = 500

RECTANGLE_HEIGHT = 20
RECTANGLE_WIDTH = 20

GREEN = (0,255,0)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)

clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Snake")

class Snake(object):
def __init__(self, color, width, height):
self.body = []
self.image = pygame.Surface((width,height))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.speed = 20
self.direction = 'right'
self.body = []
self.living = True
self.move = (0,0)

def movement(self):
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())

def blit(self):
SCREEN.blit(self.image,self.rect)

def update(self,food):
self.add_segment(food)
self.draw_snake()
self.movement()
print(self.rect.colliderect(food.rect))
print(self.body)

def add_segment(self,food):
if self.rect.colliderect(food.rect):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
self.body.append(snake_segment)
print('this code works!')

def draw_snake(self):
if len(self.body) == 0:
self.blit()
elif len(self.body) >= 1:
for snake_segment in self.body:
snake_segment.blit()

class Food(object):
def __init__(self,color,width,height):
self.image = pygame.Surface((width,height))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(20,500,20)
self.rect.y = random.randrange(20,500,20)
self.state = 'uneaten'

def check_eaten(self,snake):
if snake.rect.colliderect(self.rect):
self.state = 'eaten'

def blit(self):
SCREEN.blit(self.image,self.rect)

def update_state(self,snake):
self.check_eaten(snake)
if self.state == 'uneaten':
self.blit()
elif self.state == 'eaten':
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)

if self.rect.x == snake.rect.x or self.rect.x == snake.rect.y:
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)

self.blit()
self.state = 'uneaten'

def update(self,snake):
self.update_state(snake)
print(self.state)

def event_loop(snake,food):
while True:
SCREEN.fill(BLACK)

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
snake.move = (snake.speed,0)
snake.direction = 'right'
elif event.key == K_LEFT:
snake.move = (-snake.speed,0)
snake.direction = 'left'
elif event.key == K_UP:
snake.move = (0,-snake.speed)
snake.direction = 'up'
elif event.key == K_DOWN:
snake.move = (0,snake.speed)
snake.direction = 'down'

food.update(snake)
snake.update(food)

pygame.display.update()
clock.tick(5)

SNAKE = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
FOOD = Food(RED,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
event_loop(SNAKE,FOOD)

最佳答案

这都是顺序问题。

food.update(snake) 测试食物是否与摇晃发生碰撞并将食物放置在新位置。
snake.update(food) 测试是蛇与食物碰撞并向蛇追加一段。

所以如果food.update(snake)snake.update(food)之前执行,那么蛇永远不会吃食物,因为食物被移走了之前:

food.update(snake)
snake.update(food)

snake.update(food)
food.update(snake)

蛇永远不会处于绘制的位置,因为蛇是在移动 self.movement() 之前绘制的 self.draw_snake()

您必须按以下顺序进行操作:

  1. 移动蛇
  2. 测试蛇是否在食物上
  3. 画蛇
class Snake(object):

# [...]

def update(self,food):
self.movement()
self.add_segment(food)
self.draw_snake()

关于python - pygame.Rect.colliderect() 函数未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55008171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com