Python全系列 教程
3567个小节阅读:5930.4k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
通过加载图片来表示游戏中的坦克,此坦克代表我方坦克,完善坦克类。
xxxxxxxxxx
211class Tank():
2 def __init__(self,left,top):
3 self.images = {
4 'U':pygame.image.load('img/p1tankU.gif'),
5 'D':pygame.image.load('img/p1tankD.gif'),
6 'L':pygame.image.load('img/p1tankL.gif'),
7 'R':pygame.image.load('img/p1tankR.gif')
8 }
9 self.direction = 'U'
10 self.image = self.images[self.direction]
11 #坦克所在的区域 Rect->
12 self.rect = self.image.get_rect()
13 #指定坦克初始化位置 分别距x,y轴的位置
14 self.rect.left = left
15 self.rect.top = top
16 #展示坦克(将坦克这个surface绘制到窗口中 blit())
17 def displayTank(self):
18 #1.重新设置坦克的图片
19 self.image = self.images[self.direction]
20 #2.将坦克加入到窗口中
21 MainGame.window.blit(self.image,self.rect)
开始游戏方法,创建坦克,将坦克添加到窗口。
xxxxxxxxxx
71 def startGame(self):
2 #创建我方坦克
3 MainGame.TANK_P1 = Tank(400,300)
4 while True:
5 #将我方坦克加入到窗口中
6 MainGame.TANK_P1.displayTank()
7
运行结果: