【问题标题】:Collision Detection Not Working for Sprites in Pygame碰撞检测不适用于 Pygame 中的精灵
【发布时间】:2017-11-27 21:05:17
【问题描述】:

我最近尝试通过将基本的rect 和图像更改为更高级的Class 系统来升级我正在开发的游戏的代码。但是,我的碰撞检测似乎有问题,当我告诉我的精灵hg 在与floor(或任何输入的box)碰撞时停止下降时,它没有检测到,但脚本正在运行,所以我知道语法没有错。我究竟做错了什么? 以下是我认为足以帮助您的代码以及您需要的两张图片(如果您需要更多信息,请告诉我):

######## basic setup
import pygame, sys, time, random, threading, tkinter, ctypes
from threading import Timer
from pygame.locals import *
from tkinter import *
pygame.init()
WINDOWHEIGHT = 720
WINDOWWIDTH = 1280
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Hitman Grandma | vB1.0 (prealpha)')
white = (255,255,255)
red = (255,0,0)
black = (0,0,0)
green = (0,255,0)
blue = (0,0,255)
cyan = (0,255,255)
lightgrey = (198,198,198)
windowSurface.fill(lightgrey)
pygame.display.update()
mainClock = pygame.time.Clock()
########## variables
level = 0
touching = False
global x_speed
x_speed = 0
y_speed = 0
leftallowed = True
rightallowed = True
hgturnright = True
hgjumpallowed = True
########### the grandma d'awesome murder sorts
hgimage = pygame.image.load('hgfinal.png')
hgimage.convert_alpha()
class HG(object):
    def __init__(self,x,y,image):
        self.image = image
        self.rect = self.image.get_rect()
        self.x = x
        self.y = y       
    def draw(self):
        windowSurface.blit(self.image,(self.x,self.y))
    def move(self):
        self.x += x_speed
        self.y += y_speed
    def topcollide(self,box):
        if not self.rect.colliderect(box.rect):
            global y_speed
            if y_speed < 20:
                y_speed += 1
            elif y_speed == 20:
                y_speed = 20
            print('shhoooo')
        elif self.rect.colliderect(box.rect):
            y_speed = 0
            print('flop')
hg = HG(0,0,hgimage)
########### land and boundary
lands = pygame.image.load('hgland1.png')
floorland = pygame.transform.scale(lands,(1280,50))
sideedge = pygame.Rect(0,0,1,720),pygame.Rect(1279,0,1,720)
topedge = pygame.Rect(0,0,1280,1)
class Floor(object):
    def __init__(self,x,y,image):
        self.image = image
        self.x = x
        self.y = y
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
    def draw(self):
        windowSurface.blit(self.image,(self.x,self.y))
class Ground(object):
    def __init__(self,x,y,image):
        self.image = image
        self.x = x
        self.y = y
        self.rect = self.image.get_rect()
    def draw(self):
        windowSurface.blit(self.image,(self.x,self.y))
floor = Floor(0,670,floorland)
########### WHILE
while True:
########### background
    windowSurface.fill(lightgrey)
########### hg movement
    for event in pygame.event.get():
        if event.type == KEYDOWN:
                if event.key == K_LEFT and hg.x > 0 and leftallowed:
                    x_speed = -4
                    hgturnright = False
                if event.key == K_RIGHT and (hg.x + 36) < WINDOWWIDTH and rightallowed:
                    x_speed = 4
                    hgturnright = True
                if event.key == K_UP and hgjumpallowed:
                    y_speed = -17
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                x_speed = 0
            if event.key == K_LEFT:
                x_speed = 0
        if event.type == KEYDOWN:
########### ctrl+q
            if event.key == K_q and pygame.key.get_mods() & pygame.KMOD_CTRL:
                pygame.quit()
                sys.exit()
                exit
########### [x]
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                exit
########### drawing and .move()
    floor.draw()
    hg.draw()
    hg.move()
    hg.topcollide(floor)
########### technicals
    pygame.display.update()
    mainClock.tick(40)

【问题讨论】:

  • 顺便说一句:你可以改为if not colliderect(): elif colliderect(): if not colliderect(): else: 因为你不必检查相同的colliderect() 两次。
  • 使用print()检查变量中的值以及执行了哪部分代码——这有助于发现问题。
  • 是的,如果碰撞检测出现问题,打印所涉及对象的rects 通常会有所帮助。
  • 另外,杀手奶奶也很搞笑。 XD
  • 谢谢@skrx,我为她感到骄傲。

标签: python pygame sprite


【解决方案1】:

您必须更新HG 实例(rect.topleftrect.center)的矩形位置,因为它用于碰撞检测:

class HG(object):
    def __init__(self,x,y,image):
        self.image = image
        self.rect = self.image.get_rect(topleft=(x, y))
        self.x = x
        self.y = y

    def move(self):
        self.x += x_speed
        self.y += y_speed
        self.rect.topleft = (self.x, self.y)

【讨论】:

  • 感谢您的帮助!你以前回答过我的问题,你永远是对的!我不敢相信我没有考虑不更新我的rect 职位。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2017-09-14
  • 2015-04-13
  • 1970-01-01
  • 2019-11-05
相关资源
最近更新 更多