import pygame,sys
pygame.init()#初始化操作
#保存窗口大小
width,height=600,400
screen=pygame.display.set_mode([width,height])#创建游戏窗口
#设置窗口标题
pygame.display.set_caption("愤怒的小鸟")
#加载小鸟素材
player=pygame.image.load("xiaoniao.png")
#获取图像矩形位置
rect=player.get_rect()
#声明XY运动速度的列表
speed = [3,2]
left_head = pygame.transform.flip(player,True,False)
right_head = player
#无限循环
while True:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
exit()
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player = left_head #小鸟的头向左
speed=[-2,1]
if event.key == pygame.K_RIGHT:
player = right_head #小鸟的头向左
speed=[2,1]
if event.key == pygame.K_UP:
player = left_head #小鸟的头向左
speed=[2,-1]
if event.key == pygame.K_DOWN:
player = right_head #小鸟的头向左
speed=[2,1]
rect =rect.move(speed)
if rect.right>width or rect.left0:
#将图片水平翻转 反转对象 是否水平反转 是否垂直翻转
player = pygame.transform.flip(player,True,False)
speed[0]=-speed[0]
if rect.bottom>height or rect.top0:
speed[1]=-speed[1]
screen.fill((255,255,255))
screen.blit(player,rect)
pygame.display.update()
pygame.time.delay(10)
到此这篇关于python pygame 愤怒的小鸟游戏示例代码的文章就介绍到这了,更多相关python愤怒的小鸟内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!