python小游戏——乒乓球大战

简介: python小游戏——乒乓球大战

1.调用外部库

pygame

2.代码实现

1. import pygame
2. import random
3. import sys
4. 
5. # Initialize Pygame
6. pygame.init()
7. 
8. # Set up the window
9. WINDOW_WIDTH = 800
10. WINDOW_HEIGHT = 600
11. WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
12. pygame.display.set_caption("Ping Pong Game")
13. 
14. # Set up the colors
15. BLACK = (0, 0, 0)
16. WHITE = (255, 255, 255)
17. 
18. # Set up the fonts
19. FONT = pygame.font.SysFont(None, 48)
20. 
21. # Set up the ball
22. BALL_RADIUS = 10
23. ball_x = WINDOW_WIDTH // 2
24. ball_y = WINDOW_HEIGHT // 2
25. ball_dx = 5
26. ball_dy = 5
27. 
28. # Set up the player's paddle
29. PLAYER_PADDLE_WIDTH = 10
30. PLAYER_PADDLE_HEIGHT = 100
31. player_paddle_x = WINDOW_WIDTH - PLAYER_PADDLE_WIDTH - 10
32. player_paddle_y = WINDOW_HEIGHT // 2 - PLAYER_PADDLE_HEIGHT // 2
33. player_paddle_dy = 0
34. 
35. # Set up the computer's paddle
36. COMPUTER_PADDLE_WIDTH = 10
37. COMPUTER_PADDLE_HEIGHT = 100
38. computer_paddle_x = 10
39. computer_paddle_y = WINDOW_HEIGHT // 2 - COMPUTER_PADDLE_HEIGHT // 2
40. computer_paddle_dy = 5
41. 
42. # Set up the scores
43. player_score = 0
44. computer_score = 0
45. player_str = ""
46. computer_str = ""
47. 
48. # Set up the game state
49. game_state = "start"
50. space_state = "0"
51. 
52. # Set up the clock
53. clock = pygame.time.Clock()
54. 
55. # Set up the main game loop
56. while True:
57. # Handle events
58. for event in pygame.event.get():
59. if event.type == pygame.QUIT:
60.             pygame.quit()
61.             sys.exit()
62. elif event.type == pygame.KEYDOWN:
63. if event.key == pygame.K_UP:
64.                 player_paddle_dy = -5
65. elif event.key == pygame.K_DOWN:
66.                 player_paddle_dy = 5
67. elif event.key == pygame.K_SPACE:
68. if space_state == "0":
69.                     space_state = "1"
70. #print(f"DOWN, space_state: {space_state}, {game_state}, {computer_score}", flush=True)
71. elif event.type == pygame.KEYUP:
72. if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
73.                 player_paddle_dy = 0
74. elif event.key == pygame.K_SPACE:
75. if space_state != "1": continue
76. #print(f"  UP, space_state: {space_state}, {game_state}, {computer_score}", flush=True)
77.                 space_state = "0"
78. if game_state == "start":
79.                     game_state = "playing"
80.                     ball_dx = -ball_dx
81.                     ball_dy = -ball_dy
82. #ball_dx = random.choice([-5, 5])
83. #ball_dy = random.choice([-5, 5])
84. #elif event.type == pygame.MOUSEBUTTONDOWN:
85. #elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
86. #    if game_state == "start":
87. #        game_state = "playing"
88. #        ball_dx = random.choice([-5, 5])
89. #        ball_dy = random.choice([-5, 5])
90. 
91. # Update the game state
92. if game_state == "playing":
93. # Move the ball
94.         ball_x += ball_dx
95.         ball_y += ball_dy
96. 
97. # Bounce the ball off the top or bottom of the window
98. if ball_y - BALL_RADIUS <= 0 or ball_y + BALL_RADIUS >= WINDOW_HEIGHT:
99.             ball_dy = -ball_dy
100. 
101. # Bounce the ball off the player's paddle
102. if ball_x + BALL_RADIUS >= player_paddle_x and ball_y >= player_paddle_y and ball_y <= player_paddle_y + PLAYER_PADDLE_HEIGHT:
103.             ball_dx = -ball_dx
104. 
105. # Bounce the ball off the computer's paddle
106. if ball_x - BALL_RADIUS <= computer_paddle_x + COMPUTER_PADDLE_WIDTH and ball_y >= computer_paddle_y and ball_y <= computer_paddle_y + COMPUTER_PADDLE_HEIGHT:
107.             ball_dx = -ball_dx
108. 
109. # Check if the ball went out of bounds
110. if ball_x - BALL_RADIUS <= 0:
111. #computer_score += 1
112.             player_score += 1
113.             game_state = "start"
114. elif ball_x + BALL_RADIUS >= WINDOW_WIDTH:
115. #player_score += 1
116.             computer_score += 1
117.             game_state = "start"
118. # 判断游戏是否结束
119. if computer_score >= 5 or player_score >= 5:
120.             game_state = "game over"
121. 
122. # Move the player's paddle
123.         player_paddle_y += player_paddle_dy
124. if player_paddle_y <= 0:
125.             player_paddle_y = 0
126. elif player_paddle_y + PLAYER_PADDLE_HEIGHT >= WINDOW_HEIGHT:
127.             player_paddle_y = WINDOW_HEIGHT - PLAYER_PADDLE_HEIGHT
128. 
129. # Move the computer's paddle
130. if ball_y < computer_paddle_y + COMPUTER_PADDLE_HEIGHT // 2:
131.             computer_paddle_dy = -5
132. elif ball_y > computer_paddle_y + COMPUTER_PADDLE_HEIGHT // 2:
133.             computer_paddle_dy = 5
134. else:
135.             computer_paddle_dy = 0
136.         computer_paddle_y += computer_paddle_dy
137. if computer_paddle_y <= 0:
138.             computer_paddle_y = 0
139. elif computer_paddle_y + COMPUTER_PADDLE_HEIGHT >= WINDOW_HEIGHT:
140.             computer_paddle_y = WINDOW_HEIGHT - COMPUTER_PADDLE_HEIGHT
141. 
142. # Draw the game
143.     WINDOW.fill(BLACK)
144. if game_state == "start":
145. #start_text = FONT.render("Press the mouse button to start", True, WHITE)
146.         start_text = FONT.render("Press space key to start", True, WHITE)
147.         start_rect = start_text.get_rect()
148.         start_rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
149.         WINDOW.blit(start_text, start_rect)
150. elif game_state == "playing":
151. # Draw the ball
152.         pygame.draw.circle(WINDOW, WHITE, (ball_x, ball_y), BALL_RADIUS)
153. 
154. # Draw the player's paddle
155.         pygame.draw.rect(WINDOW, WHITE, (player_paddle_x, player_paddle_y, PLAYER_PADDLE_WIDTH, PLAYER_PADDLE_HEIGHT))
156. 
157. # Draw the computer's paddle
158.         pygame.draw.rect(WINDOW, WHITE, (computer_paddle_x, computer_paddle_y, COMPUTER_PADDLE_WIDTH, COMPUTER_PADDLE_HEIGHT))
159. 
160. # Draw the scores
161.         player_score_text = FONT.render(str(player_score), True, WHITE)
162.         player_score_rect = player_score_text.get_rect()
163.         player_score_rect.center = (WINDOW_WIDTH - 50, 50)
164.         WINDOW.blit(player_score_text, player_score_rect)
165. 
166.         computer_score_text = FONT.render(str(computer_score), True, WHITE)
167.         computer_score_rect = computer_score_text.get_rect()
168.         computer_score_rect.center = (50, 50)
169.         WINDOW.blit(computer_score_text, computer_score_rect)
170. 
171. elif game_state == "game over":
172. # Draw the game over text
173.         game_over_text = FONT.render("Game Over", True, WHITE)
174.         game_over_rect = game_over_text.get_rect()
175.         game_over_rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 - 50)
176.         WINDOW.blit(game_over_text, game_over_rect)
177. 
178. # Draw the final scores
179. #print(player_score, computer_score, flush=True)
180. if not player_str:
181.             player_str = f"Player Score: {player_score}"
182.             computer_str = f"Computer Score: {computer_score}"
183. 
184.         player_final_score_text = FONT.render(player_str, True, WHITE)
185.         player_final_score_rect = player_final_score_text.get_rect()
186.         player_final_score_rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
187.         WINDOW.blit(player_final_score_text, player_final_score_rect)
188. 
189.         computer_final_score_text = FONT.render(computer_str, True, WHITE)
190.         computer_final_score_rect = computer_final_score_text.get_rect()
191.         computer_final_score_rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 50)
192.         WINDOW.blit(computer_final_score_text, computer_final_score_rect)
193. 
194. # Reset the scores and game state
195.         player_score = 0
196.         computer_score = 0
197.         game_state = "game over"
198. 
199. # Update the window
200.     pygame.display.update()
201. 
202. # Tick the clock
203.     clock.tick(60)
204. 
205. pygame.quit()

3.游戏实测

- 游戏包含两个参与者,一方是电脑,一方是玩家;电脑在界面左侧,玩家在界面右侧。

- 游戏界面上包含一个开始按钮,点击开始按钮后,游戏才能开始。

- 乒乓球接触球拍和界面上下边界时会反弹。

- 玩家通过Up,Down键来控制球拍的上下运动,玩家按住对应的键,球拍会保持一个方向运动。

- 电脑通过自动判断乒乓球的位置来进行移动,尽可能的将乒乓球反弹。

- 一方的球拍没有接到球的时候,对方得1分,先得够5分的一方获胜,同时游戏结束。

- 游戏结束后显示获胜的一方的角色:电脑或者玩家。

相关文章
|
3月前
|
Python
【Python】Python黄金矿工小游戏开发设计(源码)【独一无二】
【Python】Python黄金矿工小游戏开发设计(源码)【独一无二】
|
4月前
|
UED Python
python编写小游戏
【7月更文挑战第6天】
51 1
|
5月前
|
开发者 Python
入门实战丨Python小游戏经典案例
入门实战丨Python小游戏经典案例
83 4
|
6月前
|
程序员 C# Python
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
|
6月前
|
Shell Python
python|闲谈2048小游戏和数组的旋转及翻转和转置
python|闲谈2048小游戏和数组的旋转及翻转和转置
67 1
|
6月前
|
Python
10个python入门小游戏,零基础打通关,就能掌握编程基础_python编写的入门简单小游戏
10个python入门小游戏,零基础打通关,就能掌握编程基础_python编写的入门简单小游戏
|
6月前
|
存储 程序员 C#
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
|
2天前
|
存储 Python
Python编程入门:打造你的第一个程序
【10月更文挑战第39天】在数字时代的浪潮中,掌握编程技能如同掌握了一门新时代的语言。本文将引导你步入Python编程的奇妙世界,从零基础出发,一步步构建你的第一个程序。我们将探索编程的基本概念,通过简单示例理解变量、数据类型和控制结构,最终实现一个简单的猜数字游戏。这不仅是一段代码的旅程,更是逻辑思维和问题解决能力的锻炼之旅。准备好了吗?让我们开始吧!
|
2天前
|
机器学习/深度学习 人工智能 TensorFlow
人工智能浪潮下的自我修养:从Python编程入门到深度学习实践
【10月更文挑战第39天】本文旨在为初学者提供一条清晰的道路,从Python基础语法的掌握到深度学习领域的探索。我们将通过简明扼要的语言和实际代码示例,引导读者逐步构建起对人工智能技术的理解和应用能力。文章不仅涵盖Python编程的基础,还将深入探讨深度学习的核心概念、工具和实战技巧,帮助读者在AI的浪潮中找到自己的位置。
|
4天前
|
设计模式 算法 搜索推荐
Python编程中的设计模式:优雅解决复杂问题的钥匙####
本文将探讨Python编程中几种核心设计模式的应用实例与优势,不涉及具体代码示例,而是聚焦于每种模式背后的设计理念、适用场景及其如何促进代码的可维护性和扩展性。通过理解这些设计模式,开发者可以更加高效地构建软件系统,实现代码复用,提升项目质量。 ####