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)
制作屏幕
screen=pygame.display.set_mode((width,height))pygame.display.set_caption("ping pong")
您将有一个名为ping pong 的窗口,并指定了宽度和高度
立即学习“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)
在 pygame 中,屏幕的左上角代表坐标 (0,0)。
pygame.rect(x, y, width, height)
pygame.rect(50, height//2 - paddle_height //2, paddle_width, paddle_height)
首先,我们将左桨定位在距左侧 50px 处。
然后我们执行 height//2 - paddle_height //2 因为如果你只执行 height//2 它看起来就像图片中的样子。它从屏幕上下来。为了使其居中,我们这样做 - paddle_height //2
这就是我们为右桨使其居中所做的事情。
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)
绘制所有内容的函数
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)
画出中心线
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 < height: left_paddle.y += paddle_speed if keys [pygame.k_up] and right_paddle.top > 0: right_paddle.y -= paddle_speedif keys [pygame.k_down] and right_paddle.bottom < height: right_paddle.y += paddle_speed 66
检测按键:
球运动
ball.x += ball_speed_x ball.y + ball_speed_y
通过将球的速度添加到当前位置来移动球
球与顶壁和底壁碰撞
if ball.top <= 0 or ball.bottom >= height: ball_speed_y=-ball_speed_y
如果球击中屏幕顶部或底部,则反转球的垂直方向
球与桨的碰撞
if ball.colliderect(left_paddle) or ball.colliderect(right_paddle): ball_speed_x = -ball_speed_x
如果球与球拍碰撞,则反转球的水平方向。
评分
995546471438定时
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 < HEIGHT: left_paddle.y += paddle_speed if keys[pygame.K_UP] and right_paddle.top > 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 <= 0 or ball.bottom >= 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 <= 0: right_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 if ball.right >= 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)