我正在pygame中开发一个游戏,我希望有一个受重力影响的玩家。
我需要一个可以像这样运行的代码-
如果玩家没有与方块碰撞:player.y=player.y+1
所以当它下面有一个块时,不要摔倒……否则就摔倒。
这是整个代码:
import pygame,random
from pygame.locals import *
import itertools
pygame.init()
screen=pygame.display.set_mode((640,480))
class Block(object):
sprite = pygame.image.load("texture\\dirt.png").convert_alpha()
def __init__(self, x, y):
self.rect = self.sprite.get_rect(centery=y, centerx=x)
class Player(object):
sprite = pygame.image.load("texture\\playr.png").convert()
sprite.set_colorkey((0,255,0))
def __init__(self, x, y):
self.rect = self.sprite.get_rect(centery=y, centerx=x)
blocklist = []
player = []
while True:
screen.fill((25,30,90))
mse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == QUIT: exit()
key=pygame.key.get_pressed()
if key[K_LSHIFT]:
if event.type==MOUSEMOTION:
x=(int(mse[0]) / 32)*32
y=(int(mse[1]) / 32)*32
blocklist.append(Block(x+16,y+16))
else:
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
to_remove = [b for b in blocklist if b.rect.collidepoint(mse)]
for b in to_remove:
blocklist.remove(b)
if not to_remove:
x=(int(mse[0]) / 32)*32
y=(int(mse[1]) / 32)*32
blocklist.append(Block(x+16,y+16))
elif event.button == 3:
x=(int(mse[0]) / 32)*32
y=(int(mse[1]) / 32)*32
player=[]
player.append(Player(x+16,y+16))
for b in blocklist:
screen.blit(b.sprite, b.rect)
for p in player:
if p.rect.colliderect(blocklist):
print 'scoof'
screen.blit(p.sprite, p.rect)
pygame.display.update()
任何帮助表示赞赏!