python-小游戏-弹球对决
需要安装pygame
代码—game-Pong.py
import pygame
import random# Initialize pygame
pygame.init()# Set up the screen
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 20
PAD_WIDTH = 10
PAD_HEIGHT = 80
WHITE = (255, 255, 255)
PURPLE = (128, 0, 128)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
LIME = (0, 128, 0)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")# Set up the clock
clock = pygame.time.Clock()# Initialize variables
LEFT = False
RIGHT = True
ball_pos = [0, 0]
ball_vel = [0, 0]
paddle1_pos = HEIGHT // 2 - PAD_HEIGHT // 2
paddle2_pos = HEIGHT // 2 - PAD_HEIGHT // 2
paddle1_vel = 0
paddle2_vel = 0
score1 = 0
score2 = 0# Function to spawn the ball
def spawn_ball(direction):global ball_pos, ball_velball_pos = [WIDTH // 2, HEIGHT // 2]vx = random.randrange(120, 240) / 60vy = -random.randrange(60, 180) / 60if direction == LEFT:vx = -vxball_vel = [vx, vy]# Function to handle events
def handle_events():global paddle1_vel, paddle2_velfor event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()quit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_w:paddle1_vel -= 4elif event.key == pygame.K_s:paddle1_vel += 4elif event.key == pygame.K_UP:paddle2_vel -= 4elif event.key == pygame.K_DOWN:paddle2_vel += 4elif event.type == pygame.KEYUP:if event.key == pygame.K_w or event.key == pygame.K_s:paddle1_vel = 0elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:paddle2_vel = 0# Function to update game state
def update():global paddle1_pos, paddle2_pos, ball_pos, ball_vel, score1, score2, paddle1_vel, paddle2_velball_pos[0] += ball_vel[0]ball_pos[1] += ball_vel[1]if ball_pos[1] - BALL_RADIUS <= 0 or ball_pos[1] + BALL_RADIUS >= HEIGHT:ball_vel[1] = -ball_vel[1]if ball_pos[0] - BALL_RADIUS <= PAD_WIDTH:if paddle1_pos <= ball_pos[1] <= paddle1_pos + PAD_HEIGHT:ball_vel[0] = -ball_vel[0]ball_vel[0] += ball_vel[0] / 10ball_vel[1] += ball_vel[1] / 10else:score2 += 1spawn_ball(True)elif ball_pos[0] + BALL_RADIUS >= WIDTH - PAD_WIDTH:if paddle2_pos <= ball_pos[1] <= paddle2_pos + PAD_HEIGHT:ball_vel[0] = -ball_vel[0]ball_vel[0] += ball_vel[0] / 10ball_vel[1] += ball_vel[1] / 10else:score1 += 1spawn_ball(False)if not (0 <= paddle1_pos + paddle1_vel <= HEIGHT - PAD_HEIGHT):paddle1_vel = 0if not (0 <= paddle2_pos + paddle2_vel <= HEIGHT - PAD_HEIGHT):paddle2_vel = 0paddle1_pos += paddle1_velpaddle2_pos += paddle2_vel# Function to draw everything
def draw():screen.fill((0, 0, 0))pygame.draw.line(screen, PURPLE, [WIDTH // 2, 0], [WIDTH // 2, HEIGHT], 3)pygame.draw.line(screen, RED, [PAD_WIDTH, 0], [PAD_WIDTH, HEIGHT], 1)pygame.draw.line(screen, GREEN, [WIDTH - PAD_WIDTH, 0], [WIDTH - PAD_WIDTH, HEIGHT], 1)pygame.draw.circle(screen, PURPLE, [WIDTH // 2, HEIGHT // 2], 85, 4)pygame.draw.circle(screen, WHITE, ball_pos, BALL_RADIUS, 13)pygame.draw.circle(screen, WHITE, ball_pos, BALL_RADIUS, 7)pygame.draw.rect(screen, WHITE, [0, paddle1_pos, PAD_WIDTH, PAD_HEIGHT])pygame.draw.rect(screen, WHITE, [WIDTH - PAD_WIDTH, paddle2_pos, PAD_WIDTH, PAD_HEIGHT])font = pygame.font.Font(None, 55)score1_text = font.render(str(score1), True, RED)score2_text = font.render(str(score2), True, LIME)screen.blit(score1_text, [180, 50])screen.blit(score2_text, [420, 50])def main():global ball_pos, ball_vel, paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel, score1, score2# 初始化球和球拍的位置、速度等变量paddle1_vel = 0paddle2_vel = 0spawn_ball(bool(random.randrange(0, 2))) # 生成球while True:handle_events() # 处理事件update() # 更新游戏状态draw() # 绘制游戏画面pygame.display.update() # 更新显示clock.tick(60) # 控制帧率# Run the game
if __name__ == "__main__":main()
运行代码