你用python写出代码来,用pycharm软件,解释器为3.7版本
时间: 2024-03-11 08:47:45 浏览: 12
好的,以下是用Python实现的乓球游戏代码,你可以在PyCharm软件上运行。注意,代码中的注释可以帮助你理解每个函数和变量的作用。
```python
import pygame
import sys
# 定义游戏屏幕尺寸
WIN_WIDTH = 800
WIN_HEIGHT = 600
# 定义乒乓球和球拍的尺寸
BALL_SIZE = 20
PAD_WIDTH = 100
PAD_HEIGHT = 20
# 定义球拍的移动速度
PAD_SPEED = 5
# 定义球拍与屏幕边缘之间的距离
PAD_MARGIN = 20
# 定义球拍颜色和乒乓球颜色
PAD_COLOR = (255, 255, 255)
BALL_COLOR = (255, 255, 255)
# 定义字体大小和颜色
FONT_SIZE = 36
FONT_COLOR = (255, 255, 255)
# 初始化pygame
pygame.init()
# 设置游戏屏幕
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
# 设置游戏标题
pygame.display.set_caption('Ping Pong Game')
# 创建字体
font = pygame.font.SysFont(None, FONT_SIZE)
def draw_text(text, x, y):
"""绘制文本"""
text_obj = font.render(text, True, FONT_COLOR)
text_rect = text_obj.get_rect()
text_rect.centerx = x
text_rect.centery = y
screen.blit(text_obj, text_rect)
def draw_ball(ball_x, ball_y):
"""绘制乒乓球"""
pygame.draw.circle(screen, BALL_COLOR, (ball_x, ball_y), BALL_SIZE // 2)
def draw_paddle(paddle_x, paddle_y):
"""绘制球拍"""
pygame.draw.rect(screen, PAD_COLOR, (paddle_x, paddle_y, PAD_WIDTH, PAD_HEIGHT))
def move_paddle(paddle_x, paddle_speed):
"""移动球拍"""
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
paddle_x -= paddle_speed
if keys[pygame.K_RIGHT]:
paddle_x += paddle_speed
# 限制球拍的移动范围
if paddle_x < PAD_MARGIN:
paddle_x = PAD_MARGIN
if paddle_x > WIN_WIDTH - PAD_MARGIN - PAD_WIDTH:
paddle_x = WIN_WIDTH - PAD_MARGIN - PAD_WIDTH
return paddle_x
def move_ball(ball_x, ball_y, ball_speed_x, ball_speed_y, paddle_x, paddle_y):
"""移动乒乓球"""
ball_x += ball_speed_x
ball_y += ball_speed_y
# 判断乒乓球是否碰到左右边缘
if ball_x <= BALL_SIZE // 2 or ball_x >= WIN_WIDTH - BALL_SIZE // 2:
ball_speed_x = -ball_speed_x
# 判断乒乓球是否碰到上边缘
if ball_y <= BALL_SIZE // 2:
ball_speed_y = -ball_speed_y
# 判断乒乓球是否碰到球拍
if ball_y >= paddle_y - BALL_SIZE // 2 and ball_x >= paddle_x - BALL_SIZE // 2 and ball_x <= paddle_x + PAD_WIDTH + BALL_SIZE // 2:
ball_speed_y = -ball_speed_y
# 判断乒乓球是否落到下边缘
if ball_y >= WIN_HEIGHT - BALL_SIZE // 2:
return -1, ball_speed_x, ball_speed_y
else:
return ball_x, ball_speed_x, ball_speed_y
def play_game():
"""游戏主循环"""
ball_x = WIN_WIDTH // 2
ball_y = WIN_HEIGHT // 2
ball_speed_x = 5
ball_speed_y = 5
paddle_x = WIN_WIDTH // 2 - PAD_WIDTH // 2
paddle_y = WIN_HEIGHT - PAD_MARGIN - PAD_HEIGHT
paddle_speed = PAD_SPEED
score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 按Esc键退出游戏
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
# 按enter键开始下一轮游戏
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
return
# 移动球拍
paddle_x = move_paddle(paddle_x, paddle_speed)
# 移动乒乓球
ball_x, ball_speed_x, ball_speed_y = move_ball(ball_x, ball_y, ball_speed_x, ball_speed_y, paddle_x, paddle_y)
if ball_x == -1:
score += 1
ball_x = WIN_WIDTH // 2
ball_y = WIN_HEIGHT // 2
ball_speed_x = 5
ball_speed_y = 5
# 绘制游戏界面
screen.fill((0, 0, 0))
draw_ball(ball_x, ball_y)
draw_paddle(paddle_x, paddle_y)
draw_text("Score: {}".format(score), WIN_WIDTH // 2, FONT_SIZE)
pygame.display.update()
# 游戏开始界面
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 按Esc键退出游戏
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
# 绘制游戏开始界面
screen.fill((0, 0, 0))
draw_text("Ping Pong Game", WIN_WIDTH // 2, WIN_HEIGHT // 2 - FONT_SIZE)
draw_text("Press '1' for single player mode", WIN_WIDTH // 2, WIN_HEIGHT // 2 + FONT_SIZE)
draw_text("Press '2' for double player mode", WIN_WIDTH // 2, WIN_HEIGHT // 2 + 2 * FONT_SIZE)
pygame.display.update()
# 选择游戏模式
keys = pygame.key.get_pressed()
if keys[pygame.K_1]:
play_game()
draw_text("You lose", WIN_WIDTH // 2, WIN_HEIGHT // 2 - FONT_SIZE)
draw_text("Press 'Enter' to play again", WIN_WIDTH // 2, WIN_HEIGHT // 2 + FONT_SIZE)
pygame.display.update()
# 按enter键重新开始游戏
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
break
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
break
elif keys[pygame.K_2]:
play_game()
draw_text("Player 2 wins", WIN_WIDTH // 2, WIN_HEIGHT // 2 - FONT_SIZE)
draw_text("Press 'Enter' to play again", WIN_WIDTH // 2, WIN_HEIGHT // 2 + FONT_SIZE)
pygame.display.update()
# 按enter键重新开始游戏
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
break
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
break
```