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分的一方获胜,同时游戏结束。

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

相关文章
|
4月前
|
Python
【Python】Python黄金矿工小游戏开发设计(源码)【独一无二】
【Python】Python黄金矿工小游戏开发设计(源码)【独一无二】
135 1
|
5月前
|
UED Python
python编写小游戏
【7月更文挑战第6天】
54 1
|
6月前
|
开发者 Python
入门实战丨Python小游戏经典案例
入门实战丨Python小游戏经典案例
88 4
|
7月前
|
程序员 C# Python
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
|
7月前
|
Shell Python
python|闲谈2048小游戏和数组的旋转及翻转和转置
python|闲谈2048小游戏和数组的旋转及翻转和转置
78 1
|
7月前
|
Python
10个python入门小游戏,零基础打通关,就能掌握编程基础_python编写的入门简单小游戏
10个python入门小游戏,零基础打通关,就能掌握编程基础_python编写的入门简单小游戏
|
7月前
|
存储 程序员 C#
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
|
16天前
|
人工智能 数据可视化 数据挖掘
探索Python编程:从基础到高级
在这篇文章中,我们将一起深入探索Python编程的世界。无论你是初学者还是有经验的程序员,都可以从中获得新的知识和技能。我们将从Python的基础语法开始,然后逐步过渡到更复杂的主题,如面向对象编程、异常处理和模块使用。最后,我们将通过一些实际的代码示例,来展示如何应用这些知识解决实际问题。让我们一起开启Python编程的旅程吧!
|
15天前
|
存储 数据采集 人工智能
Python编程入门:从零基础到实战应用
本文是一篇面向初学者的Python编程教程,旨在帮助读者从零开始学习Python编程语言。文章首先介绍了Python的基本概念和特点,然后通过一个简单的例子展示了如何编写Python代码。接下来,文章详细介绍了Python的数据类型、变量、运算符、控制结构、函数等基本语法知识。最后,文章通过一个实战项目——制作一个简单的计算器程序,帮助读者巩固所学知识并提高编程技能。
|
3天前
|
Unix Linux 程序员
[oeasy]python053_学编程为什么从hello_world_开始
视频介绍了“Hello World”程序的由来及其在编程中的重要性。从贝尔实验室诞生的Unix系统和C语言说起,讲述了“Hello World”作为经典示例的起源和流传过程。文章还探讨了C语言对其他编程语言的影响,以及它在系统编程中的地位。最后总结了“Hello World”、print、小括号和双引号等编程概念的来源。
97 80
下一篇
DataWorks