因此,我试图用Python和Pygame创建一个拼图平台游戏,但我遇到了一点小麻烦。当我使用闪电式图像作为主要角色,而不是rect图像时,我如何制作碰撞检测器?我知道rect图像有左、右、上、下像素函数(这对于碰撞检测非常有用),但是对于闪电式图像是否有类似的功能呢?或者我只需要为x和y坐标+图像的宽度/高度创建一个变量?我试过用
import pygame, sys
from pygame.locals import *
WINDOWWIDTH = 400
WINDOWHEIGHT = 300
WHITE = (255, 255, 255)
catImg = pygame.image.load('cat.png')
catx = 0
caty = 0
catRight = catx + 100
catBot = caty + 100
moveRight = False
pygame.init()
FPS = 40 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Animation')
while True: # the main game loop
DISPLAYSURF.fill(WHITE)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key in (K_RIGHT, K_w):
moveRight = True
elif event.type == KEYUP:
if event.key in (K_RIGHT, K_w):
moveRight = False
if catRight == WINDOWWIDTH:
moveRight = False
if moveRight == True:
catx += 5
DISPLAYSURF.blit(catImg, (catx, caty))
pygame.display.update()
fpsClock.tick(FPS)但是catImg就在窗口的尽头继续前进。我做错了什么?提前谢谢。
发布于 2013-10-12 20:47:11
为了防止图像偏离右侧边缘,您需要计算它的x坐标可以具有的最大值,并确保该值永远不会超过。因此,在循环创建包含值的变量之前:
CAT_RIGHT_LIMIT = WINDOWWIDTH - catImg.get_width()然后在循环中检查它:
if catx >= CAT_RIGHT_LIMIT:
moveRight = False
catx = CAT_RIGHT_LIMIT
if moveRight == True:
catx += 5当然,你可以把这个想法扩展到所有其他的边缘。
发布于 2013-10-12 20:13:00
if catRight >= WINDOWWIDTH:
moveRight = False
catright = WINDOWHEIGHT
if moveRight == True:
catx += 5我想这就是你的错误所在。
https://stackoverflow.com/questions/19338311
复制相似问题