python 当投射物到达窗口的左端而不是右端时,我的程序似乎会 Flink ,是我的代码有问题吗?

7lrncoxx  于 2023-03-28  发布在  Python
关注(0)|答案(1)|浏览(89)

这模拟了一个2-D游戏,显示一个人走路和射击弹丸

import pygame

pygame.init()

win = pygame.display.set_mode((500, 480))
pygame.display.set_caption('Legend of Zorro')

walkRight = [pygame.image.load('Game/R1.png'), pygame.image.load('Game/R2.png'), pygame.image.load('Game/R3.png'),
             pygame.image.load(
                 'Game/R4.png'), pygame.image.load('Game/R5.png'), pygame.image.load('Game/R6.png'),
             pygame.image.load('Game/R7.png'), pygame.image.load(
        'Game/R8.png'), pygame.image.load('Game/R9.png')]
walkLeft = [pygame.image.load('Game/L1.png'), pygame.image.load('Game/L2.png'), pygame.image.load('Game/L3.png'),
            pygame.image.load(
                'Game/L4.png'), pygame.image.load('Game/L5.png'), pygame.image.load('Game/L6.png'),
            pygame.image.load('Game/L7.png'), pygame.image.load(
        'Game/L8.png'), pygame.image.load('Game/L9.png')]
bg = pygame.image.load('Game/bg.jpg')
char = pygame.image.load('Game/standing.png')

clock = pygame.time.Clock()
framerate = 45

这是模拟主角功能的玩家类

class Player(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 10
        self.isJump = False
        self.jumpCount = 10
        self.left = True
        self.right = False
        self.walkCount = 0
        self.standing = True

    def draw(self, win):
        if self.walkCount + 1 >= 27:
            self.walkCount = 0

        if not self.standing:

            if self.left:
                win.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            elif self.right:
                win.blit(walkRight[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
        else:
            if self.right:
                win.blit(walkRight[0], (self.x, self.y))
            else:
                win.blit(walkLeft[0], (self.x, self.y))

我相信问题出在这部分代码上

class projectile(object):
    def __init__(self, x, y, radius, colour, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.colour = colour
        self.facing = facing
        self.vel = 8 * facing

    def draw(self, win):
        pygame.draw.circle(win, self.colour, (self.x, self.y), self.radius)

直到现在

def redrawGameWindow():
    win.blit(bg, (0, 0))
    man.draw(win)
    for bullet in bullets:
        bullet.draw(win)

    pygame.display.update()

man = Player(300, 410, 64, 64)
bullets = []

这就是While循环

run = True
while run:
    clock.tick(framerate)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

我也有一个理论,当子弹到达终点时将其移除,屏幕在子弹从数组中移除之前无法刷新。但为什么只有当射弹向右侧射击时才会发生这种情况?

for bullet in bullets:
        if 500 > bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.remove(bullet)
            break

以下是指示动作的按键

keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        if man.left:
            facing = -1
        else:
            facing = 1

        if len(bullets) < 1:
            bullets.append(
                projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))

    if keys[pygame.K_LEFT] and man.x > man.vel:
        man.x -= man.vel
        man.left = True
        man.right = False
        man.standing = False
    elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
        man.x += man.vel
        man.right = True
        man.left = False
        man.standing = False

    else:
        man.standing = True
        man.walkCount = 0

    if not man.isJump:
        if keys[pygame.K_UP]:
            man.isJump = True
            man.walkCount = 0
    else:
        if man.jumpCount >= -10:
            neg = 1
            if man.jumpCount < 0:
                neg = -1
            man.y -= (man.jumpCount ** 2) * 0.5 * neg
            man.jumpCount -= 1
        else:
            man.isJump = False
            man.jumpCount = 10

    redrawGameWindow()

pygame.quit()

编辑#1

删除中断功能-无改进
break函数实际上是我在代码的早期阶段添加的,并且忘记删除。即使在删除之后, Flink 仍然存在。我想强调一点,我在原始帖子中没有提到。虽然 Flink 似乎只发生在窗口的左侧,并且只发生在子弹与窗口左侧碰撞时,有随机的情况下, Flink 没有发生!所以我的一部分想知道这个问题是否归因于我的代码以外的东西!!

rnmwe5a2

rnmwe5a21#

当你移除一个子弹时不要break循环,而是贯穿所有的子弹。如果你在移动子弹的循环中省略了一些子弹,可能会导致子弹运动的明显中断。据推测,break用于防止异常。遍历项目符号列表的浅副本(bullets[:])而不是中断循环。(另见How to remove items from a list while iterating?)。

for bullet in bullets[:]:
    if 500 > bullet.x > 0:
        bullet.x += bullet.vel
    else:
        bullets.remove(bullet)

相关问题