• python/pygame 挑战魂斗罗 笔记(三)


    感觉最难的部分已经解决了,下面开始发射子弹。

    一、建立ContraBullet.py文件,Bullit类:

    1、设定子弹速度

    Config.py中设定子弹移动速度为常量Constant.BULLET_SPEED = 8。

    2、载入子弹图片:

    图片也是6张,子弹发出后逐渐变大(这是为什么呢)?同样建立bullet_list列表和bullet_number索引。载入图片后获取rect属性再按照人物放大比例放大,加到子弹列表中。

    3、子弹图片索引

    设置一个计数器变量counter,随着循环增加到30,也就是1/2个FPS,就让子弹图片索引增加1,改变子弹图片样式。

    1. import os.path
    2. import pygame
    3. from Config import Constant
    4. class Bullet(pygame.sprite.Sprite):
    5. def __init__(self, x, y):
    6. pygame.sprite.Sprite.__init__(self)
    7. self.bullet_list = []
    8. self.bullet_number = 0
    9. for i in range(6):
    10. image = pygame.image.load(os.path.join('image', 'bullet', 'bullet' + str(i + 1) + '.png'))
    11. rect = image.get_rect()
    12. image = pygame.transform.scale(
    13. image, (rect.width * Constant.PLAYER_SCALE, rect.height * Constant.PLAYER_SCALE))
    14. self.bullet_list.append(image)
    15. self.image = self.bullet_list[self.bullet_number]
    16. self.rect = self.image.get_rect()
    17. self.rect.centerx = x
    18. self.rect.centery = y
    19. self.speed_x = 0
    20. self.speed_y = 0
    21. self.counter = 0
    22. def update(self):
    23. if self.counter >= 30:
    24. self.bullet_number += 1
    25. if self.bullet_number > 5:
    26. self.bullet_number = 0
    27. else:
    28. self.counter += 1
    29. self.image = self.bullet_list[self.bullet_number]
    30. self.rect.x += self.speed_x
    31. self.rect.y += self.speed_y

    二、Bill类增加按键判定以及shoot发射方法:

    1、设定k键为发射子弹以及子弹发射间隔时间、子弹x、y方向移速和不同角度子弹发射位置等变量。
    1. self.bullet_time = 0
    2. self.bullet_speed_x = 0
    3. self.bullet_speed_y = 0
    4. self.bullet_x = self.rect.right
    5. self.bullet_y = self.rect.y + 11.5 * Constant.PLAYER_SCALE
    2、建立shoot发射子弹方法:

    主要是理清各种方向、状态下子弹的发射位置和x、y方向的速度。

    1. def shoot(self):
    2. if self.direction == 'right':
    3. self.bullet_speed_x = Constant.BULLET_SPEED
    4. self.bullet_x = self.rect.right
    5. elif self.direction == 'left':
    6. self.bullet_speed_x = -Constant.BULLET_SPEED
    7. self.bullet_x = self.rect.left
    8. if self.image_type == 'stand':
    9. self.bullet_y = self.rect.y + 11.5 * Constant.PLAYER_SCALE
    10. self.bullet_speed_y = 0
    11. elif self.image_type == 'down':
    12. self.bullet_y = self.rect.y + 24 * Constant.PLAYER_SCALE
    13. self.bullet_speed_y = 0
    14. elif self.image_type == 'up':
    15. self.bullet_y = self.rect.y
    16. self.bullet_speed_x = 0
    17. self.bullet_speed_y = -Constant.BULLET_SPEED
    18. elif self.image_type == 'oblique_down':
    19. self.bullet_y = self.rect.y + 20.5 * Constant.PLAYER_SCALE
    20. self.bullet_speed_y = Constant.BULLET_SPEED
    21. elif self.image_type == 'oblique_up':
    22. self.bullet_y = self.rect.y
    23. self.bullet_speed_y = -Constant.BULLET_SPEED
    24. if self.bullet_time >= 30:
    25. bill_bullet = ContraBullet.Bullet(self.bullet_x, self.bullet_y)
    26. bill_bullet.speed_x = self.bullet_speed_x
    27. bill_bullet.speed_y = self.bullet_speed_y
    28. Variable.all_sprites.add(bill_bullet)
    29. self.bullet_time = 0
    30. else:
    31. self.bullet_time += 1

    子弹发射成功! 

    三、添加敌人:

    1、敌人图片载入

    敌人就简单一点吧,只选择了一个图片形象的,PS处理完,一共7张图片,6个行走运动的,1个射击状态的,这里让敌人从游戏窗口外面开始落下来,坠落状态的图片就省去了。

    设定一个敌人图片列表,同样需要载入图片并按照PLAYER_SCALE比例放大。

    再设定一些变量,来控制敌人的产生时间间隔以及发射子弹间隔等。

    1. import os.path
    2. import random
    3. import pygame
    4. from Config import Constant, Variable
    5. class Enemy(pygame.sprite.Sprite):
    6. def __init__(self):
    7. pygame.sprite.Sprite.__init__(self)
    8. self.enemy_list = []
    9. for i in range(7):
    10. image = pygame.image.load(os.path.join('image', 'enemy', 'enemy' + str(i + 1) + '.png'))
    11. rect = image.get_rect()
    12. image = pygame.transform.scale(
    13. image, (rect.width * Constant.PLAYER_SCALE, rect.height * Constant.PLAYER_SCALE))
    14. image = pygame.transform.flip(image, True, False)
    15. self.enemy_list.append(image)
    16. self.order = 0
    17. self.shooting = False
    18. if self.shooting:
    19. self.image = self.enemy_list[6]
    20. else:
    21. self.image = self.enemy_list[self.order]
    22. self.rect = self.image.get_rect()
    23. self.rect.x = random.randrange(Constant.WIDTH, Constant.WIDTH * 5)
    24. self.rect.y = 100
    25. self.counter = 0
    26. self.speed = 2
    27. self.floor = 0
    28. self.last_time = 0
    29. self.now_time = 0
    2、敌人的产生方法

    定义一个敌人的组,用来判定碰撞及控制敌人数量。

    0-100之间取一个随机数,等与50时产生一个敌人,分别加入敌人组和所有精灵组。

    1. def new_enemy():
    2. if Variable.step == 2 and len(Variable.enemy_sprites) <= 3:
    3. i = random.randint(0, 100)
    4. if i == 50:
    5. enemy = Enemy()
    6. Variable.all_sprites.add(enemy)
    7. Variable.enemy_sprites.add(enemy)
    3、移动及射击

    先写个索引循环方法,控制order,每完成一次移动图片,计数器counter+1,到5时射击一次。也就是6张移动图片轮回5次就射击一次。

    1. def order_loop(self):
    2. self.now_time = pygame.time.get_ticks()
    3. if self.now_time - self.last_time >= 150:
    4. self.order += 1
    5. if self.order > 6:
    6. self.counter += 1
    7. self.order = 0
    8. if self.counter >= 5:
    9. self.shooting = True
    10. self.last_time = self.now_time

     再定义一个射击方法,子弹的位置和移动速度

    1. def shoot(self):
    2. enemy_bullet = ContraBullet.Bullet(self.rect.left, self.rect.y + 7.5 * Constant.PLAYER_SCALE)
    3. enemy_bullet.speed_x = -10
    4. Variable.all_sprites.add(enemy_bullet)
    5. self.shooting = False
    6. self.counter = 0

     更新update,实现移动、图片改变及射击。同样也是让敌人一直处于下落状态,碰到地面碰撞体就停止下落,并向左移动。这里写的比较简单,没有考虑敌人的跳跃问题。

    1. def update(self):
    2. if self.shooting:
    3. self.image = self.enemy_list[6]
    4. else:
    5. self.image = self.enemy_list[self.order]
    6. self.rect.y += Constant.GRAVITY * 10
    7. self.order_loop()
    8. if self.shooting:
    9. self.shoot()
    10. collider_ground = pygame.sprite.groupcollide(Variable.enemy_sprites, Variable.collider, False, False)
    11. for e, l in collider_ground.items():
    12. self.floor = l[0].rect.top
    13. e.rect.bottom = self.floor
    14. e.rect.x -= self.speed

    四、全部文件:

    剩下的就是各种碰撞检测和关头了,这里就不再写啦。把所有文件都完整贴一遍。

    1、Contra.py主循环文件

    感觉都挺长时间没改动这个文件了,就增加了一个ContraEnemy.new_enemy()。

    1. # Contra.py
    2. import sys
    3. import pygame
    4. import ContraBill
    5. import ContraMap
    6. import ContraEnemy
    7. from Config import Constant, Variable
    8. def control():
    9. for event in pygame.event.get():
    10. if event.type == pygame.QUIT:
    11. Variable.game_start = False
    12. if event.type == pygame.KEYDOWN:
    13. if event.key == pygame.K_RETURN:
    14. Variable.step = 1
    15. class Main:
    16. def __init__(self):
    17. pygame.init()
    18. self.game_window = pygame.display.set_mode((Constant.WIDTH, Constant.HEIGHT))
    19. self.clock = pygame.time.Clock()
    20. def game_loop(self):
    21. while Variable.game_start:
    22. control()
    23. if Variable.stage == 1:
    24. ContraMap.new_stage()
    25. ContraBill.new_player()
    26. ContraEnemy.new_enemy()
    27. if Variable.stage == 2:
    28. pass
    29. if Variable.stage == 3:
    30. pass
    31. Variable.all_sprites.draw(self.game_window)
    32. Variable.all_sprites.update()
    33. self.clock.tick(Constant.FPS)
    34. pygame.display.set_caption(f'魂斗罗 1.0 {self.clock.get_fps():.2f}')
    35. pygame.display.update()
    36. pygame.quit()
    37. sys.exit()
    38. if __name__ == '__main__':
    39. main = Main()
    40. main.game_loop()
    2、ContraMap.py第一关背景地图及地面碰撞体文件

    别的关的背景地图也是按照这个方式,再单独测算每个台阶的位置和长度,一条条的把地面碰撞体给绘制出来就可以了。

    1. # ContraMap.py
    2. import os
    3. import pygame
    4. from Config import Constant, Variable
    5. class StageMap(pygame.sprite.Sprite):
    6. def __init__(self, order):
    7. pygame.sprite.Sprite.__init__(self)
    8. self.image_list = []
    9. self.order = order
    10. for i in range(1, 9):
    11. image = pygame.image.load(os.path.join('image', 'map', 'stage' + str(i) + '.png'))
    12. rect = image.get_rect()
    13. image = pygame.transform.scale(image, (rect.width * Constant.MAP_SCALE, rect.height * Constant.MAP_SCALE))
    14. self.image_list.append(image)
    15. self.image = self.image_list[self.order]
    16. self.rect = self.image.get_rect()
    17. self.rect.x = 0
    18. self.rect.y = 0
    19. self.speed = 0
    20. def update(self):
    21. if self.order == 2:
    22. print('纵向地图')
    23. else:
    24. if Variable.step == 0 and self.rect.x >= -Constant.WIDTH:
    25. self.rect.x -= 10
    26. if Variable.step == 1 and self.rect.x > -Constant.WIDTH * 2:
    27. self.rect.x -= 10
    28. if self.rect.x == -Constant.WIDTH * 2:
    29. Variable.step = 2
    30. Variable.all_sprites.add(Variable.collider)
    31. def new_stage():
    32. if Variable.map_switch:
    33. stage_map = StageMap(Variable.stage - 1)
    34. Variable.all_sprites.add(stage_map)
    35. Variable.map_storage.add(stage_map)
    36. Variable.map_switch = False
    37. class CollideGround(pygame.sprite.Sprite):
    38. def __init__(self, length, x, y):
    39. pygame.sprite.Sprite.__init__(self)
    40. self.image = pygame.Surface((length * Constant.MAP_SCALE, 3))
    41. self.image.fill((255, 0, 0))
    42. self.rect = self.image.get_rect()
    43. self.rect.x = x * Constant.MAP_SCALE - Constant.WIDTH * 2
    44. self.rect.y = y * Constant.MAP_SCALE
    45. Variable.collider83.add(
    46. CollideGround(511, 2176, 83),
    47. CollideGround(161, 2847, 83),
    48. CollideGround(64, 3296, 83)
    49. )
    50. Variable.collider115.add(
    51. CollideGround(736, 832, 115),
    52. CollideGround(128, 1568, 115),
    53. CollideGround(160, 1695, 115),
    54. CollideGround(128, 1856, 115),
    55. CollideGround(256, 1984, 115),
    56. CollideGround(224, 2656, 115),
    57. CollideGround(65, 3040, 115),
    58. CollideGround(64, 3264, 115),
    59. CollideGround(64, 3392, 115),
    60. CollideGround(128, 3808, 115)
    61. )
    62. Variable.collider146.add(
    63. CollideGround(97, 959, 146),
    64. CollideGround(66, 1215, 146),
    65. CollideGround(225, 2400, 146),
    66. CollideGround(96, 2976, 146),
    67. CollideGround(64, 3136, 146),
    68. CollideGround(160, 3424, 146),
    69. CollideGround(64, 3744, 146),
    70. CollideGround(32, 3936, 146)
    71. )
    72. Variable.collider163.add(
    73. CollideGround(95, 1440, 163),
    74. CollideGround(64, 2304, 163),
    75. CollideGround(32, 2912, 163),
    76. CollideGround(32, 2328, 163),
    77. CollideGround(32, 3328, 163),
    78. CollideGround(97, 3840, 163)
    79. )
    80. Variable.collider178.add(
    81. CollideGround(32, 1055, 178),
    82. CollideGround(32, 1151, 178),
    83. CollideGround(65, 2720, 178),
    84. CollideGround(64, 2816, 178),
    85. CollideGround(96, 3168, 178),
    86. CollideGround(63, 3648, 178),
    87. CollideGround(32, 3969, 178)
    88. )
    89. Variable.collider211.add(
    90. CollideGround(63, 1088, 211),
    91. CollideGround(63, 1407, 211),
    92. CollideGround(97, 2208, 211),
    93. CollideGround(192, 2528, 211),
    94. CollideGround(33, 3135, 211),
    95. CollideGround(33, 3295, 211),
    96. CollideGround(97, 3520, 211),
    97. CollideGround(242, 3807, 211)
    98. )
    99. Variable.collider.add(Variable.collider83, Variable.collider115, Variable.collider146, Variable.collider163,
    100. Variable.collider178, Variable.collider211, Variable.collider231)
    3、ContraBill.py主角玩家文件

    主角Bill向下跳跃,后来想了想,是不是可以考虑写一个矮点的碰撞体紧随Bill的脚下,然后由这个碰撞体来检测是否和地面碰撞体碰撞,这样就不用等Bill的top超过地面碰撞体就可以把它加回来了。而且在往上跳时,也不会因为两层地面距离太近直接跳到上面层了,感觉体验感应该会好些。

    1. # ContraBill.py
    2. import os
    3. import pygame
    4. import ContraBullet
    5. from Config import Constant, Variable
    6. class Bill(pygame.sprite.Sprite):
    7. def __init__(self):
    8. pygame.sprite.Sprite.__init__(self)
    9. self.image_dict = {
    10. 'be_hit': [],
    11. 'down': [],
    12. 'jump': [],
    13. 'oblique_down': [],
    14. 'oblique_up': [],
    15. 'run': [],
    16. 'shoot': [],
    17. 'stand': [],
    18. 'up': []
    19. }
    20. for i in range(6):
    21. for key in self.image_dict:
    22. image = pygame.image.load(os.path.join('image', 'bill', str(key), str(key) + str(i + 1) + '.png'))
    23. rect = image.get_rect()
    24. image_scale = pygame.transform.scale(
    25. image, (rect.width * Constant.PLAYER_SCALE, rect.height * Constant.PLAYER_SCALE))
    26. self.image_dict[key].append(image_scale)
    27. self.image_order = 0
    28. self.image_type = 'stand'
    29. self.image = self.image_dict[self.image_type][self.image_order]
    30. self.rect = self.image.get_rect()
    31. self.rect.x = 100
    32. self.rect.y = 100
    33. self.direction = 'right'
    34. self.now_time = 0
    35. self.last_time = 0
    36. self.falling = True
    37. self.jumping = False
    38. self.moving = False
    39. self.floor = 0
    40. self.bullet_time = 0
    41. self.bullet_speed_x = 0
    42. self.bullet_speed_y = 0
    43. self.bullet_x = self.rect.right
    44. self.bullet_y = self.rect.y + 11.5 * Constant.PLAYER_SCALE
    45. def update(self):
    46. self.image = self.image_dict[self.image_type][self.image_order]
    47. if self.direction == 'left':
    48. self.image = pygame.transform.flip(self.image, True, False)
    49. if self.rect.top >= self.floor:
    50. for i in Variable.sprite_storage:
    51. Variable.collider.add(i)
    52. Variable.sprite_storage.remove(i)
    53. key_pressed = pygame.key.get_pressed()
    54. if self.falling:
    55. self.fall()
    56. if self.jumping:
    57. self.jump()
    58. if self.moving:
    59. self.move()
    60. self.order_loop()
    61. collider_ground = pygame.sprite.groupcollide(Variable.player_sprites, Variable.collider, False, False)
    62. for p, l in collider_ground.items():
    63. self.floor = l[0].rect.top
    64. p.rect.bottom = self.floor
    65. if key_pressed[pygame.K_k]:
    66. self.shoot()
    67. if key_pressed[pygame.K_d]:
    68. self.direction = 'right'
    69. self.moving = True
    70. if key_pressed[pygame.K_w]:
    71. self.image_type = 'oblique_up'
    72. elif key_pressed[pygame.K_s]:
    73. self.image_type = 'oblique_down'
    74. elif key_pressed[pygame.K_j]:
    75. self.image_type = 'jump'
    76. self.jumping = True
    77. self.falling = False
    78. else:
    79. self.image_type = 'run'
    80. elif key_pressed[pygame.K_a]:
    81. self.direction = 'left'
    82. self.moving = True
    83. if key_pressed[pygame.K_w]:
    84. self.image_type = 'oblique_up'
    85. elif key_pressed[pygame.K_s]:
    86. self.image_type = 'oblique_down'
    87. elif key_pressed[pygame.K_j]:
    88. self.image_type = 'jump'
    89. self.jumping = True
    90. self.falling = False
    91. else:
    92. self.image_type = 'run'
    93. elif key_pressed[pygame.K_w]:
    94. self.image_type = 'up'
    95. self.moving = False
    96. elif key_pressed[pygame.K_s]:
    97. self.image_type = 'down'
    98. self.moving = False
    99. else:
    100. self.image_type = 'stand'
    101. self.moving = False
    102. if key_pressed[pygame.K_s] and key_pressed[pygame.K_j]:
    103. self.image_type = 'oblique_down'
    104. Variable.collider.remove(l[0])
    105. Variable.sprite_storage.add(l[0])
    106. def shoot(self):
    107. if self.direction == 'right':
    108. self.bullet_speed_x = Constant.BULLET_SPEED
    109. self.bullet_x = self.rect.right
    110. elif self.direction == 'left':
    111. self.bullet_speed_x = -Constant.BULLET_SPEED
    112. self.bullet_x = self.rect.left
    113. if self.image_type == 'stand':
    114. self.bullet_y = self.rect.y + 11.5 * Constant.PLAYER_SCALE
    115. self.bullet_speed_y = 0
    116. elif self.image_type == 'down':
    117. self.bullet_y = self.rect.y + 24 * Constant.PLAYER_SCALE
    118. self.bullet_speed_y = 0
    119. elif self.image_type == 'up':
    120. self.bullet_y = self.rect.y
    121. self.bullet_speed_x = 0
    122. self.bullet_speed_y = -Constant.BULLET_SPEED
    123. elif self.image_type == 'oblique_down':
    124. self.bullet_y = self.rect.y + 20.5 * Constant.PLAYER_SCALE
    125. self.bullet_speed_y = Constant.BULLET_SPEED
    126. elif self.image_type == 'oblique_up':
    127. self.bullet_y = self.rect.y
    128. self.bullet_speed_y = -Constant.BULLET_SPEED
    129. if self.bullet_time >= 30:
    130. bill_bullet = ContraBullet.Bullet(self.bullet_x, self.bullet_y)
    131. bill_bullet.speed_x = self.bullet_speed_x
    132. bill_bullet.speed_y = self.bullet_speed_y
    133. Variable.all_sprites.add(bill_bullet)
    134. self.bullet_time = 0
    135. else:
    136. self.bullet_time += 1
    137. def order_loop(self):
    138. self.now_time = pygame.time.get_ticks()
    139. if self.now_time - self.last_time >= 100:
    140. self.image_order += 1
    141. if self.image_order > 5:
    142. self.image_order = 0
    143. self.last_time = self.now_time
    144. def move(self):
    145. if self.direction == 'right' and self.rect.right <= Constant.WIDTH - 80 * Constant.MAP_SCALE:
    146. self.rect.x += Constant.SPEED_X
    147. if self.rect.centerx >= Constant.WIDTH / 2:
    148. x = self.rect.centerx - Constant.WIDTH / 2
    149. for j in Variable.map_storage:
    150. if j.rect.right >= Constant.WIDTH:
    151. for i in Variable.all_sprites:
    152. i.rect.x -= x
    153. elif self.direction == 'left' and self.rect.left >= 40 * Constant.MAP_SCALE:
    154. self.rect.x -= Constant.SPEED_X
    155. if self.rect.centerx <= Constant.WIDTH / 2:
    156. x = Constant.WIDTH / 2 - self.rect.centerx
    157. for j in Variable.map_storage:
    158. if j.rect.left <= -Constant.WIDTH * 2:
    159. for i in Variable.all_sprites:
    160. i.rect.x += x
    161. def jump(self):
    162. Constant.SPEED_Y += Constant.GRAVITY
    163. self.rect.y += Constant.SPEED_Y
    164. if Constant.SPEED_Y == 0:
    165. self.jumping = False
    166. self.falling = True
    167. Constant.SPEED_Y = -10
    168. def fall(self):
    169. self.rect.y += Constant.GRAVITY * 10
    170. def new_player():
    171. if Variable.step == 2 and Variable.player_switch:
    172. bill = Bill()
    173. Variable.all_sprites.add(bill)
    174. Variable.player_sprites.add(bill)
    175. Variable.player_switch = False
    4、ContraEnemy.py

    敌人这里写的太简单了,连出游戏窗口的判定都没写!我记得正常游戏里面还有一些在墙上的炮台啥的,还有随机掉落的各种buff以及关头。

    1. import os.path
    2. import random
    3. import pygame
    4. import ContraBullet
    5. from Config import Constant, Variable
    6. class Enemy(pygame.sprite.Sprite):
    7. def __init__(self):
    8. pygame.sprite.Sprite.__init__(self)
    9. self.enemy_list = []
    10. for i in range(7):
    11. image = pygame.image.load(os.path.join('image', 'enemy', 'enemy' + str(i + 1) + '.png'))
    12. rect = image.get_rect()
    13. image = pygame.transform.scale(
    14. image, (rect.width * Constant.PLAYER_SCALE, rect.height * Constant.PLAYER_SCALE))
    15. image = pygame.transform.flip(image, True, False)
    16. self.enemy_list.append(image)
    17. self.order = 0
    18. self.shooting = False
    19. if self.shooting:
    20. self.image = self.enemy_list[6]
    21. else:
    22. self.image = self.enemy_list[self.order]
    23. self.rect = self.image.get_rect()
    24. self.rect.x = random.randrange(Constant.WIDTH, Constant.WIDTH * 5)
    25. self.rect.y = 100
    26. self.counter = 0
    27. self.speed = 2
    28. self.floor = 0
    29. self.last_time = 0
    30. self.now_time = 0
    31. def update(self):
    32. if self.shooting:
    33. self.image = self.enemy_list[6]
    34. else:
    35. self.image = self.enemy_list[self.order]
    36. self.rect.y += Constant.GRAVITY * 10
    37. self.order_loop()
    38. if self.shooting:
    39. self.shoot()
    40. collider_ground = pygame.sprite.groupcollide(Variable.enemy_sprites, Variable.collider, False, False)
    41. for e, l in collider_ground.items():
    42. self.floor = l[0].rect.top
    43. e.rect.bottom = self.floor
    44. e.rect.x -= self.speed
    45. def order_loop(self):
    46. self.now_time = pygame.time.get_ticks()
    47. if self.now_time - self.last_time >= 150:
    48. self.order += 1
    49. if self.order > 6:
    50. self.counter += 1
    51. self.order = 0
    52. if self.counter >= 5:
    53. self.shooting = True
    54. self.last_time = self.now_time
    55. def shoot(self):
    56. enemy_bullet = ContraBullet.Bullet(self.rect.left, self.rect.y + 7.5 * Constant.PLAYER_SCALE)
    57. enemy_bullet.speed_x = -10
    58. Variable.all_sprites.add(enemy_bullet)
    59. self.shooting = False
    60. self.counter = 0
    61. def new_enemy():
    62. if Variable.step == 2 and len(Variable.enemy_sprites) <= 3:
    63. i = random.randint(0, 100)
    64. if i == 50:
    65. enemy = Enemy()
    66. Variable.all_sprites.add(enemy)
    67. Variable.enemy_sprites.add(enemy)
    5、ContraBullet.py

    子弹这里没有区分双方的子弹,同样也没写出游戏窗口后kill。

    1. import os.path
    2. import pygame
    3. from Config import Constant
    4. class Bullet(pygame.sprite.Sprite):
    5. def __init__(self, x, y):
    6. pygame.sprite.Sprite.__init__(self)
    7. self.bullet_list = []
    8. self.bullet_number = 0
    9. for i in range(6):
    10. image = pygame.image.load(os.path.join('image', 'bullet', 'bullet' + str(i + 1) + '.png'))
    11. rect = image.get_rect()
    12. image = pygame.transform.scale(
    13. image, (rect.width * Constant.PLAYER_SCALE, rect.height * Constant.PLAYER_SCALE))
    14. self.bullet_list.append(image)
    15. self.image = self.bullet_list[self.bullet_number]
    16. self.rect = self.image.get_rect()
    17. self.rect.centerx = x
    18. self.rect.centery = y
    19. self.speed_x = 0
    20. self.speed_y = 0
    21. self.counter = 0
    22. def update(self):
    23. if self.counter >= 30:
    24. self.bullet_number += 1
    25. if self.bullet_number > 5:
    26. self.bullet_number = 0
    27. else:
    28. self.counter += 1
    29. self.image = self.bullet_list[self.bullet_number]
    30. self.rect.x += self.speed_x
    31. self.rect.y += self.speed_y
    6、Config.py

    把游戏的全局常量、变量单独写出来,感觉在调用时还是挺方便的。

    1. # Config.py
    2. import pygame
    3. class Constant:
    4. WIDTH = 1200
    5. HEIGHT = 720
    6. FPS = 60
    7. MAP_SCALE = 3
    8. PLAYER_SCALE = 2.5
    9. SPEED_X = 3
    10. SPEED_Y = -10
    11. GRAVITY = 0.5
    12. BULLET_SPEED = 8
    13. class Variable:
    14. all_sprites = pygame.sprite.Group()
    15. player_sprites = pygame.sprite.Group()
    16. enemy_sprites = pygame.sprite.Group()
    17. collider = pygame.sprite.Group()
    18. collider83 = pygame.sprite.Group()
    19. collider115 = pygame.sprite.Group()
    20. collider146 = pygame.sprite.Group()
    21. collider163 = pygame.sprite.Group()
    22. collider178 = pygame.sprite.Group()
    23. collider211 = pygame.sprite.Group()
    24. collider231 = pygame.sprite.Group()
    25. sprite_storage = pygame.sprite.Group()
    26. map_storage = pygame.sprite.Group()
    27. game_start = True
    28. map_switch = True
    29. player_switch = True
    30. enemy_counter = 0
    31. stage = 1
    32. step = 0

  • 相关阅读:
    网络编码中的椭圆曲线多重签名方案
    Prompt Playground 7月开发记录(2): Avalonia 应用开发
    Yarn工作机制流程
    多线程之ConcurrentHashMap原理
    Vue官方文档(48): vuex的基本使用
    【Nacos】Nacos集群模式启动报错&解决方案
    Visual Studio 2019 简单安装教程
    C---链表
    (面试题)面试官为啥总是让我们手撕call、apply、bind?
    DFT specification file & string
  • 原文地址:https://blog.csdn.net/2302_76282232/article/details/137676971