pygame安装 pip install pygame
在CMD中输入python -m pygame.examples.aliens可以看到一个自带的pygame的演示小游戏
学了几天pygame,那就实践一下吧,如图实现玩家操控小鸟躲避小球的游戏,当碰撞到小球游戏结束,关闭游戏
所以我们需要实现以下几个功能:
1.操控小鸟
2.小球移动
3.碰撞检测
4.结束游戏
一. 控制小鸟我们用pygame.KEYDOWN来检测,当用户敲击下键盘上下左右时,改变鸟的位置坐标,并消除之前的图片
1 #键盘敲击事件
2 elif event.type ==pygame.KEYDOWN:3 #上
4 if event.key ==pygame.K_UP:5 if imageRect.top !=0:6 pygame.draw.rect(screen,THECOLORS['white'],imageRect)7 imageRect =imageRect.move(0,birdspeed[0])8 count = count + 1
9 #下
10 elif event.key ==pygame.K_DOWN:11 if imageRect.bottom != 360:12 pygame.draw.rect(screen, THECOLORS['white'], imageRect)13 imageRect = imageRect.move(0,birdspeed[1])14 count = count + 1
15 #左
16 elif event.key ==pygame.K_LEFT:17 if imageRect.left !=0:18 pygame.draw.rect(screen, THECOLORS['white'], imageRect)19 imageRect = imageRect.move(birdspeed[3],0)20 #右
21 elif event.key ==pygame.K_RIGH