当前位置:   article > 正文

Python Pygame开发小游戏球球大战_while running: clock.tick(fps) for event in pygame

while running: clock.tick(fps) for event in pygame.event.get(): if event.typ

源代码:

  1. import pygame
  2. import random
  3. import sys
  4. pygame.init()
  5. W, H = 600, 500
  6. FPS = 100
  7. CLOCK = pygame.time.Clock()
  8. screen = pygame.display.set_mode((W, H))
  9. pygame.display.set_caption("BallWar")
  10. def draw_bg():
  11. for i in range(0, H, 25):
  12. pygame.draw.line(screen, (86, 86, 86, 0.2), (0, i), (W, i), 1)
  13. for i in range(0, W, 25):
  14. pygame.draw.line(screen, (86, 86, 86, 0.2), (i, 0), (i, H), 1)
  15. def draw_text(surf, text, size, postion=set(), color=(0, 0, 0), font=None, isAllCenter=False):
  16. font = pygame.font.SysFont(font, size)
  17. font_surface = font.render(text, True, color)
  18. text_rect = font_surface.get_rect()
  19. text_rect.left = postion[0]
  20. text_rect.top = postion[1]
  21. if isAllCenter:
  22. text_rect.center = postion
  23. surf.blit(font_surface, text_rect)
  24. def welcome():
  25. running = True
  26. while running:
  27. CLOCK.tick(FPS)
  28. for event in pygame.event.get():
  29. if event.type == pygame.QUIT:
  30. pygame.quit()
  31. sys.exit()
  32. if event.type == pygame.KEYDOWN:
  33. if event.key == pygame.K_SPACE:
  34. running = False
  35. screen.fill((255, 255, 255))
  36. draw_bg()
  37. draw_text(screen, "BallWarGame", 100, (W/2, 60), (10, 10, 10), isAllCenter=True)
  38. draw_text(screen, "made by HuangYiMing", 30, (W/2+60, 100), (50, 50, 50), isAllCenter=True)
  39. draw_text(screen, "----Space bar start----", 60, (W/2, 160), (0, 0, 0), isAllCenter=True)
  40. pygame.display.update()
  41. def main():
  42. def make_snack(num=1, type='snack', a_=()):
  43. for i in range(num):
  44. s = Snack()
  45. snack_list.add(s)
  46. if type != 'snack':
  47. for i in range(num):
  48. a = AI(a_)
  49. ai_list.add(a)
  50. class Player(pygame.sprite.Sprite):
  51. def __init__(self):
  52. pygame.sprite.Sprite.__init__(self)
  53. self.radius = 8
  54. self.image = pygame.Surface((self.radius, self.radius))
  55. self.rect = self.image.get_rect()
  56. self.moveSpeed = 2
  57. self.rect.center = (W/2, H/2)
  58. def update(self):
  59. pressed = pygame.key.get_pressed()
  60. if pressed[pygame.K_UP]:
  61. if self.rect.centery - self.radius > 0:
  62. self.rect.centery -= self.moveSpeed
  63. if pressed[pygame.K_DOWN]:
  64. if self.rect.centery + self.radius < H:
  65. self.rect.centery += self.moveSpeed
  66. if pressed[pygame.K_LEFT]:
  67. if self.rect.centerx - self.radius > 0:
  68. self.rect.centerx -= self.moveSpeed
  69. if pressed[pygame.K_RIGHT]:
  70. if self.rect.centerx + self.radius < W:
  71. self.rect.centerx += self.moveSpeed
  72. def draw(self):
  73. pygame.draw.circle(screen, (200, 0, 0), self.rect.center, self.radius)
  74. # draw_text(screen, str(self.radius).split(".")[0], int(self.radius)*4-15, self.rect.center, (200, 200, 100), isAllCenter=True)
  75. class Snack(pygame.sprite.Sprite):
  76. def __init__(self):
  77. pygame.sprite.Sprite.__init__(self)
  78. self.radius = random.randint(3, 5)
  79. self.image = pygame.Surface(((self.radius), (self.radius)))
  80. self.rect = self.image.get_rect()
  81. self.rect.center = (random.randint(int(self.radius), W-int(self.radius)), random.randint(int(self.radius), H-int(self.radius)))
  82. self.colors = (
  83. (91,165,133),
  84. (192,138,100),
  85. (134,71,95),
  86. (42,52,52)
  87. )
  88. self.color = random.choice(self.colors)
  89. def draw(self):
  90. pygame.draw.circle(screen, self.color, self.rect.center, (self.radius))
  91. class AI(pygame.sprite.Sprite):
  92. def __init__(self, center=()):
  93. pygame.sprite.Sprite.__init__(self)
  94. self.radius = random.randint(8, 18)
  95. self.image = pygame.Surface((self.radius, self.radius))
  96. self.rect = self.image.get_rect()
  97. self.moveSpeed = 2.1
  98. if center == ():
  99. self.rect.center = (random.randint(0, W), random.randint(0, H))
  100. else:
  101. self.rect.top = center[1]
  102. self.rect.centerx = center[0]
  103. self.direction = 0
  104. self.rand_direction()
  105. self.step = random.randint(3, 50)
  106. def rand_direction(self):
  107. self.direction = random.randint(1, 4+1)
  108. if self.direction == 1:
  109. self.direction = 'up'
  110. if self.direction == 2:
  111. self.direction = 'right'
  112. if self.direction == 3:
  113. self.direction = 'down'
  114. if self.direction == 4:
  115. self.direction = 'left'
  116. def update(self, onlyDraw=False):
  117. if onlyDraw is False:
  118. if self.direction == 'right':
  119. self.rect.x += self.moveSpeed
  120. if self.direction == 'left':
  121. self.rect.x -= self.moveSpeed
  122. if self.direction == 'up':
  123. self.rect.y -= self.moveSpeed
  124. if self.direction == 'down':
  125. self.rect.y += self.moveSpeed
  126. self.step -= 1
  127. if self.step <= 0:
  128. self.step = random.randint(3, 50)
  129. self.rand_direction()
  130. if self.rect.centery + self.radius > H:
  131. self.direction = 'up'
  132. if self.rect.centery - self.radius < 0:
  133. self.direction = 'down'
  134. if self.rect.centerx - self.radius < 0:
  135. self.direction = 'right'
  136. if self.rect.centerx + self.radius > W:
  137. self.direction = 'left'
  138. self.draw()
  139. def draw(self):
  140. pygame.draw.circle(screen, (0, 0, 180), self.rect.center, self.radius)
  141. draw_text(screen, str(self.radius).split(".")[0], int(self.radius)*4-15, self.rect.center, (200, 200, 100), isAllCenter=True)
  142. player = Player()
  143. snack_list = pygame.sprite.Group()
  144. ai_list = pygame.sprite.Group()
  145. running = True
  146. for i in range(0, 3+1):
  147. if i % 2 == 0:
  148. make_snack(3, 'ai')
  149. continue
  150. make_snack(5)
  151. start = pygame.time.get_ticks()
  152. while running:
  153. CLOCK.tick(FPS)
  154. now = pygame.time.get_ticks()
  155. if now - start >= 1000:
  156. start = now
  157. make_snack(random.randint(1, 5))
  158. if len(ai_list.sprites()) <= 12:
  159. if random.randint(1, 100) >= 90:
  160. make_snack(random.randint(0, 2), 'ai', (random.randint(0, W), 0))
  161. for event in pygame.event.get():
  162. if event.type == pygame.QUIT:
  163. pygame.quit()
  164. sys.exit()
  165. screen.fill((255, 255, 255))
  166. draw_bg()
  167. for sprite in snack_list.sprites():
  168. sprite.draw()
  169. player.update()
  170. player.draw()
  171. ai_list.update()
  172. hit = pygame.sprite.groupcollide(ai_list, snack_list, False, True, pygame.sprite.collide_circle)
  173. for i in hit:
  174. i.radius += 0.7
  175. hit = pygame.sprite.spritecollide(player, snack_list, [0, 1], pygame.sprite.collide_circle)
  176. if hit:
  177. player.radius += 0.3
  178. hit = pygame.sprite.spritecollide(player, ai_list, False, pygame.sprite.collide_circle)
  179. for i in hit:
  180. if i.radius >= player.radius:
  181. i.radius += player.radius//5
  182. player.kill()
  183. running = False
  184. if player.radius > i.radius:
  185. player.radius += i.radius//10
  186. i.kill()
  187. draw_text(screen, 'Size:'+str(int(player.radius)), 30, (W/2-200, 0), (200, 100, 80))
  188. pygame.display.update()
  189. return {"score": str(int(player.radius)), "player": player, "ai": ai_list}
  190. def over():
  191. running = True
  192. rect = pygame.Rect(W/2-250, 10, 500, 180)
  193. while running:
  194. CLOCK.tick(FPS)
  195. for event in pygame.event.get():
  196. if event.type == pygame.QUIT:
  197. pygame.quit()
  198. sys.exit()
  199. if event.type == pygame.KEYDOWN:
  200. if event.key == pygame.K_b:
  201. running = False
  202. screen.fill((255, 255, 255))
  203. draw_bg()
  204. datas['player'].draw()
  205. datas['ai'].update(onlyDraw=True)
  206. pygame.draw.rect(screen, (0, 0, 0), rect)
  207. draw_text(screen, 'Your size is '+datas['score'], 50, (W/2, 60), (200, 200, 0), isAllCenter=True)
  208. draw_text(screen, '-----B key returns-----', 40, (W/2, 100), (100, 100, 100), isAllCenter=True)
  209. pygame.display.update()
  210. while True:
  211. welcome()
  212. datas = main()
  213. over()

做的有些简陋,不需要素材,直接就可以运行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/984587
推荐阅读
相关标签
  

闽ICP备14008679号