<p>最新版本是:</p>
<pre><code>import pygame
class GameObj(pygame.sprite.Sprite):
def __init__(self, image, x, y, s):
super(GameObj, self).__init__()
self.image = pygame.transform.scale(pygame.image.load("img/"+ image), (s, s))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Player(GameObj):
leftUnable = False
rightUnable = False
jumpHeight = 200
jumpSpeed = 5
image = "player.png"
inAir = True
def __init__(self, x, y, s=100):
super(Player,self).__init__(self.image, x, y, s)
class Block(GameObj):
image = "wall.png"
def __init__(self, x, y, s=100):
super(Block, self).__init__(self.image, x, y, s)
def collideNum(sprite, group):
total = 0
for member in group:
if(pygame.sprite.collide_rect(sprite, member)):
total += 1
return total
def setup():
pygame.init()
global display, player, block, sprites, living, noliving, clock
display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Test")
clock = pygame.time.Clock()
player = Player(100, 0)
block = Block(100, 300)
block1 = Block(200, 400)
block2 = Block(400, 400)
sprites = pygame.sprite.Group()
living = pygame.sprite.Group()
noliving = pygame.sprite.Group()
noliving.add(block)
noliving.add(block1)
noliving.add(block2)
living.add(player)
for liv in living:
sprites.add(liv)
for noliv in noliving:
sprites.add(noliv)
main()
def main():
speed = 5
gravity = 5
xCh, yCh = 0, 0
player.leftUnable = False
player.rightUnable = False
while True:
clock.tick(60)
display.fill((184, 184, 184))
yCh = gravity
for event in pygame.event.get():
if(event.type == pygame.QUIT):
quit()
elif(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_ESCAPE):
quit()
elif((event.key == pygame.K_a or event.key == pygame.K_LEFT) and not player.leftUnable):
for noliv in noliving:
if(pygame.sprite.collide_rect(player, noliv)):
if(noliv.rect.left < player.rect.left < noliv.rect.right and player.rect.bottom > noliv.rect.top + 5):
player.leftUnable = True
break
else:
xCh = -speed
player.leftUnable = False
else:
xCh = -speed
player.leftUnable = False
elif((event.key == pygame.K_d or event.key == pygame.K_RIGHT) and not player.rightUnable):
for noliv in noliving:
if(pygame.sprite.collide_rect(player, noliv)):
if(noliv.rect.left < player.rect.right < noliv.rect.right and player.rect.bottom > noliv.rect.top + 5):
player.rightUnable = True
break
else:
xCh = speed
player.rightUnable = False
else:
xCh = speed
player.rightUnable = False
elif(event.key == pygame.K_SPACE or event.key == pygame.K_w or event.key == pygame.K_UP):
oldPos = player.rect.bottom
xCh = 0
if(not player.inAir):
while player.rect.bottom > oldPos - player.jumpHeight:
clock.tick(60)
display.fill((184, 184, 184))
for ev in pygame.event.get():
if(ev.type == pygame.KEYDOWN):
if(ev.key == pygame.K_d or ev.key == pygame.K_RIGHT):
xCh = speed
elif(ev.key == pygame.K_a or ev.key == pygame.K_LEFT):
xCh = -speed
elif(ev.type == pygame.KEYUP):
xCh = 0
player.rect.x += xCh
player.rect.y -= player.jumpSpeed
player.inAir = True
sprites.draw(display)
pygame.display.update()
elif(event.type == pygame.KEYUP):
xCh = 0
for liv in living:
for noliv in noliving:
if(pygame.sprite.collide_rect(liv, noliv)):
liv.inAir = False
break
else:
liv.inAir = True
for noliv in noliving:
if(pygame.sprite.collide_rect(player, noliv)):
if(noliv.rect.left < player.rect.left < noliv.rect.right and player.rect.bottom > noliv.rect.top + 5):
player.leftUnable = True
if(collideNum(player, noliving) == 1):
player.inAir = True
if(xCh < 0):
xCh = 0
elif(noliv.rect.left < player.rect.right < noliv.rect.right and player.rect.bottom > noliv.rect.top + 5):
player.rightUnable = True
if(collideNum(player, noliving) == 1):
player.inAir = True
if(xCh > 0):
xCh = 0
else:
player.leftUnable = False
player.rightUnable = False
else:
player.leftUnable = False
player.rightUnable = False
if(not player.inAir):
yCh = 0
if(player.rect.top > display.get_size()[1]):
setup()
player.rect.x += xCh
player.rect.y += yCh
sprites.draw(display)
pygame.display.update()
setup()
</code></pre>
<p>它工作得很好。在</p>