找回密码
 中文实名注册
查看: 72|回复: 0

乒乓球--向浩辰

[复制链接]

4

主题

4

帖子

313

积分

中级会员

Rank: 3Rank: 3

积分
313
发表于 2025-1-19 17:28:56 | 显示全部楼层 |阅读模式
[Python] 纯文本查看 复制代码
import pygame
import sys


# 初始化 pygame
pygame.init()


# 屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("乒乓球游戏")


# 颜色
white = (255, 255, 255)
black = (0, 0, 0)


# 球的初始属性
ball_radius = 20
ball_x = screen_width // 2
ball_y = screen_height // 2
ball_speed_x = 5
ball_speed_y = 5


# 玩家球拍的属性
player_paddle_width = 100
player_paddle_height = 20
player_paddle_x = (screen_width - player_paddle_width) // 2
player_paddle_y = screen_height - 50
player_paddle_speed = 10


# AI 球拍的属性
ai_paddle_width = 100
ai_paddle_height = 20
ai_paddle_x = (screen_width - ai_paddle_width) // 2
ai_paddle_y = 30
ai_paddle_speed = 7


# 分数
player_score = 0
ai_score = 0


# 字体
font = pygame.font.Font(None, 36)


# 绘制游戏元素
def draw_elements():
    # 绘制球
    pygame.draw.circle(screen, white, (ball_x, ball_y), ball_radius)
    # 绘制玩家球拍
    pygame.draw.rect(screen, white, (player_paddle_x, player_paddle_y, player_paddle_width, player_paddle_height))
    # 绘制 AI 球拍
    pygame.draw.rect(screen, white, (ai_paddle_x, ai_paddle_y, ai_paddle_width, ai_paddle_height))
    # 绘制玩家分数
    player_score_text = font.render(f"Player Score: {player_score}", True, white)
    screen.blit(player_score_text, (10, 10))
    # 绘制 AI 分数
    ai_score_text = font.render(f"AI Score: {ai_score}", True, white)
    screen.blit(ai_score_text, (screen_width - 150, 10))


# 主游戏循环
while True:
    screen.fill(black)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()


    # 处理玩家输入
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_paddle_x > 0:
        player_paddle_x -= player_paddle_speed
    if keys[pygame.K_RIGHT] and player_paddle_x < screen_width - player_paddle_width:
        player_paddle_x += player_paddle_speed


    # 简单的 AI 算法:AI 球拍跟随球的水平位置移动
    if ai_paddle_x + ai_paddle_width // 2 < ball_x:
        ai_paddle_x += ai_paddle_speed
    if ai_paddle_x + ai_paddle_width // 2 > ball_x:
        ai_paddle_x -= ai_paddle_speed


    # 确保 AI 球拍不会超出屏幕范围
    if ai_paddle_x < 0:
        ai_paddle_x = 0
    if ai_paddle_x > screen_width - ai_paddle_width:
        ai_paddle_x = screen_width - ai_paddle_width


    # 球的运动
    ball_x += ball_speed_x
    ball_y += ball_speed_y


    # 球的边界碰撞检测
    if ball_x - ball_radius <= 0 or ball_x + ball_radius >= screen_width:
        ball_speed_x = -ball_speed_x
    if ball_y - ball_radius <= 0 or ball_y + ball_radius >= screen_height:
        ball_speed_y = -ball_speed_y


    # 球与玩家球拍的碰撞检测
    if (
        ball_y + ball_radius >= player_paddle_y
        and ball_x >= player_paddle_x
        and ball_x <= player_paddle_x + player_paddle_width
    ):
        ball_speed_y = -ball_speed_y


    # 球与 AI 球拍的碰撞检测
    if (
        ball_y - ball_radius <= ai_paddle_y + ai_paddle_height
        and ball_x >= ai_paddle_x
        and ball_x <= ai_paddle_x + ai_paddle_width
    ):
        ball_speed_y = -ball_speed_y


    # 球出界检测
    if ball_y + ball_radius >= screen_height:
        # 球出玩家边界,AI 得分
        ai_score += 1
        # 重置球的位置
        ball_x = screen_width // 2
        ball_y = screen_height // 2
    if ball_y - ball_radius <= 0:
        # 球出 AI 边界,玩家得分
        player_score += 1
        # 重置球的位置
        ball_x = screen_width // 2
        ball_y = screen_height // 2


    # 绘制游戏元素
    draw_elements()


    # 检查获胜条件
    if player_score >= 100:
        win_text = font.render("Player Wins!", True, white)
        screen.blit(win_text, (screen_width // 2 - 100, screen_height // 2))
        pygame.display.flip()
        pygame.time.wait(3000)  # 显示 3 秒
        pygame.quit()
        sys.exit()
    elif ai_score >= 100:
        win_text = font.render("AI Wins!", True, white)
        screen.blit(win_text, (screen_width // 2 - 100, screen_height // 2))
        pygame.display.flip()
        pygame.time.wait(3000)  # 显示 3 秒
        pygame.quit()
        sys.exit()


    pygame.display.flip()
    pygame.time.Clock().tick(60)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2025-3-15 00:20 , Processed in 0.043056 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表