Pygame python 中的乒乓球游戏

输入

import pygameimport sys

登录后复制

pygame 是我们用来制作游戏的模块。它为我们提供了图形、声音等工具。

sys 是 python 中的一个模块,可以帮助我们与 python 解释器交互。

初始化

pygame.init()

登录后复制

初始化所有 pygame 模块并使其可供使用。

常数

#dimensionswidth, height=800,600#frame ratefps=60#the paddles at the side of ping pongpaddle_width, paddle_height=15,90#the balls radiusball_radius=15#the color of the ball and paddlewhite=(255, 255, 255)

登录后复制宽度和高度:游戏窗口的尺寸。 800px 为宽度,600px 为高度fps:每秒帧数,控制游戏的速度和流畅度。paddle_width、paddle_height:桨叶的尺寸。ball_radius:球的半径。white:白色的 rgb 值,用于球拍、球和文本。

制作屏幕

screen=pygame.display.set_mode((width,height))pygame.display.set_caption("ping pong")

登录后复制

您将有一个名为ping pong 的窗口,并指定了宽度和高度

Pygame python 中的乒乓球游戏

立即学习“Python免费学习笔记(深入)”;

桨和球设置

left_paddle=pygame.rect(50, height//2 - paddle_height //2, paddle_width, paddle_height)right_paddle=pygame.rect(width - 50 - paddle_width, height //2- paddle_height //2, paddle_width, paddle_height)ball=pygame.rect(width //2 - ball_radius, height //2 - ball_radius, ball_raduis *2, ball_radius *2)

登录后复制

representing (0,0)

在 pygame 中,屏幕的左上角代表坐标 (0,0)。

pygame.rect:用于在 pygame 中创建矩形(此处用于桨和球)。

pygame.rect(x, y, width, height)

登录后复制left_paddle:位于屏幕左侧附近,垂直居中

pygame.rect(50, height//2 - paddle_height //2, paddle_width, paddle_height)

登录后复制

首先,我们将左桨定位在距左侧 50px 处。

然后我们执行 height//2 – paddle_height //2 因为如果你只执行 height//2 它看起来就像图片中的样子。它从屏幕上下来。为了使其居中,我们这样做 – paddle_height //2

Pygame python 中的乒乓球游戏

这就是我们为右桨使其居中所做的事情。

right_paddle:位于屏幕右侧附近,垂直居中。

right_paddle=pygame.rect(width - 50 - paddle_width, height //2- paddle_height //2, paddle_width, paddle_height)

登录后复制球:最初位于屏幕中央。

ball=pygame.rect(width //2 - ball_radius, height //2 - ball_radius, ball_raduis *2, ball_radius *2)

登录后复制

为了使球居中,我们减去半径。

速度

ball_speed_x=7ball_speed_y=7paddle_speed=10

登录后复制

ball_speed_x 和 ball_speed_y 控制球的水平和垂直速度。

paddle_speed:控制桨的移动速度。

分数变量

left_score=0right_score=0font=pygame.font.sysfont(none,55)

登录后复制left_score 和 right_score:跟踪玩家的分数。字体:用于在屏幕上渲染乐谱文本。 none 使用默认字体,55为字体大小。

绘制所有内容的函数

def draw():   screen.fill((0,0,0)) #fill the screen with black   pygame.draw.rect(screen, white, left_paddle)   pygame.draw.rect(screen, white, right_paddle)   pygame.draw.ellipse(screen, white, ball)

登录后复制fill((0, 0, 0)):用黑色填充屏幕(rgb:0, 0, 0)。pygame.draw.rect:绘制矩形桨。pygame.draw.ellipse:将球绘制为圆形(以矩形球为边界)。

画出中心线

   pygame.draw.aaline(screen, white, (width //2, 0), (width //2, height))

登录后复制画一条垂直中心线来划分比赛场地。

抽签分数

   left_text=font.render(str(left_score),true, white)   screen.blit(left_text, (width // 4 - left_text.get_width() // 2, 20))   right_text=font.render(str(right_score), true, white)   screen.blit(right_text, (width * 3 // 4 - right_text.get_width() //2, 20))

登录后复制

渲染双方玩家的分数并将其放置在屏幕上。

更新屏幕

   pygame.display.flip()

登录后复制

使用最新更改更新显示。

#main game loopwhile true:

登录后复制

让游戏无限期地运行。

for event in pygame.event.get():   if event.type == pygame.quitt:      pygame.quit()      sys.exit()

登录后复制

这将遍历 pygame 中可能发生的所有事件,如果其中一个事件正在关闭窗口,则退出 pygame 并关闭窗口。

桨控制

#paddle controlskeys pygame.key.get_pressed()if keys [pygame.k_w] and left_paddle.top > 0:    left_paddle.y-=paddle_speedif keys [pygame.k_s] and left_paddle.bottom  0:    right_paddle.y -= paddle_speedif keys [pygame.k_down] and right_paddle.bottom < height:    right_paddle.y += paddle_speed 66

登录后复制

检测按键:

ws:上下移动左桨。pygame.k_wwpygame.k_ss向上向下:上下移动右桨。pygame.k_up 是向上键pygame.k_down 是向下键包括防止桨移出屏幕的检查。left_paddle.top > 0检查桨顶部坐标是否大于 0。检查当您单击 w 时它是否击中屏幕顶部。left_paddle.bottom right_paddle.top > 0检查桨顶部坐标是否大于 0。检查当您单击向上键时它是否击中屏幕顶部。right_paddle.bottom

球运动

   ball.x += ball_speed_x   ball.y + ball_speed_y

登录后复制

通过将球的速度添加到当前位置来移动球

球与顶壁和底壁碰撞

if ball.top = height:   ball_speed_y=-ball_speed_y

登录后复制

如果球击中屏幕顶部或底部,则反转球的垂直方向

球与桨的碰撞

if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):    ball_speed_x = -ball_speed_x

登录后复制

如果球与球拍碰撞,则反转球的水平方向。

评分

9955464714​​38如果球出界,则更新得分。将球重置到中心并反转其方向。

定时

pygame.time.clock().tick (fps)

登录后复制

限制游戏运行速度最高为60帧/秒,确保游戏流畅。

完整代码

import pygameimport syspygame.init()# ConstantsWIDTH, HEIGHT = 800, 600FPS = 60PADDLE_WIDTH, PADDLE_HEIGHT = 15, 90BALL_RADIUS = 15WHITE = (255, 255, 255)# Setup screenscreen = pygame.display.set_mode((WIDTH, HEIGHT))pygame.display.set_caption("Pong")# Paddles and ball setupleft_paddle = pygame.Rect(50, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)right_paddle = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)# Speedsball_speed_x = 7ball_speed_y = 7paddle_speed = 10# Score variablesleft_score = 0right_score = 0font = pygame.font.SysFont(None, 55)# Function to draw everythingdef draw():        screen.fill((0, 0, 0))  # Fill screen with black        pygame.draw.rect(screen, WHITE, left_paddle)        pygame.draw.rect(screen, WHITE, right_paddle)        pygame.draw.ellipse(screen, WHITE, ball)        # Draw the center line        pygame.draw.aaline(screen, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT))        # Draw scores        left_text = font.render(str(left_score), True, WHITE)        screen.blit(left_text, (WIDTH // 4 - left_text.get_width() // 2, 20))        right_text = font.render(str(right_score), True, WHITE)        screen.blit(right_text, (WIDTH * 3 // 4 - right_text.get_width() // 2, 20))        pygame.display.flip()# Main game loopwhile True:        for event in pygame.event.get():                if event.type == pygame.QUIT:                        pygame.quit()                        sys.exit()        # Paddle controls        keys = pygame.key.get_pressed()        if keys[pygame.K_w] and left_paddle.top > 0:                left_paddle.y -= paddle_speed        if keys[pygame.K_s] and left_paddle.bottom  0:                right_paddle.y -= paddle_speed        if keys[pygame.K_DOWN] and right_paddle.bottom < HEIGHT:                right_paddle.y += paddle_speed        # Ball movement        ball.x += ball_speed_x        ball.y += ball_speed_y        # Ball collision with top and bottom walls        if ball.top = HEIGHT:                ball_speed_y = -ball_speed_y        # Ball collision with paddles        if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):                ball_speed_x = -ball_speed_x        # Scoring        if ball.left = WIDTH:                left_score += 1                ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)                ball_speed_x = -ball_speed_x        draw()        pygame.time.Clock().tick(FPS)

登录后复制

Pygame python 中的乒乓球游戏

以上就是Pygame python 中的乒乓球游戏的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2177250.html

(0)
上一篇 2025年2月25日 13:12:23
下一篇 2025年2月22日 14:59:14

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论