为什么我的图像在闪烁?(Pygame)

2024-09-29 00:15:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图“动画”一个精灵,只要加载一组4个图像反复,而分配的左/右按钮被按下。它基本上是有效的,只是图像一直在闪烁,并不像我想的那样“切换”。它的速度也大大减慢。作为一个Pygame/Python的初学者,我的大脑完全被炸飞了。在

import pygame
import os

from test_menu import drawmenu
from game_over import player1wins
from game_over import player2wins
from game_over import tie
pygame.init()

while True: 
    screen_size = (1080, 400)
    screen = pygame.display.set_mode(screen_size)

    background = (255,255,255)
    image = pygame.image.load("background(1).png")
    imagerect = image.get_rect()
    wall_left = pygame.Rect(0,0,1,950)
    wall_right = pygame.Rect(1080,0,1,950)

    square1 = pygame.Rect(750, 350, 20, 20)
    square2 = pygame.Rect(330, 350, 20, 20)
    IMAGE = pygame.image.load("player1standright.png").convert_alpha()
    rect = IMAGE.get_rect()
    rect.x = 100
    rect.y = 225

    x_movement = 0
    y_movement = 0
    x2_movement = 0
    y2_movement = 0

    accelx1 = 0
    accely1 = 0
    accelx2 = 0
    accely2 = 0

    jumping = False
    jumping2 = False

    #This is the function that does it
    def player1runright(images1right, IMAGE, image, imagerect):
        i = -1
        while i <= 2:
            pygame.time.delay(0)
            i += 1
            print (i)
            IMAGE = pygame.image.load(images1right[i])
            print (images1right[i])
            screen.blit(IMAGE, rect)

    images1right = ["player1runright-1.png","player1runright-2.png","player1runright-3.png","player1runright-4.png"]

    def health_bars(player1_health, player2_health):
        if player1_health > 75:
            player1_health_color = (62, 219, 41)
        elif player1_health > 50:
            player1_health_color = (239, 212, 9)
        else:
            player1_health_color = (239, 9, 9)

        if player2_health > 75:
            player2_health_color = (62, 219, 41)
        elif player2_health > 50:
            player2_health_color = (239, 212, 9)
        else:
            player2_health_color = (239, 9, 9)

        pygame.draw.rect(screen, player1_health_color, (70, 50, player1_health, 25))
        pygame.draw.rect(screen, player2_health_color, (900, 50, player2_health, 25))

    player1_health = int(100)
    player2_health = int(100)

    drawmenu()

    while True:
        screen.fill(background)
        screen.blit(image, imagerect)
        pygame.draw.rect(screen, (255,0,0), square1)
        pygame.draw.rect(screen, (0,0,255), square2)
        pygame.draw.rect(screen, (0,0,0), wall_left)
        pygame.draw.rect(screen, (0,0,0), wall_right)
        health_bars(player1_health, player2_health)
        pygame.display.update()
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                exit()
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_d] != 1:
            screen.blit(IMAGE, rect)

        #MOVEMENT P1

        if keys_pressed[pygame.K_LEFT] == 1:
            x_movement += -1
        elif keys_pressed[pygame.K_RIGHT] == 1:
            x_movement += 1
        else:
            x_movement = 0

        #MOVEMENT P2

        if keys_pressed[pygame.K_a] == 1:
            x2_movement += -1
        elif keys_pressed[pygame.K_d] == 1:
            x2_movement += 1
            player1runright(images1right, IMAGE, image, imagerect)
        else:
            x2_movement = 0

        #MAX SPEED P1
        if x_movement < -7:
            x_movement = -7
        if x_movement > 7:
            x_movement = 7
        if y_movement < -7:
            y_movement = -7
        if y_movement > 7:
            y_movement = 7

        #MAX SPEED P2
        if x2_movement < -7:
            x2_movement = -7
        if x2_movement > 7:
            x2_movement = 7
        if y2_movement < -7:
            y2_movement = -7
        if y2_movement > 7: 
            y2_movement = 7

        #COLLISION (WALL) P1

        if square1.colliderect(wall_left):
            player2_health -= 0.5
            x_movement = 0
            y_movement = 0
            if keys_pressed[pygame.K_RIGHT] == 1:
                x_movement += 1

        if square1.colliderect(wall_right):
            player2_health -= 0.5
            x_movement = 0
            y_movement = 0
            if keys_pressed[pygame.K_LEFT] == 1:
                x_movement += -1

        #COLLISION (WALL) P2

        if rect.colliderect(wall_left):
            player1_health -= 0.5
            x2_movement = 0
            y2_movement = 0
            rect.x = 0
            rect.y = 0
            if keys_pressed[pygame.K_d] == 1:
                x2_movement += 1
                rect.x += 1

        if rect.colliderect(wall_right):
            player1_health -= 0.5
            x2_movement = 0
            y2_movement = 0
            rect.x = 0
            rect.y = 0
            if keys_pressed[pygame.K_a] == 1:
                x2_movement += -1
                rect.x += -1


        #COLLISION (ATTACK) P1
        if square1.colliderect(rect) and keys_pressed[pygame.K_g] == 1:
            player2_health -= 1

        #COLLISION (ATTACK) P2
        if rect.colliderect(square1) and keys_pressed[pygame.K_p] == 1:
            player1_health -= 1

        #JUMPING P1
        if keys_pressed[pygame.K_w] == 1:
            if jumping2 == False:
                jumping2 = True

                y2_movement = -30
                accely2 = 1

        rect = rect.move(x2_movement, y2_movement)

        x2_movement += accelx2
        y2_movement += accely2

        if jumping2 == True:
            if rect.y >= 225:
                jumping2 = False

                rect.y = 225
                y2_movement = 0
                accely2 = 0

        #JUMPING P2
        if keys_pressed[pygame.K_UP] == 1:
            if jumping == False:
                jumping = True

                y_movement = -30
                accely1 = 1

        square1 = square1.move(x_movement, y_movement)

        x_movement += accelx1
        y_movement += accely1

        if jumping == True:
            if square1.y >= 350:
                jumping = False

                square1.y = 350
                y_movement = 0
                accely1 = 0

        pygame.display.update()
        pygame.time.delay(20)

        if player2_health == 0 and player1_health !=0:
            pygame.time.delay(400)
            player1wins()
            pygame.time.delay(3000)
            drawmenu()
            break
        elif player1_health == 0 and player2_health !=0:
            pygame.time.delay(400)
            player2wins()
            pygame.time.delay(3000)
            drawmenu()
            break
        if player1_health == 0 and player2_health == 0:
            pygame.time.delay(400)
            tie()
            pygame.time.delay(3000)
            drawmenu()
            break

(我知道代码有点长,但我不知道我是否可以删掉其中的任何一个。)


Tags: rectimageifkeysscreenpygamex2health
2条回答

你的blit rect是错误的(至少)两个原因:

  1. player1runright中,使用一个名为imagerect的参数,但是使用rect进行blit,上面定义为来自"player1standright.png"的rect。那个矩形永远不变。在
  2. 调用player1runright时,传递imagerect的值,该值定义为"background(1).png"中的rect。同样,这是永远不会改变的,它不是你想要使用的rect。在

这两个问题都是由于变量“泄漏”到其他范围造成的。有时这是需要的,但在这里它只是导致错误。我会考虑重新组织代码(除非有意捕获包含范围,否则不要嵌套函数)以帮助查找这些错误。在

关于减速:不要每帧都加载图像。文件加载缓慢。装载一次,然后存放在某处。在

闪烁是因为每帧更新两次屏幕。删除第一行pygame.display.update(),它应该可以工作了。通常每个帧应该只有一个pygame.display.updatepygame.display.flip调用。在

我通过连续删除程序的一部分,直到只剩下一个最小的例子,就知道了。每次删除某些内容后都要检查错误是否仍然存在。在


动画的问题与此无关,但这里有一个快速的解决方案。首先,必须删除while函数中的while循环,因为它将立即在每帧中逐个地blit所有图像。在

如果按了d键,我只需每帧增加一个索引变量,从动画列表中取出相应的图像,并将其分配给保存当前图像的变量,然后进行blit操作。在

import sys
import pygame

pygame.init()
clock = pygame.time.Clock()

# Some surfaces which serve as the player images.
PLAYER_IMAGE = pygame.Surface((30, 50))  # Idle player image.
PLAYER_IMAGE.fill((30, 150, 230))
IMAGE1 = pygame.Surface((30, 55))
IMAGE1.fill((60, 150, 180))
IMAGE2 = pygame.Surface((30, 60))
IMAGE2.fill((90, 150, 130))
IMAGE3 = pygame.Surface((30, 65))
IMAGE3.fill((120, 150, 80))
IMAGE4 = pygame.Surface((30, 70))
IMAGE4.fill((150, 150, 30))
PLAYER1_IMAGES_RIGHT = [IMAGE1,IMAGE2,IMAGE3,IMAGE4]

anim_index = 0

while True:
    screen = pygame.display.set_mode((1080, 400))
    background_image = pygame.Surface(screen.get_size())
    background_image.fill((30, 30, 30))
    background_rect = background_image.get_rect()

    player_image = PLAYER_IMAGE
    player_rect = player_image.get_rect(x=100, y=225)

    while True:
        # Handle the events.
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_d]:
            # Increment the index and change the player image.
            anim_index += 1
            anim_index %= len(PLAYER1_IMAGES_RIGHT)*4
            # * 4 in the line above and floor div 4 to slow the animation down.
            player_image = PLAYER1_IMAGES_RIGHT[anim_index//4]
        else:  # Switch back to the idle image.
            player_image = PLAYER_IMAGE
            # Reset the index, so that we don't start in the middle of the anim.
            anim_index = 0

        # Draw everything at the end.
        screen.blit(background_image, background_rect)
        # Just blit the current player image at the player_rect's topleft coords.
        screen.blit(player_image, player_rect)

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

另外,看看this answer。在

相关问题 更多 >