Python Pygame:两个图像的碰撞

Python Pygame:两个图像的碰撞,python,python-2.7,pygame,Python,Python 2.7,Pygame,我正在做我的学校项目,为此我设计了一个2D游戏 我有3张图片,一张是播放器,另外两张是实例(咖啡和电脑)。我想做的是,当播放器图像与两个实例中的一个发生冲突时,我希望程序打印一些东西 我不确定是否可能发生图像冲突。但我知道直接碰撞是可能的。然而,在几次失败的尝试之后,我无法使我的图像变为矩形。谁来帮帮我。以下是我的源代码: import pygame import os black=(0,0,0) white=(255,255,255) blue=(0,0,255) class Playe

我正在做我的学校项目,为此我设计了一个2D游戏

我有3张图片,一张是播放器,另外两张是实例(咖啡和电脑)。我想做的是,当播放器图像与两个实例中的一个发生冲突时,我希望程序打印一些东西

我不确定是否可能发生图像冲突。但我知道直接碰撞是可能的。然而,在几次失败的尝试之后,我无法使我的图像变为矩形。谁来帮帮我。以下是我的源代码:

import pygame
import os

black=(0,0,0)
white=(255,255,255)
blue=(0,0,255)


class Player(object):  
    def __init__(self):
        self.image = pygame.image.load("player1.png")
        self.image2 = pygame.transform.flip(self.image, True, False)
        self.coffee=pygame.image.load("coffee.png")
        self.computer=pygame.image.load("computer.png")
        self.flipped = False
        self.x = 0
        self.y = 0


    def handle_keys(self):
        """ Movement keys """
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_DOWN]: 
            self.y += dist 
        elif key[pygame.K_UP]: 
            self.y -= dist 
        if key[pygame.K_RIGHT]: 
            self.x += dist
            self.flipped = False
        elif key[pygame.K_LEFT]:
            self.x -= dist
            self.flipped = True

    def draw(self, surface):
        if self.flipped:
            image = self.image2
        else:
            im = self.image            
        for x in range(0, 810, 10):
            pygame.draw.rect(screen, black, [x, 0, 10, 10])
            pygame.draw.rect(screen, black, [x, 610, 10, 10])

        for x in range(0, 610, 10):
            pygame.draw.rect(screen, black, [0, x, 10, 10])
            pygame.draw.rect(screen, black, [810, x, 10, 10])

        surface.blit(self.coffee, (725,500))
        surface.blit(self.computer,(15,500))
        surface.blit(im, (self.x, self.y))



pygame.init()



screen = pygame.display.set_mode((800, 600))#creates the screen

player = Player()
clock = pygame.time.Clock()

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()      # quit the screen
            running = False

    player.handle_keys()       # movement keys
    screen.fill((255,255,255)) # fill the screen with white




    player.draw(screen)        # draw the player to the screen
    pygame.display.update()    # update the screen

    clock.tick(60)             # Limits Frames Per Second to 60 or less
用于保持图像大小和位置

Image(或者更确切地说是
pygame.Surface()
)具有函数
get_rect()
,该函数返回带有图像大小(和位置)的
pygame.rect()

现在您可以设置起始位置,即。
(0,0)

Rect
使用左上角作为(x,y))

用它来改变位置

self.rect.x += dist
并绘制图像

surface.blit(self.image, self.rect)
然后你可以测试碰撞

if self.rect.colliderect(self.rect_coffe):


顺便说一句:现在
class Player
看起来几乎像:)

好的,我合并了更改,我把播放器做成了一个矩形,还有咖啡。我将碰撞测试放在while循环之前,并输入if self.rect.colliderect(self.rect_coffe):启动程序后打印“no”。我做错了什么?在
钥匙手柄
之后和
绘图
之前。您是否在
rect_coffe
中设置了咖啡位置?作为默认的rect使用(x=0,y=0)。你总是可以
打印(self.rect,self.rect\u coffe)
来查看位置。所以当我点咖啡时,我把它放在了surface.blit(self.coffe,self.coffe\u rect,[725500]),但是我得到了一个无效的参数样式。我应该更正什么?仅
surface.blit(self.coffee,self.coffee\u rect)
但在开始时,您必须设置
self.coffee\u rect.topleft=[725500]
surface.blit(self.image, self.rect)
if self.rect.colliderect(self.rect_coffe):