在pygame中不使用精灵的情况下两个矩形之间的碰撞检测

问题描述 投票:0回答:2

我试图让我的

Player()
和我的
Coin()
发生碰撞。我浏览了很多问题和互联网论坛,但似乎找不到不使用
Sprite
的答案。

我画了两个矩形(玩家和硬币),我只想知道如何查看它们是否发生碰撞。这是我的代码:

import pygame, sys, random

pygame.init()

display_width = 640
display_height = 480

display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Tutorial")

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
yellow = (255,255,0)

clock = pygame.time.Clock()

running = True

class Player:

    def __init__(self,x,y,hspd,vspd,color,screen):
        self.x = x
        self.y = y
        self.hspd = hspd
        self.vspd = vspd
        self.color = color
        self.screen = screen

    def draw(self):
        pygame.draw.rect(self.screen,self.color,(self.x,self.y,32,32))

    def move(self):
        self.x += self.hspd
        self.y += self.vspd

def collisionDetect(obj1,obj2):
    pygame.sprite.collide_rect(obj1,obj2)

enemies = []

class Enemy:

    def __init__(self,x,y,hspd,vspd,color,screen):
        self.x = x
        self.y = y
        self.hspd = hspd
        self.vspd = vspd
        self.color = color
        self.screen = screen

    def draw(self):
        pygame.draw.rect(display,red,(self.x,self.y,32,32))

    def move(self):
        self.x += self.hspd
        self.y += self.vspd

    def checkBounce(self):
        if self.x > 640-32 or self.x < 0:
            self.hspd = self.hspd * -1
        if self.y > 480-32 or self.y < 0:
            self.vspd = self.vspd * -1


class Coin:

    def __init__(self,x,y,color,screen):
        self.x = random.randint(0,640-32)
        self.y = random.randint(0,480-32)
        self.color = color
        self.screen = screen

    def draw(self):
        pygame.draw.rect(self.screen,yellow,(self.x,self.y,32,32))

    def move(self):
        if collisionDetect(self,player):
            self.x = random.randint(0,640-32)
            self.y = random.randint(0,480-32)


coin = Coin(255,255,yellow,display)   

player = Player(0,0,0,0,black,display)



def current_speed():
    currently_pressed = pygame.key.get_pressed()
    hdir = currently_pressed[pygame.K_RIGHT] - currently_pressed[pygame.K_LEFT]
    vdir = currently_pressed[pygame.K_DOWN] - currently_pressed[pygame.K_UP]
    return hdir * 4, vdir * 4

def updateEnemies():
    for enemy in enemies:
        enemy.move()
        enemy.draw()
        enemy.checkBounce()

CREATEENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(CREATEENEMY, 1000)


while running:

    clock.tick(60)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        if event.type == CREATEENEMY:
            enemy = Enemy(0,0,random.randint(1,4),random.randint(1,4),red,display)
            enemies.append(enemy)

        player.hspd, player.vspd = current_speed()

    display.fill(white)

    player.move()

    player.draw()
    coin.draw()
    updateEnemies()

    pygame.display.flip()

    print len(enemies)
pygame.quit()
sys.exit()

提前致谢!

python pygame collision-detection
2个回答
0
投票

我也浏览了多个网站,在碰撞检测方面我找不到任何与Python相关的内容,我在Javascript中找到了碰撞检测if语句。 将其翻译成 Python 后就是:

if rect_1 x < rect_2 x + rect_1 width and rect_1 x + rect_2 width > rect_2 x and rect_1 y < rect_2 y + rect_1 height and rect_1 height + rect_1 y > rect_2 y:

之所以有效,是因为它可以感知矩形的边缘是否接触。


0
投票

您可以在 pygame 中使用内置函数 colliderect。

© www.soinside.com 2019 - 2024. All rights reserved.