【问题标题】:pygame health display issuepygame健康显示问题
【发布时间】:2021-09-29 13:52:28
【问题描述】:

所以我正在观看 Teach with Tim pygame 90 分钟,我在 1:24:39。当我运行代码时,红色船的健康在我射击它时会持续下降(通过黄色船并且当红色船撞到黄色船时),直到我移动其中一艘船或在我只射击另一艘船时按下射击。出了什么问题有人可以帮忙吗? 我认为问题出在这部分,但我不确定:

 if event.type == red_hit:
    red_health -= 1

 if event.type == yellow_hit:  
    yellow_health -= 1 


    import pygame
    import os
    import time
    pygame.font.init()
    
    width , height = 900 , 500
    
    dispay_surface = pygame.display.set_mode((width, height))
    
    pygame.display.set_caption("first game")
    
    teal = (153,255,255)
    light_green = (102,255,102)
    BLACK = (0,0,0)
    RED = (255,0,0)
    YELLOW = (255,255,0)
    
    BORDER = pygame.Rect(width//2,0,10,height)
    
    health_font = pygame.font.SysFont('comicsans', 40)
    
    
    fps = 60
    VEL = 5
    BULLET_VEL = 7
    max_bullet = 10
    spaceship_width, spaceship_height = 55, 40
    
    
    yellow_hit = pygame.USEREVENT +1
    red_hit = pygame.USEREVENT +2
    
    
    #making the space ship and rotating it correctly
    yellow_spaceship_image = pygame.image.load(
        os.path.join('Assets','spaceship_yellow.png'))
    
    yellow_spaceship_nr = pygame.transform.scale(
        yellow_spaceship_image, (spaceship_width, spaceship_height))
    
    yellow_spaceship = pygame.transform.rotate(yellow_spaceship_nr, 90)
    
    red_spaceship_image = pygame.image.load(
        os.path.join('Assets','spaceship_red.png'))
    
    red_spaceship_nr=  pygame.transform.scale(
        red_spaceship_image, (spaceship_width, spaceship_height))
    
    red_spaceship = pygame.transform.rotate(red_spaceship_nr, -90)
    
    
    space_background = pygame.transform.scale(pygame.image.load(os.path.join('Assets','space.png')),(width,height))
    
    
    def draw_window(red,yellow,red_bullet,yellow_bullet,red_health,yellow_health):
    
        dispay_surface.blit(space_background,(0,0))
    
        #showing the health
        red_health_text = health_font.render("health: " + str(red_health), 1 , teal)
        yellow_health_text = health_font.render("health: " + str(yellow_health), 1, teal)
        dispay_surface.blit(red_health_text,(width - red_health_text.get_width()- 10 , 10))
        dispay_surface.blit(yellow_health_text, (10 , 10))
    
        #use to draw the ship
        dispay_surface.blit(yellow_spaceship,(yellow.x, yellow.y))
        dispay_surface.blit(red_spaceship,(red.x, red.y))
    
        #drawing a border so the 2 spaceship can't go on top of each other
        pygame.draw.rect(dispay_surface,BLACK,BORDER)
    
        for bullet in red_bullet:
            pygame.draw.rect(dispay_surface,RED,bullet)
        for bullet in yellow_bullet:
            pygame.draw.rect(dispay_surface,YELLOW,bullet) 
        
    
    
        pygame.display.update()  
    
    #getting yellow ship to move when player input
    def yellow_mov(keys_pressed,yellow):
        if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: #right
                yellow.x += VEL
        if keys_pressed[pygame.K_a]  and yellow.x - VEL > 0: #left
                yellow.x -= VEL
        if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: #up
                yellow.y -= VEL
        if keys_pressed[pygame.K_s] and yellow.y - VEL + yellow.height < height - 15 : #down
             yellow.y += VEL
    
    #getting red to move
    def red_mov(keys_pressed,red):
        if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < width: #right
                red.x += VEL
        if keys_pressed[pygame.K_LEFT] and red.x - VEL >BORDER.x + BORDER.width: #left
                red.x -= VEL
        if keys_pressed[pygame.K_UP] and red.y - VEL > 0: #up
                red.y -= VEL
        if keys_pressed[pygame.K_DOWN] and red.y - VEL + red.height < height - 15: #down
                red.y += VEL
    
    def handle_bullet(yellow_bullet,red_bullet,yellow,red):
        for bullet in yellow_bullet:
            bullet.x += BULLET_VEL
            if red.colliderect(bullet):
                pygame.event.post(pygame.event.Event(red_hit))
                yellow_bullet.remove(bullet)
            elif  bullet.x > width:
                yellow_bullet.remove(bullet)
    
        for bullet in red_bullet:
            bullet.x -= BULLET_VEL
            if yellow.colliderect(bullet):
                pygame.event.post(pygame.event.Event(red_hit))
                red_bullet.remove(bullet)       
            elif  bullet.x < 0 :
                red_bullet.remove(bullet)
    
    
    
    
    #keep window running
    def main():
        #determin the player position
        red = pygame.Rect(700,300,spaceship_width,spaceship_height)
        yellow = pygame.Rect(100,300,spaceship_width,spaceship_height)
        
        red_bullet = []
        yellow_bullet = []
    
        red_health = 10
        yellow_health = 10
    
    
        clock = pygame.time.Clock()
        run = True  
        while run:
            clock.tick(fps)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                  run = False  
                #Shoot the bullet
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LCTRL and len(yellow_bullet) < max_bullet:
                        bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 , 10 ,5)
                        yellow_bullet.append(bullet)
    
                    if event.key == pygame.K_RCTRL and len(red_bullet) < max_bullet:
                        bullet = pygame.Rect(red.x , red.y + red.height//2  , 10 ,5)
                        red_bullet.append(bullet) 
            if event.type == red_hit:
              red_health -= 1
    
            if event.type == yellow_hit:  
              yellow_health -= 1 
    
            if red_health <=0:
             winner_text = "yellow wins"
            if yellow_health <=0:
             winner_text = "red wins"
    
            keys_pressed = pygame.key.get_pressed()  
            yellow_mov(keys_pressed,yellow) 
            red_mov(keys_pressed,red)
            handle_bullet(yellow_bullet,red_bullet,yellow,red)
            draw_window(red,yellow,red_bullet,yellow_bullet,red_health,yellow_health)       
        pygame.quit()     
       
    
    
    if __name__ == '__main__':
        main()

【问题讨论】:

  • 您的 if 语句在 for 循环之外。缩进和一致性很重要。

标签: python python-3.x pygame


【解决方案1】:

我查看了您的代码,我认为问题在于这段代码的 sn-p:

if event.type == red_hit:
    red_health -= 1
if event.type == yellow_hit:
    yellow_health -= 1

在你的主循环而不是你的

for event in pygame.event.get():

循环。

尝试将格式更改为:

for event in pygame.event.get():

    if event.type == pygame.QUIT:
        run = False  

    #Shoot the bullet
    if event.type == pygame.KEYDOWN:

        if event.key == pygame.K_LCTRL and len(yellow_bullet) < max_bullet:
            bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height // 2, 10, 5)
            yellow_bullet.append(bullet)

        if event.key == pygame.K_RCTRL and len(red_bullet) < max_bullet:
            bullet = pygame.Rect(red.x, red.y + red.height // 2, 10 ,5)
            red_bullet.append(bullet)

        if event.type == red_hit:
            red_health -= 1

        if event.type == yellow_hit:  
            yellow_health -= 1

【讨论】:

  • 我尝试将其更改为我的代码,但船舶健康状况一直在下降,直到我移动它,但感谢您的帮助
【解决方案2】:

这里的代码有问题。

def handle_bullet(yellow_bullet,red_bullet,yellow,red):
    for bullet in yellow_bullet:
        bullet.x += BULLET_VEL
        if red.colliderect(bullet):
            pygame.event.post(pygame.event.Event(red_hit))
            yellow_bullet.remove(bullet)
        elif  bullet.x > width:
            yellow_bullet.remove(bullet)

    for bullet in red_bullet:
        bullet.x -= BULLET_VEL
        if yellow.colliderect(bullet):
            pygame.event.post(pygame.event.Event(red_hit))
            red_bullet.remove(bullet)       
        elif  bullet.x < 0 :
            red_bullet.remove(bullet)

您已经考虑了 red_hit 的两个事件。更改如下。

def handle_bullet(yellow_bullet,red_bullet,yellow,red):
for bullet in yellow_bullet:
    bullet.x += BULLET_VEL
    if red.colliderect(bullet):
        pygame.event.post(pygame.event.Event(red_hit))
        yellow_bullet.remove(bullet)
    elif  bullet.x > width:
        yellow_bullet.remove(bullet)

for bullet in red_bullet:
    bullet.x -= BULLET_VEL
    if yellow.colliderect(bullet):
        pygame.event.post(pygame.event.Event(yellow_hit))
        red_bullet.remove(bullet)       
    elif  bullet.x < 0 :
        red_bullet.remove(bullet)

还有一个缩进问题,如下修改。

for event in pygame.event.get():
        if event.type == pygame.QUIT:
          run = False  
        #Shoot the bullet
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LCTRL and len(yellow_bullet) < max_bullet:
                bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 , 10 ,5)
                yellow_bullet.append(bullet)

            if event.key == pygame.K_RCTRL and len(red_bullet) < max_bullet:
                bullet = pygame.Rect(red.x , red.y + red.height//2  , 10 ,5)
                red_bullet.append(bullet) 
        if event.type == red_hit:
            red_health-= 1

        if event.type == yellow_hit:  
            yellow_health-= 1 

发现您的代码存在另一个问题。飞船的生命值一直在0以下,和这里有关。

if red_health <=0:
        winner_text = "yellow wins"

当生命值小于或等于零时,您必须停止游戏。使用 break 停止。 更改如下=>

if red_health <=0:
        winner_text = "yellow wins"
        print(winner_text)
        break
if yellow_health <=0:
        winner_text = "red wins"
        print(winner_text)
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2019-07-28
    • 1970-01-01
    • 2018-05-18
    相关资源
    最近更新 更多