设置背景图片的初始位置

x_ori_bg = 0 y_ori_bg = 380 points = 0 font = pygame.font.SysFont(['方正粗黑宋简体','microsoftsansserif'], 20) # 分数的字体

设置障碍物列表

barriers = []

设置子弹列表

bullets = []

计算分数

def score(): global points, game_speed points += 1 if points % 150 == 0: # 分数每过150速度加1,时间越长速度越快 game_speed += 1 # 显示分数 text = font.render("Points: " + str(points), True, (0, 0, 0)) # render(内容,是否抗锯齿,字体颜色,字体背景颜色) textRect = text.get_rect() textRect.center = (550, 40) SCREEN.blit(text, textRect)

背景

def background(): global x_ori_bg, y_ori_bg image_width = BG.get_width() # 图像宽度 SCREEN.blit(BG, (x_ori_bg, y_ori_bg)) SCREEN.blit(BG, (image_width + x_ori_bg, y_ori_bg)) # 保证图片连续 if x_ori_bg <= -image_width: # 越界 SCREEN.blit(BG, (image_width + x_ori_bg, y_ori_bg)) x_ori_bg = 0 x_ori_bg -= game_speed # 背景图片每帧往左移game_speed个单位

flag_hit = 0 # 标记恐龙是否被击中过 death_cnt = 0 # 记录死亡次数,death_cnt = 0显示开始界面,大于0的就会显示重开界面 last_timestamp = 0 # 记录时钟记录的上一个时刻 last_timestamp_hit = 0 # 记录恐龙受到伤害时钟记录的上一个时刻 last_timestamp_barr_hit = 0 # 记录障碍物受到伤害时钟记录的上一个时刻 flag_barr_hit = 0 # 记录障碍物有没有受得伤害

while run: # 判断游戏是否退出 for event in pygame.event.get(): if event.type == pygame.QUIT: run = False

SCREEN.fill((255, 255, 255)) # 背景填充为白色
userInput = pygame.key.get_pressed() # 从键盘读入按键

# 3s内显示血量的简单逻辑
if flag_hit == 1:
    if pygame.time.get_ticks() - last_timestamp_hit <= 3000:
        player.showHit()
    if pygame.time.get_ticks() - last_timestamp_hit > 3000:
        flag_hit = 1

代码功能:设置背景图片的初始位置和分数的字体,以及障碍物和子弹列表,计算分数和绘制背景

score函数:计算分数并显示在屏幕上

background函数:绘制背景图像,实现背景图像的循环滚动

flag_hit、death_cnt、last_timestamp等变量用于记录游戏状态和时间,后面的while循环用于处理游戏事件和更新游戏界面

如果还没有障碍物,那么生成一个障碍物

if len(barriers) <= 1:

# 防止障碍物为0的情况下恐龙闪烁
player.draw(SCREEN)  # 渲染恐龙画面
player.update(userInput)  # 调用dinosaur的update函数每次渲染都判断一次是否按下相应的键位
player.showHp()

# 随机等可能生成障碍物
if random.randint(0, 2) == 0:
    barriers.append(SmallCactus(SMALL_CACTUS)) # 向列表添加障碍物元素
elif random.randint(0, 2) == 1:
    barriers.append(LargeCactus(LARGE_CACTUS))
elif random.randint(0, 2) == 2:
    barriers.append(Bird(BIRD))

for barrier in barriers: barrier.draw(SCREEN) # 调用barrier类的draw函数,渲染画面 barrier.update() barrier.showHp() # 显示血量

if flag_barr_hit == 1: # 击中判定
    if barrier.hp < 100 and pygame.time.get_ticks() - last_timestamp_barr_hit <= 3000:
        barrier.showHit() # 显示击中动画
    if pygame.time.get_ticks() - last_timestamp_barr_hit > 3000:
        flag_barr_hit = 0

if player.dino_rect.colliderect(barrier.rect): # pygame的一个方法colliderect检测两个物体是否碰撞
    last_timestamp_hit = pygame.time.get_ticks()
    player.hp -= 1  # 被击中生命值减1
    flag_hit = 1
    if not len(barriers) == 0:
        barriers.remove(barrier)
else:
    player.draw(SCREEN)  # 渲染恐龙画面
    player.update(userInput)  # 调用dinosaur的update函数每次渲染都判断一次是否按下相应的键位
    player.showHp()

if player.hp == 0: # 血量为0时
    player.showDead()
    player.draw_death(SCREEN)
    pygame.display.update() # 显示死亡动画
    pygame.time.delay(1000)
    death_cnt += 1
    menu(death_cnt) # 调出死亡界面

# 子弹击中障碍物部分
for bullet in bullets:
    bullet.draw(SCREEN)
    bullet.update()
    if bullet.rect.x > SCREEN_WIDTH: # 判定子弹位置是否在画面内
        if not len(bullets):
            bullets.remove(bullet)
    if bullet.rect.colliderect(barrier.rect):
        last_timestamp_barr_hit = pygame.time.get_ticks() # 子弹击中障碍物
        barrier.hp -= 50
        flag_barr_hit = 1
        bullets.remove(bullet)
        if not len(barriers) == 0 and barrier.hp <= 0:
            points += 50
            barriers.remove(barrier)

代码功能:生成障碍物并判断是否被击中,判断恐龙是否被击中或死亡,以及子弹是否击中障碍物

发射子弹, 并且保证每个子弹的发射间隔小于200毫秒

if (userInput[pygame.K_SPACE] and pygame.time.get_ticks() - last_timestamp >= 200) or (userInput[pygame.K_SPACE] and last_timestamp == 0): last_timestamp = pygame.time.get_ticks() # 更新当前时间 bullets.append(Bullet(game_speed, player))

代码功能:判断是否需要发射子弹并更新时间

pygame.display.update() # 画面更新

代码功能:更新屏幕显


原文地址: https://www.cveoy.top/t/topic/g71t 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录