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

时间:2019-03-05 17:12:43

标签: python pygame

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)

1 个答案:

答案 0 :(得分:0)

这都是秩序的问题。

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.draw_snake()之前,蛇已绘制self.movement()

您必须按以下顺序进行:

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

    # [...]

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