【问题标题】:Collision detection between two rectangles without using a sprite in pygame在pygame中不使用精灵的两个矩形之间的碰撞检测
【发布时间】:2016-02-20 17:20:08
【问题描述】:

我试图在我的Player() 和我的Coin() 之间发生冲突。我浏览了 SO 问题和互联网论坛,但似乎找不到不使用 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()

提前致谢!

【问题讨论】:

  • @Suever 这是在 C++ 中,这是在 python 中。
  • 这是一个通用问题,不应该对所有库和编程语言都有答案。接受的答案可以适应以任何语言实现的任何矩形。
  • 基本逻辑是检查一个矩形的任何角是否在另一个矩形内。我同意另一个答案应该很容易翻译

标签: python pygame collision-detection


【解决方案1】:

我也浏览了多个网站,在碰撞检测方面我找不到任何与 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:

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

【讨论】:

  • 欢迎来到 SO!感谢您回答这个问题......不过,我可以建议一些改进吗?首先,您应该将有关您的内容移到您的个人资料中,因为它与答案无关。其次,您能否使您的代码在语法上正确(例如,作为示例函数)并解释其工作原理? Legodog5678 的回答很好地说明了目标。
猜你喜欢
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
相关资源
最近更新 更多