我在做一个地牢游戏。我开始实施碰撞。这是一个简单的矩形碰撞检测。但我不知道怎么做。
我想让obstacle
对象与播放器发生碰撞
我有所有对象的X和Y位置,以及对象的X边界和Y边界位置。在
import pygame
from pygame.locals import *
screen = pygame.display.set_mode((500, 500))
ball = pygame.image.load("ball.png") #43x43 png
white = (255, 255, 255)
black = (0, 0, 0)
px = 0
py = 0
ticker = 0
class obstacle(object):
def __init__(self, x, y, xbord, ybord):
self.x = x
self.y = y
self.xbord = xbord
self.ybord = ybord
self.surf = pygame.Surface((self.xbord, self.ybord))
self.surf.fill(black)
o = []
o.append(obstacle(50, 50, 70, 58))
while True:
keys = pygame.key.get_pressed()
screen.fill(white)
for obstacle in o:
screen.blit(obstacle.surf, (obstacle.x, obstacle.y))
pass
#code to check which way the player is touching an obstacle and deny walking into the obstacle
if ticker == 0:
if keys[K_UP]:
py -= 1
if keys[K_DOWN]:
py += 1
if keys[K_RIGHT]:
px += 1
if keys[K_LEFT]:
px -= 1
ticker = 15
if ticker > 0:
ticker -= 1
screen.blit(ball, (px, py))
pygame.event.get()
pygame.display.flip()
你可以使用pygame.Rect.colliderect(). 我会把它应用到障碍课上。在
相关问题 更多 >
编程相关推荐