python制作弹球小游戏

以下是一个使用 Python 和 Pygame 库实现的简单弹跳小球的示例代码:

import pygame
import sys

# 初始化 Pygame
pygame.init()

# 设置窗口尺寸
WIDTH, HEIGHT = 800, 600
WINDOW_SIZE = (WIDTH, HEIGHT)
WINDOW_TITLE = "弹跳小球"
FPS = 60

# 设置颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 设置小球属性
BALL_RADIUS = 20
BALL_COLOR = RED
BALL_SPEED_Y = 5
GRAVITY = 0.2

# 创建窗口
window = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption(WINDOW_TITLE)
clock = pygame.time.Clock()

# 设置小球初始位置和速度
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
ball_speed_x = 0
ball_speed_y = 0

# 游戏主循环
while True:
    window.fill(BLACK)  # 填充窗口为黑色

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # 更新小球位置
    ball_x += ball_speed_x
    ball_y += ball_speed_y

    # 检查小球是否触底
    if ball_y + BALL_RADIUS >= HEIGHT:
        ball_y = HEIGHT - BALL_RADIUS
        ball_speed_y *= -1

    # 更新小球速度(模拟重力)
    ball_speed_y += GRAVITY

    # 绘制小球
    pygame.draw.circle(window, BALL_COLOR, (int(ball_x), int(ball_y)), BALL_RADIUS)

    pygame.display.update()
    clock.tick(FPS)

这段代码使用 Pygame 库创建了一个窗口,并在窗口中绘制了一个红色的小球。

小球会在窗口内上下弹跳,模拟了简单的重力运动。

你可以通过修改代码中的参数来调整窗口大小、小球属性和行为等。

当你运行这段代码时,会打开一个窗口,窗口的大小为800x600像素,背景为黑色。在窗口的中央绘制了一个红色的小球,初始位置位于窗口的正中央。

运行效果

接下来,让我们一行一行地解释代码:

  1. 导入必要的模块:
import pygame
import sys
  1. 初始化 Pygame:
pygame.init()
  1. 设置窗口尺寸、标题和帧率:
WIDTH, HEIGHT = 800, 600
WINDOW_SIZE = (WIDTH, HEIGHT)
WINDOW_TITLE = "弹跳小球"
FPS = 60
  1. 定义颜色:
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
  1. 设置小球属性:
BALL_RADIUS = 20
BALL_COLOR = RED
BALL_SPEED_Y = 5
GRAVITY = 0.2
  1. 创建窗口:
window = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption(WINDOW_TITLE)
clock = pygame.time.Clock()
  1. 初始化小球的位置和速度:
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
ball_speed_x = 0
ball_speed_y = 0
  1. 进入游戏主循环:
while True:
  1. 处理事件,例如关闭窗口事件:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
  1. 更新小球的位置:
ball_x += ball_speed_x
ball_y += ball_speed_y
  1. 检查小球是否触底,如果触底则让小球反弹:
if ball_y + BALL_RADIUS >= HEIGHT:
    ball_y = HEIGHT - BALL_RADIUS
    ball_speed_y *= -1
  1. 更新小球的速度,模拟重力:
ball_speed_y += GRAVITY
  1. 绘制小球:
pygame.draw.circle(window, BALL_COLOR, (int(ball_x), int(ball_y)), BALL_RADIUS)
  1. 更新屏幕显示:
pygame.display.update()
  1. 控制帧率:
clock.tick(FPS)

这样,小球就会在窗口中上下弹跳,模拟了简单的重力运动。

你可以通过修改代码中的参数来调整窗口大小、小球属性和行为等。

了解更多python项目开发,请关注我:Python提升课堂

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Python弹球小游戏的例子,代码如下所示: ```python import turtle import time import random # 设置窗口 win = turtle.Screen() win.title("弹球小游戏") win.bgcolor("black") win.setup(width=600, height=600) # 创建球拍 A paddle_a = turtle.Turtle() paddle_a.speed(0) paddle_a.shape("square") paddle_a.color("white") paddle_a.shapesize(stretch_wid=1, stretch_len=5) paddle_a.penup() paddle_a.goto(0, -250) # 创建球 ball = turtle.Turtle() ball.speed(40) ball.shape("circle") ball.color("white") ball.penup() ball.goto(0, 0) ball.dx = 3 ball.dy = -3 # 移动球拍 A def move_left(): x = paddle_a.xcor() x -= 20 paddle_a.setx(x) def move_right(): x = paddle_a.xcor() x += 20 paddle_a.setx(x) # 键盘绑定 win.listen() win.onkeypress(move_left, "Left") win.onkeypress(move_right, "Right") # 循环运行游戏 while True: win.update() # 移动球 ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) # 碰到左右边界 if ball.xcor() > 290: ball.setx(290) ball.dx *= -1 elif ball.xcor() < -290: ball.setx(-290) ball.dx *= -1 # 碰到上下边界 if ball.ycor() > 290: ball.sety(290) ball.dy *= -1 elif ball.ycor() < -290: ball.goto(0, 0) ball.dy *= -1 time.sleep(1) # 碰到球拍 if (ball.ycor() < -240 and ball.ycor() > -250) and (ball.xcor() < paddle_a.xcor() + 50 and ball.xcor() > paddle_a.xcor() - 50): ball.dy *= -1 # 游戏结束 if ball.ycor() < -290: break # 显示游戏结束 game_over = turtle.Turtle() game_over.speed(0) game_over.color("white") game_over.penup() game_over.hideturtle() game_over.goto(0, 0) game_over.write("游戏结束", align="center", font=("Courier", 24, "normal")) # 延迟关闭窗口 time.sleep(3) win.bye() ``` 这个小游戏中,玩家需要控制球拍 A 来接住弹出的球,如果球落地则游戏结束。玩家可以通过键盘左右箭头来移动球拍 A,当球碰到球拍 A 时,球会反弹。这个小游戏可以帮助玩家更好地理解 Python 类的概念,并且也是一个有趣的小游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值