在你的Python游戏中添加投掷机制

开发 后端
四处奔跑躲避敌人是一回事,反击敌人是另一回事。学习如何在这系列的第十二篇文章中在 Pygame 中创建平台游戏。

在你的Python游戏中添加投掷机制

四处奔跑躲避敌人是一回事,反击敌人是另一回事。学习如何在这系列的第十二篇文章中在 Pygame 中创建平台游戏。

这是仍在进行中的关于使用 Pygame 模块在 Python 3 中创建电脑游戏的第十二部分。先前的文章是:

  1. 通过构建一个简单的掷骰子游戏去学习怎么用 Python 编程
  2. 使用 Python 和 Pygame 模块构建一个游戏框架
  3. 如何在你的 Python 游戏中添加一个玩家
  4. 用 Pygame 使你的游戏角色移动起来
  5. 如何向你的 Python 游戏中添加一个敌人
  6. 在 Pygame 游戏中放置平台
  7. 在你的 Python 游戏中模拟引力
  8. 为你的 Python 平台类游戏添加跳跃功能
  9. 使你的 Python 游戏玩家能够向前和向后跑
  10. 在你的 Python 平台类游戏中放一些奖励
  11. 添加计分到你的 Python 游戏

我的上一篇文章本来是这一系列文章的最后一篇,它鼓励你为这个游戏编写自己的附加程序。你们很多人都这么做了!我收到了一些电子邮件,要求帮助我还没有涵盖的常用机制:战斗。毕竟,跳起来躲避坏人是一回事,但是有时候让他们走开是一件非常令人满意的事。在电脑游戏中向你的敌人投掷一些物品是很常见的,不管是一个火球、一支箭、一道闪电,还是其它适合游戏的东西。

与迄今为止你在这个系列中为你的平台游戏编程的任何东西不同,可投掷物品有一个生存时间。在你投掷一个物品后,它会如期在移动一段距离后消失。如果它是一支箭或其它类似的东西,它可能会在通过屏幕的边缘时而消失。如果它是一个火球或一道闪电,它可能会在一段时间后熄灭。

这意味着每次生成一个可投掷的物品时,也必须生成一个独特的衡量其生存时间的标准。为了介绍这个概念,这篇文章演示如何一次只投掷一个物品。(换句话说,每次仅存在一个投掷物品)。 一方面,这是一个游戏的限制条件,但另一方面,它却是游戏本身的运行机制。你的玩家不能每次同时投掷 50 个火球,因为每次仅允许一个投掷物品,所以当你的玩家释放一个火球来尝试击中一名敌人就成为了一项挑战。而在幕后,这也使你的代码保持简单。

如果你想启用每次投掷多个项目,在完成这篇教程后,通过学习这篇教程所获取的知识来挑战你自己。

创建 Throwable 类

如果你跟随学习这系列的其它文章,那么你应该熟悉在屏幕上生成一个新的对象基础的 __init__ 函数。这和你用来生成你的 玩家 和 敌人 的函数是一样的。这里是生成一个 throwable 对象的 __init__ 函数来:

  1. class Throwable(pygame.sprite.Sprite):
  2. """
  3. 生成一个 throwable 对象
  4. """
  5. def __init__(self, x, y, img, throw):
  6. pygame.sprite.Sprite.__init__(self)
  7. self.image = pygame.image.load(os.path.join('images',img))
  8. self.image.convert_alpha()
  9. self.image.set_colorkey(ALPHA)
  10. self.rect = self.image.get_rect()
  11. self.rect.x = x
  12. self.rect.y = y
  13. self.firing = throw

同你的 Player 类或 Enemy 类的 __init__ 函数相比,这个函数的主要区别是,它有一个 self.firing 变量。这个变量保持跟踪一个投掷的物品是否在当前屏幕上活动,因此当一个 throwable 对象创建时,将变量设置为 1 的合乎情理的。

判断存活时间

接下来,就像使用 Player 和 Enemy 一样,你需要一个 update 函数,以便投掷的物品在瞄准敌人抛向空中时,它会自己移动。

测定一个投掷的物品存活时间的最简单方法是侦测它何时离开屏幕。你需要监视的屏幕边缘取决于你投掷的物品的物理特性。

  • 如果你的玩家正在投掷的物品是沿着水平轴快速移动的,像一只弩箭或箭或一股非常快的魔法力量,而你想监视你游戏屏幕的水平轴极限。这可以通过 worldx 定义。
  • 如果你的玩家正在投掷的物品是沿着垂直方向或同时沿着水平方向和垂直方向移动的,那么你必须监视你游戏屏幕的垂直轴极限。这可以通过 worldy 定义。

这个示例假设你投掷的物品向前移动一点并最终落到地面上。不过,投掷的物品不会从地面上反弹起来,而是继续掉落出屏幕。你可以尝试不同的设置来看看什么最适合你的游戏:

  1. def update(self,worldy):
  2. '''
  3. 投掷物理学
  4. '''
  5. if self.rect.y < worldy: #垂直轴
  6. self.rect.x += 15 #它向前移动的速度有多快
  7. self.rect.y += 5 #它掉落的速度有多快
  8. else:
  9. self.kill() #移除投掷对象
  10. self.firing = 0 #解除火力发射

为使你的投掷物品移动地更快,增加 self.rect 的动量值。

如果投掷物品不在屏幕上,那么该物品将被销毁,以及释放其所占用的寄存器。另外,self.firing 将被设置回 0 以允许你的玩家来进行另一次射击。

设置你的投掷对象

就像使用你的玩家和敌人一样,你必须在你的设置部分中创建一个精灵组来保持投掷对象。

此外,你必须创建一个非活动的投掷对象来供开始的游戏使用。如果在游戏开始时却没有一个投掷对象,那么玩家在第一次尝试投掷一柄武器时,投掷将失败。

这个示例假设你的玩家使用一个火球作为开始的武器,因此,每一个投掷实例都是由 fire 变量指派的。在后面的关卡中,当玩家获取新的技能时,你可以使用相同的 Throwable 类来引入一个新的变量以使用一张不同的图像。

在这代码块中,前两行已经在你的代码中,因此不要重新键入它们:

  1. player_list = pygame.sprite.Group() #上下文
  2. player_list.add(player) #上下文
  3. fire = Throwable(player.rect.x,player.rect.y,'fire.png',0)
  4. firepower = pygame.sprite.Group()

注意,每一个投掷对象的起始位置都是和玩家所在的位置相同。这使得它看起来像是投掷对象来自玩家。在第一个火球生成时,使用 0 来显示 self.firing 是可用的。

在主循环中获取投掷行为

没有在主循环中出现的代码不会在游戏中使用,因此你需要在你的主循环中添加一些东西,以便能在你的游戏世界中获取投掷对象。

首先,添加玩家控制。当前,你没有火力触发器。在键盘上的按键是有两种状态的:释放的按键,按下的按键。为了移动,你要使用这两种状态:按下按键来启动玩家移动,释放按键来停止玩家移动。开火仅需要一个信号。你使用哪个按键事件(按键按下或按键释放)来触发你的投掷对象取决于你的品味。

在这个代码语句块中,前两行是用于上下文的:

  1. if event.key == pygame.K_UP or event.key == ord('w'):
  2. player.jump(platform_list)
  3. if event.key == pygame.K_SPACE:
  4. if not fire.firing:
  5. fire = Throwable(player.rect.x,player.rect.y,'fire.png',1)
  6. firepower.add(fire)

与你在设置部分创建的火球不同,你使用一个 1 来设置 self.firing 为不可用。

最后,你必须更新和绘制你的投掷物品。这个顺序很重要,因此把这段代码放置到你现有的 enemy.move 和 player_list.draw 的代码行之间:

  1. enemy.move() # 上下文
  2.  
  3. if fire.firing:
  4. fire.update(worldy)
  5. firepower.draw(world)
  6. player_list.draw(screen) # 上下文
  7. enemy_list.draw(screen) # 上下文

注意,这些更新仅在 self.firing 变量被设置为 1 时执行。如果它被设置为 0 ,那么 fire.firing 就不为 true,接下来就跳过更新。如果你尝试做上述这些更新,不管怎样,你的游戏都会崩溃,因为在游戏中将不会更新或绘制一个 fire 对象。

启动你的游戏,尝试挑战你的武器。

检测碰撞

如果你玩使用了新投掷技巧的游戏,你可能会注意到,你可以投掷对象,但是它却不会对你的敌人有任何影响。

原因是你的敌人没有被查到碰撞事故。一名敌人可能会被你的投掷物品所击中,但是敌人却从来不知道被击中了。

你已经在你的 Player 类中完成了碰撞检测,这非常类似。在你的 Enemy 类中,添加一个新的 update 函数:

  1. def update(self,firepower, enemy_list):
  2. """
  3. 检测火力碰撞
  4. """
  5. fire_hit_list = pygame.sprite.spritecollide(self,firepower,False)
  6. for fire in fire_hit_list:
  7. enemy_list.remove(self)

代码很简单。每个敌人对象都检查并看看它自己是否被 firepower 精灵组的成员所击中。如果它被击中,那么敌人就会从敌人组中移除和消失。

为集成这些功能到你的游戏之中,在主循环中调用位于新触发语句块中的函数:

  1. if fire.firing: # 上下文
  2. fire.update(worldy) # 上下文
  3. firepower.draw(screen) # 上下文
  4. enemy_list.update(firepower,enemy_list) # 更新敌人

你现在可以尝试一下你的游戏了,大多数的事情都如预期般的那样工作。不过,这里仍然有一个问题,那就是投掷的方向。

更改投掷机制的方向

当前,你英雄的火球只会向右移动。这是因为 Throwable 类的 update 函数将像素添加到火球的位置,在 Pygame 中,在 X 轴上一个较大的数字意味着向屏幕的右侧移动。当你的英雄转向另一个方向时,你可能希望它投掷的火球也抛向左侧。

到目前为止,你已经知道如何实现这一点,至少在技术上是这样的。然而,最简单的解决方案却是使用一个变量,在一定程度上对你来说可能是一种新的方法。一般来说,你可以“设置一个标记”(有时也被称为“翻转一个位”)来标明你的英雄所面向的方向。在你做完后,你就可以检查这个变量来得知火球是向左移动还是向右移动。

首先,在你的 Player 类中创建一个新的变量来代表你的游戏所面向的方向。因为我的游戏天然地面向右侧,由此我把面向右侧作为默认值:

  1. self.score = 0
  2. self.facing_right = True # 添加这行
  3. self.is_jumping = True

当这个变量是 True 时,你的英雄精灵是面向右侧的。当玩家每次更改英雄的方向时,变量也必须重新设置,因此,在你的主循环中相关的 keyup 事件中这样做:

  1. if event.type == pygame.KEYUP:
  2. if event.key == pygame.K_LEFT or event.key == ord('a'):
  3. player.control(steps, 0)
  4. player.facing_right = False # 添加这行
  5. if event.key == pygame.K_RIGHT or event.key == ord('d'):
  6. player.control(-steps, 0)
  7. player.facing_right = True # 添加这行

最后,更改你的 Throwable 类的 update 函数,以检测英雄是否面向右侧,并恰当地添加或减去来自火球位置的像素:

  1. if self.rect.y < worldy:
  2. if player.facing_right:
  3. self.rect.x += 15
  4. else:
  5. self.rect.x -= 15
  6. self.rect.y += 5

再次尝试你的游戏,清除掉你游戏世界中的一些坏人。

 

Python 平台类使用投掷能力

作为一项额外的挑战,当彻底打败敌人时,尝试增加你玩家的得分。

完整的代码

  1. #!/usr/bin/env python3
  2. # 作者: Seth Kenlon
  3.  
  4. # GPLv3
  5. # This program is free software: you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <[http://www.gnu.org/licenses/>][17].
  17.  
  18. import pygame
  19. import pygame.freetype
  20. import sys
  21. import os
  22.  
  23. '''
  24. 变量
  25. '''
  26.  
  27. worldx = 960
  28. worldy = 720
  29. fps = 40
  30. ani = 4
  31. world = pygame.display.set_mode([worldx, worldy])
  32. forwardx = 600
  33. backwardx = 120
  34.  
  35. BLUE = (80, 80, 155)
  36. BLACK = (23, 23, 23)
  37. WHITE = (254, 254, 254)
  38. ALPHA = (0, 255, 0)
  39.  
  40. tx = 64
  41. ty = 64
  42.  
  43. font_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "fonts", "amazdoom.ttf")
  44. font_size = tx
  45. pygame.freetype.init()
  46. myfont = pygame.freetype.Font(font_path, font_size)
  47.  
  48. '''
  49. 对象
  50. '''
  51.  
  52. def stats(score, health):
  53. myfont.render_to(world, (4, 4), "Score:"+str(score), BLUE, None, size=64)
  54. myfont.render_to(world, (4, 72), "Health:"+str(health), BLUE, None, size=64)
  55.  
  56. class Throwable(pygame.sprite.Sprite):
  57. """
  58. 生成一个投掷的对象
  59. """
  60. def __init__(self, x, y, img, throw):
  61. pygame.sprite.Sprite.__init__(self)
  62. self.image = pygame.image.load(os.path.join('images', img))
  63. self.image.convert_alpha()
  64. self.image.set_colorkey(ALPHA)
  65. self.rect = self.image.get_rect()
  66. self.rect.x = x
  67. self.rect.y = y
  68. self.firing = throw
  69.  
  70. def update(self, worldy):
  71. '''
  72. 投掷物理学
  73. '''
  74. if self.rect.y < worldy:
  75. if player.facing_right:
  76. self.rect.x += 15
  77. else:
  78. self.rect.x -= 15
  79. self.rect.y += 5
  80. else:
  81. self.kill()
  82. self.firing = 0
  83.  
  84. # x 位置, y 位置, img 宽度, img 高度, img 文件
  85. class Platform(pygame.sprite.Sprite):
  86. def __init__(self, xloc, yloc, imgw, imgh, img):
  87. pygame.sprite.Sprite.__init__(self)
  88. self.image = pygame.image.load(os.path.join('images', img)).convert()
  89. self.image.convert_alpha()
  90. self.image.set_colorkey(ALPHA)
  91. self.rect = self.image.get_rect()
  92. self.rect.y = yloc
  93. self.rect.x = xloc
  94.  
  95. class Player(pygame.sprite.Sprite):
  96. """
  97. 生成一名玩家
  98. """
  99.  
  100. def __init__(self):
  101. pygame.sprite.Sprite.__init__(self)
  102. self.movex = 0
  103. self.movey = 0
  104. self.frame = 0
  105. self.health = 10
  106. self.damage = 0
  107. self.score = 0
  108. self.facing_right = True
  109. self.is_jumping = True
  110. self.is_falling = True
  111. self.images = []
  112. for i in range(1, 5):
  113. img = pygame.image.load(os.path.join('images', 'walk' + str(i) + '.png')).convert()
  114. img.convert_alpha()
  115. img.set_colorkey(ALPHA)
  116. self.images.append(img)
  117. self.image = self.images[0]
  118. self.rect = self.image.get_rect()
  119.  
  120. def gravity(self):
  121. if self.is_jumping:
  122. self.movey += 3.2
  123.  
  124. def control(self, x, y):
  125. """
  126. 控制玩家移动
  127. """
  128. self.movex += x
  129.  
  130. def jump(self):
  131. if self.is_jumping is False:
  132. self.is_falling = False
  133. self.is_jumping = True
  134.  
  135. def update(self):
  136. """
  137. 更新精灵位置
  138. """
  139.  
  140. # 向左移动
  141. if self.movex < 0:
  142. self.is_jumping = True
  143. self.frame += 1
  144. if self.frame > 3 * ani:
  145. self.frame = 0
  146. self.image = pygame.transform.flip(self.images[self.frame // ani], True, False)
  147.  
  148. # 向右移动
  149. if self.movex > 0:
  150. self.is_jumping = True
  151. self.frame += 1
  152. if self.frame > 3 * ani:
  153. self.frame = 0
  154. self.image = self.images[self.frame // ani]
  155.  
  156. # 碰撞
  157. enemy_hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
  158. if self.damage == 0:
  159. for enemy in enemy_hit_list:
  160. if not self.rect.contains(enemy):
  161. self.damage = self.rect.colliderect(enemy)
  162. if self.damage == 1:
  163. idx = self.rect.collidelist(enemy_hit_list)
  164. if idx == -1:
  165. self.damage = 0 # 设置伤害回 0
  166. self.health -= 1 # 减去 1 单位健康度
  167.  
  168. ground_hit_list = pygame.sprite.spritecollide(self, ground_list, False)
  169. for g in ground_hit_list:
  170. self.movey = 0
  171. self.rect.bottom = g.rect.top
  172. self.is_jumping = False # 停止跳跃
  173.  
  174. # 掉落世界
  175. if self.rect.y > worldy:
  176. self.health -=1
  177. print(self.health)
  178. self.rect.x = tx
  179. self.rect.y = ty
  180.  
  181. plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)
  182. for p in plat_hit_list:
  183. self.is_jumping = False # 停止跳跃
  184. self.movey = 0
  185. if self.rect.bottom <= p.rect.bottom:
  186. self.rect.bottom = p.rect.top
  187. else:
  188. self.movey += 3.2
  189.  
  190. if self.is_jumping and self.is_falling is False:
  191. self.is_falling = True
  192. self.movey -= 33 # 跳跃多高
  193.  
  194. loot_hit_list = pygame.sprite.spritecollide(self, loot_list, False)
  195. for loot in loot_hit_list:
  196. loot_list.remove(loot)
  197. self.score += 1
  198. print(self.score)
  199.  
  200. plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)
  201.  
  202. self.rect.x += self.movex
  203. self.rect.y += self.movey
  204.  
  205. class Enemy(pygame.sprite.Sprite):
  206. """
  207. 生成一名敌人
  208. """
  209.  
  210. def __init__(self, x, y, img):
  211. pygame.sprite.Sprite.__init__(self)
  212. self.image = pygame.image.load(os.path.join('images', img))
  213. self.image.convert_alpha()
  214. self.image.set_colorkey(ALPHA)
  215. self.rect = self.image.get_rect()
  216. self.rect.x = x
  217. self.rect.y = y
  218. self.counter = 0
  219.  
  220. def move(self):
  221. """
  222. 敌人移动
  223. """
  224. distance = 80
  225. speed = 8
  226.  
  227. if self.counter >= 0 and self.counter <= distance:
  228. self.rect.x += speed
  229. elif self.counter >= distance and self.counter <= distance * 2:
  230. self.rect.x -= speed
  231. else:
  232. self.counter = 0
  233.  
  234. self.counter += 1
  235.  
  236. def update(self, firepower, enemy_list):
  237. """
  238. 检测火力碰撞
  239. """
  240. fire_hit_list = pygame.sprite.spritecollide(self, firepower, False)
  241. for fire in fire_hit_list:
  242. enemy_list.remove(self)
  243.  
  244. class Level:
  245. def ground(lvl, gloc, tx, ty):
  246. ground_list = pygame.sprite.Group()
  247. i = 0
  248. if lvl == 1:
  249. while i < len(gloc):
  250. ground = Platform(gloc[i], worldy - ty, tx, ty, 'tile-ground.png')
  251. ground_list.add(ground)
  252. i = i + 1
  253.  
  254. if lvl == 2:
  255. print("Level " + str(lvl))
  256.  
  257. return ground_list
  258.  
  259. def bad(lvl, eloc):
  260. if lvl == 1:
  261. enemy = Enemy(eloc[0], eloc[1], 'enemy.png')
  262. enemy_list = pygame.sprite.Group()
  263. enemy_list.add(enemy)
  264. if lvl == 2:
  265. print("Level " + str(lvl))
  266.  
  267. return enemy_list
  268.  
  269. # x 位置, y 位置, img 宽度, img 高度, img 文件
  270. def platform(lvl, tx, ty):
  271. plat_list = pygame.sprite.Group()
  272. ploc = []
  273. i = 0
  274. if lvl == 1:
  275. ploc.append((200, worldy - ty - 128, 3))
  276. ploc.append((300, worldy - ty - 256, 3))
  277. ploc.append((550, worldy - ty - 128, 4))
  278. while i &lt; len(ploc):
  279. j = 0
  280. while j <= ploc[i][2]:
  281. plat = Platform((ploc[i][0] + (j * tx)), ploc[i][1], tx, ty, 'tile.png')
  282. plat_list.add(plat)
  283. j = j + 1
  284. print('run' + str(i) + str(ploc[i]))
  285. i = i + 1
  286.  
  287. if lvl == 2:
  288. print("Level " + str(lvl))
  289.  
  290. return plat_list
  291.  
  292. def loot(lvl):
  293. if lvl == 1:
  294. loot_list = pygame.sprite.Group()
  295. loot = Platform(tx*5, ty*5, tx, ty, 'loot_1.png')
  296. loot_list.add(loot)
  297.  
  298. if lvl == 2:
  299. print(lvl)
  300.  
  301. return loot_list
  302.  
  303. '''
  304. Setup 部分
  305. '''
  306.  
  307. backdrop = pygame.image.load(os.path.join('images', 'stage.png'))
  308. clock = pygame.time.Clock()
  309. pygame.init()
  310. backdropbox = world.get_rect()
  311. main = True
  312.  
  313. player = Player() # 生成玩家
  314. player.rect.x = 0 # 转到 x
  315. player.rect.y = 30 # 转到 y
  316. player_list = pygame.sprite.Group()
  317. player_list.add(player)
  318. steps = 10
  319. fire = Throwable(player.rect.x, player.rect.y, 'fire.png', 0)
  320. firepower = pygame.sprite.Group()
  321.  
  322. eloc = []
  323. eloc = [300, worldy-ty-80]
  324. enemy_list = Level.bad(1, eloc)
  325. gloc = []
  326.  
  327. i = 0
  328. while i &lt;= (worldx / tx) + tx:
  329. gloc.append(i * tx)
  330. i = i + 1
  331.  
  332. ground_list = Level.ground(1, gloc, tx, ty)
  333. plat_list = Level.platform(1, tx, ty)
  334. enemy_list = Level.bad( 1, eloc )
  335. loot_list = Level.loot(1)
  336.  
  337. '''
  338. 主循环
  339. '''
  340.  
  341. while main:
  342. for event in pygame.event.get():
  343. if event.type == pygame.QUIT:
  344. pygame.quit()
  345. try:
  346. sys.exit()
  347. finally:
  348. main = False
  349.  
  350. if event.type == pygame.KEYDOWN:
  351. if event.key == ord('q'):
  352. pygame.quit()
  353. try:
  354. sys.exit()
  355. finally:
  356. main = False
  357. if event.key == pygame.K_LEFT or event.key == ord('a'):
  358. player.control(-steps, 0)
  359. if event.key == pygame.K_RIGHT or event.key == ord('d'):
  360. player.control(steps, 0)
  361. if event.key == pygame.K_UP or event.key == ord('w'):
  362. player.jump()
  363.  
  364. if event.type == pygame.KEYUP:
  365. if event.key == pygame.K_LEFT or event.key == ord('a'):
  366. player.control(steps, 0)
  367. player.facing_right = False
  368. if event.key == pygame.K_RIGHT or event.key == ord('d'):
  369. player.control(-steps, 0)
  370. player.facing_right = True
  371. if event.key == pygame.K_SPACE:
  372. if not fire.firing:
  373. fire = Throwable(player.rect.x, player.rect.y, 'fire.png', 1)
  374. firepower.add(fire)
  375.  
  376. # 向向滚动世界
  377. if player.rect.x >= forwardx:
  378. scroll = player.rect.x - forwardx
  379. player.rect.x = forwardx
  380. for p in plat_list:
  381. p.rect.x -= scroll
  382. for e in enemy_list:
  383. e.rect.x -= scroll
  384. for l in loot_list:
  385. l.rect.x -= scroll
  386.  
  387. # 向后滚动世界
  388. if player.rect.x <= backwardx:
  389. scroll = backwardx - player.rect.x
  390. player.rect.x = backwardx
  391. for p in plat_list:
  392. p.rect.x += scroll
  393. for e in enemy_list:
  394. e.rect.x += scroll
  395. for l in loot_list:
  396. l.rect.x += scroll
  397.  
  398. world.blit(backdrop, backdropbox)
  399. player.update()
  400. player.gravity()
  401. player_list.draw(world)
  402. if fire.firing:
  403. fire.update(worldy)
  404. firepower.draw(world)
  405. enemy_list.draw(world)
  406. enemy_list.update(firepower, enemy_list)
  407. loot_list.draw(world)
  408. ground_list.draw(world)
  409. plat_list.draw(world)
  410. for e in enemy_list:
  411. e.move()
  412. stats(player.score, player.health)
  413. pygame.display.flip()
  414. clock.tick(fps)

 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2020-01-14 12:05:20

Python游戏引力

2019-05-21 13:55:22

Python编程语言游戏

2020-11-30 13:33:25

Python平台类游戏编程语言

2019-05-21 21:36:42

Python编程语言游戏

2010-02-01 14:48:43

2010-03-11 18:57:17

Python脚本

2015-08-11 08:51:40

游戏死亡

2012-12-25 10:51:39

IBMdW

2013-04-03 15:10:09

GMGC全球移动游戏大

2020-12-02 09:46:08

Python游戏编程语言

2012-06-25 10:11:48

2022-05-27 11:22:40

Canvas超级玛丽游戏

2017-05-02 13:45:14

2011-11-02 14:55:53

移动游戏LBS位置识别

2019-05-27 15:00:17

Pygame游戏平台

2015-09-23 10:25:41

Docker英雄联盟Docker实践

2011-08-24 11:14:25

LUA 游戏

2017-07-26 15:59:51

寻路算法Dijkstra游戏

2022-01-12 10:37:09

区块链技术金融

2020-11-30 13:45:35

Python游戏编程语言
点赞
收藏

51CTO技术栈公众号