【问题标题】:How to make "player health" decrease when it collides with an enemy sprite in pygame [duplicate]如何在 pygame 中与敌方精灵碰撞时“玩家健康”减少 [重复]
【发布时间】:2023-02-01 02:10:45
【问题描述】:

我为玩家健康制作了一个表面并将其绘制在屏幕上。然后我做了一个 if 语句来检查玩家的 X 位置是否 == 到敌人的 X 位置,如果是的话玩家的生命值会持续下降。我知道它正在减少,因为我让它在控制台中打印出玩家的健康状况,一切都很好,但屏幕上的健康状况不会改变。我究竟做错了什么? 我是 pygame 的新手和 python 的初学者,因此我的代码不是以最好的方式编写的......

对不起,代码很长,但 # 应该显示所有内容......

import pygame
from sys import exit
from time import sleep
pygame.init()

clock = pygame.time.Clock()

screen = pygame.display.set_mode((1200, 535))
background = pygame.image.load("background1.jpg")
background = pygame.transform.rotozoom(background, 0, 2)

# Player
player_surf = pygame.image.load("player_sprites/player_right.png").convert_alpha()
player_rect = player_surf.get_rect(bottomright = (575, 470))
player_surf = pygame.transform.rotozoom(player_surf, 0, 2.5).convert_alpha()

# Health...
health = 50
health_font = pygame.font.Font('Pixeltype.ttf', 50)
health_display = health_font.render(f"Health {health}", True, (0, 0, 0))

# Enemy #1
enemy1_surf = pygame.image.load("enemy1sprites/enemy1_right.png").convert_alpha()
enemy1_rect = enemy1_surf.get_rect(bottomleft = (1100, 462))
enemy1_surf = pygame.transform.rotozoom(enemy1_surf, 0, 2.5).convert_alpha()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    screen.blit(background, (0, 0))
    screen.blit(player_surf, player_rect)
    screen.blit(enemy1_surf, enemy1_rect)
    screen.blit(health_display, (550, 75))

    # Player Movement
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            player_surf = pygame.image.load("player_sprites/player_left.png")
            player_surf = pygame.transform.rotozoom(player_surf, 0, 2.5)
            player_rect.x -= 4
        if event.key == pygame.K_RIGHT:
            player_surf = pygame.image.load("player_sprites/player_right.png")
            player_surf = pygame.transform.rotozoom(player_surf, 0, 2.5)
            player_rect.x += 4
        if player_rect.x >= 1075:
            player_rect.x = 1075
        elif player_rect.x <= 0:
            player_rect.x = 0
    wave_one = True
    # Enemy 1 AI
    if player_rect.x != enemy1_rect.x:
        if player_rect.x > enemy1_rect.x:
            enemy1_surf = pygame.image.load("enemy1sprites/enemy1_right.png")
            enemy1_surf = pygame.transform.rotozoom(enemy1_surf, 0, 2.5)
            enemy1_rect.x += 1
        elif player_rect.x < enemy1_rect.x:
            enemy1_surf = pygame.image.load("enemy1sprites/enemy1_left.png")
            enemy1_surf = pygame.transform.rotozoom(enemy1_surf, 0, 2.5)
            enemy1_rect.x -= 1
            
    # Collision with enemy
    if player_rect.x == enemy1_rect.x:
        health -= 0.005
        print(health)

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

我预计它会不断更新 health_display 但它没有。

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您是否尝试过更新/重新呈现屏幕上的文本?从您的代码来看,健康状况似乎仅在更新时打印,而不是在游戏中显示。

    【讨论】:

    • 你是怎样做的??
    猜你喜欢
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2023-03-21
    相关资源
    最近更新 更多