JAVA碰撞检测无效_碰撞检测不适用于Pygame中的精灵

我最近尝试通过将我的基本 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)

b5c1b3da-1da9-4b48-b0ce-9cc0248b2fdb.png

971b9e2d-5644-4880-938b-d98b5e134494.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值