pygame落球游戏优化

16 篇文章 2 订阅

这篇文章是对pygame实现落球游戏1的优化

优化挡板的控制为左右按键

如果使用鼠标控制非常的不好控制,而且控制不连续

引入对应变量

这里pos_x和pos_y作为挡板的初始位置,不在作为和鼠标的位置挂钩

# 挡板初始位置
pos_x = 300
pos_y = 460
# 标注方向的变量  -1 左 0 不动 1 右
direction = 0
# 改变位置幅度
rangeChange = 5

捕捉左右按键事件

左右按键按下,改变方向变量,如果左右键抬起,则将方向变为不变

 # 按键之后改变方向
        elif event.type == KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
            elif event.key == pygame.K_RIGHT:
                direction = 1
            elif event.key == pygame.K_LEFT:
                direction = -1
        elif event.type == KEYUP:
            if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                direction = 0

根据方向和改变位置幅度,改变位置

这里将最大右方向坐标修改为480,挡板宽度为120,这样不会超出窗口的宽度600

	pos_x += rangeChange * direction
    if pos_x < 0:
        pos_x = 0
    elif pos_x > 480:
        pos_x = 480

在这里插入图片描述

圆球位置和阴影考虑

圆球位置,

这里(bomb_x, int(bomb_y))值的是圆心位置,如果随机到0的话,那么就会出现屏幕只有一个半圆,圆的半径是30,起始位置30,到结束的位置减去30的范围,所以是30-570,纵坐标也不必要从 -50开始,-35即可。

# 球落下的x和y轴坐标
bomb_x = random.randint(0, 500)
bomb_y = -50

	pygame.draw.circle(screen, yellow, (bomb_x, int(bomb_y)), 30, 0)

修改

bomb_x = random.randint(30, 570)
bomb_y = -35

阴影考虑

纯黑色的阴影过于深,考虑一个浅色,将阴影的半径设置为1

black = 32, 32, 32

    pygame.draw.circle(screen, black, (bomb_x - 1, int(bomb_y) - 1), 30, 0)
    pygame.draw.circle(screen, yellow, (bomb_x, int(bomb_y)), 30, 0)

    pygame.draw.rect(screen, black, (pos_x - 1, pos_y - 1, 120, 40), 0)
    pygame.draw.rect(screen, red, (pos_x, pos_y, 120, 40), 0)

在这里插入图片描述

游戏结束实现提示

显示一个爆炸在屏幕之上

在这里插入图片描述
如果游戏进行而且失败了,则显示

# 游戏进行flg
game_start = False

    if game_start and game_over:
        pygame.draw.polygon(screen, (255, 255, 0), ((80, 80), (160, 120), (500, 120), (460, 320),
                                                    (320, 300), (160, 400), (150, 200), (80, 80)))
        print_text(font2, 280, 200, "爆炸", (0, 0, 0))

全部代码

import sys, random,  pygame
from pygame.locals import *


def print_text(font, x, y, text, color=(255, 255, 255)):
    img_text = font.render(text, True, color)
    screen.blit(img_text, (x, y))


pygame.init()
# 定时器
mainClock = pygame.time.Clock()

# 设置屏幕和窗口标题
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("落球游戏")
# 设置字体
font1 = pygame.font.SysFont("方正粗黑宋简体", 24)
font2 = pygame.font.SysFont("方正粗黑宋简体", 48)
pygame.mouse.set_visible(False)
# 设置颜色变量
white = 255, 255, 255
red = 220, 50, 50
yellow = 230, 230, 50
black = 32, 32, 32

# 生命条数
lives = 3
# 分
score = 0
# 游戏开始flg
game_over = True
# 游戏进行flg
game_start = False
# 鼠标位置
mouse_x = mouse_y = 0
# 挡板初始位置
pos_x = 300
pos_y = 460
# 球落下的x和y轴坐标
bomb_x = random.randint(30, 570)
bomb_y = -35
# 球下落的速度
vel_y = 0.7
# 标注方向的变量  -1 左 0 不动 1 右
direction = 0
# 改变位置幅度
rangeChange = 5


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == MOUSEBUTTONUP:
            if game_over:
                game_over = False
                game_start = True
                lives = 3
                score = 0
        # 按键之后改变方向
        elif event.type == KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
            elif event.key == pygame.K_RIGHT:
                direction = 1
            elif event.key == pygame.K_LEFT:
                direction = -1
        elif event.type == KEYUP:
            if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                direction = 0

    screen.fill((255, 166, 77))

    if game_over:
        print_text(font1, 300, 400, "点击新游戏")
    else:
        bomb_y += vel_y

    if game_start and game_over:
        pygame.draw.polygon(screen, (255, 255, 0), ((80, 80), (160, 120), (500, 120), (460, 320),
                                                    (320, 300), (160, 400), (150, 200), (80, 80)))
        print_text(font2, 280, 200, "爆炸", (0, 0, 0))

    if bomb_y > 500:
        bomb_x = random.randint(0, 500)
        bomb_y = -50
        lives -= 1
    if lives == 0:
        game_over = True

    elif bomb_y > pos_y:
        if bomb_x > pos_x and bomb_x < pos_x + 120:
            score += 120
            bomb_x = random.randint(0, 500)
            bomb_y = -50

    pygame.draw.circle(screen, black, (bomb_x - 1, int(bomb_y) - 1), 30, 0)
    pygame.draw.circle(screen, yellow, (bomb_x, int(bomb_y)), 30, 0)
    pos_x += rangeChange * direction
    if pos_x < 0:
        pos_x = 0
    elif pos_x > 460:
        pos_x = 460

    pygame.draw.rect(screen, black, (pos_x - 1, pos_y - 1, 120, 40), 0)
    pygame.draw.rect(screen, red, (pos_x, pos_y, 120, 40), 0)

    print_text(font1, 0, 0, "生命数: " + str(lives))

    print_text(font1, 500, 0, "分数:  " + str(score))

    pygame.display.update()
    mainClock.tick(2000)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的天才女友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值