0
import pygame
from pygame import *
import sys
pygame.init()
displayWidth = 800
displayHeight = 448
screen = pygame.display.set_mode((displayWidth, displayHeight))
clock = pygame.time.Clock()
black = (0, 0, 0)
level = [
"PPPPPPPPPPPPPPPPPPPPPPPPP",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P PPPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPPPPPPPPPPPPPPPPPPPPPPP",]
playerImg = pygame.image.load('monkeyStanding.png').convert_alpha()
bananaImg = pygame.image.load('Banana.jpg').convert_alpha()
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.dx = 0
self.dy = 0
self.onGround = False
self.image = playerImg
self.rect = self.image.get_rect()
self.rect.left = x
self.rect.top = y
def update(self, up, down, left, right, platforms):
if up:
#Only jump if on the ground
if self.onGround:
self.dy = -5
if left:
self.dx = -2
if right:
self.dx = 2
if not self.onGround:
self.dy += .1
if not (left or right):
self.dx = 0
self.rect.left += self.dx
self.collide(self.dx, 0, platforms)
self.rect.top += self.dy
self.onGround = False
self.collide(0, self.dy, platforms)
def collide(self, dx, dy, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if dx > 0:
self.rect.right = p.rect.left
print 'collide right'
if dx < 0:
self.rect.left = p.rect.right
print 'collide left'
if dy > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if dy < 0:
self.rect.top = p.rect.bottom
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill((255, 255, 255))
self.rect = Rect(x, y, 32, 32)
def update(self):
pass
class Banana(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = bananaImg
self.rect = self.image.get_rect()
self.rect.left = 100
self.rect.top = 100
self.dx = 2
def update(self):
self.rect.left += self.dx
def main():
up = down = left = right = False
bg = Surface((32, 32))
bg.convert()
bg.fill(black)
player = Player(100, 100)
banana = Banana()
platforms = []
allSprites = pygame.sprite.Group(player)
banSprites = pygame.sprite.Group()
x = y = 0
# build the level
for row in level:
for col in row:
if col == "P":
p = Platform(x, y)
platforms.append(p)
allSprites.add(p)
x += 32
y += 32
x = 0
while True:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
sys.exit()
if e.type == KEYDOWN:
if e.key == K_UP:
up = True
if e.key == K_LEFT:
left = True
if e.key == K_RIGHT:
right = True
if e.key == K_SPACE:
banSprites.add(banana)
if e.key == 27:
pygame.quit()
sys.exit()
if e.type == KEYUP:
if e.key == K_UP:
up = False
if e.key == K_RIGHT:
right = False
if e.key == K_LEFT:
left = False
if e.key == K_RIGHT:
right = False
# draw background
for y in range(32):
for x in range(32):
screen.blit(bg, (x * 32, y * 32))
allSprites.clear(screen, bg)
banSprites.clear(screen, bg)
# update player, draw everything else
player.update(up, down, left, right, platforms)
banSprites.update()
allSprites.draw(screen)
banSprites.draw(screen)
pygame.display.update()
if __name__ == '__main__':
main()
所以我想提出一個平臺的遊戲,一切都運作良好,到目前爲止,但是當我嘗試按空間,使香蕉的圖像就會使第一個,但一旦它飛走了屏幕,你不能再做了。有誰知道我做錯了什麼?我是pygame的新手,在主遊戲循環中調用類對象時仍然有點不穩定。謝謝!pygame的只有一個雪碧