尝试发射与init()有关的项目符号时,出现位置信息“ shots”丢失的错误消息

我对如何解决这个问题一无所知(对pygame来说是新的),我们将不胜感激。

import pygame
pygame.init()

window = pygame.display.set_mode((650, 630))

pygame.display.set_caption("PeaShooters")

avatar = pygame.image.load('Sprite 1 Red.png')
background = pygame.image.load('Bg.jpg')
white = (255, 255, 255)

class player(object):
    def __init__(self, x, y, width, height, shots):
        self.x = 300
        self.y = 500
        self.width = 40
        self.height = 60
        self.vel = 9



def drawGrid():
    window.blit(background, (0,0))
    window.blit(avatar, (av.x, av.y))
    pygame.draw.line(window, white, [50,50], [50, 600], 5)
    pygame.draw.line(window, white, [50,50], [600, 50], 5)
    pygame.draw.line(window, white, [600,600], [600, 50], 5)
    pygame.draw.line(window, white, [50,600], [600, 600], 5)
    pygame.draw.line(window, white, [50,450], [600, 450], 5)
    for bullet in bullets:
        bullet.draw(window)
    pygame.display.update()


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

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

av = player(300, 500, 40, 60)
bullets = []
running = True
while running:
    pygame.time.delay(100)

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

    for bullet in bullets:
        if bullet.x < 500 and bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_w] and av.y > 440:
        av.y -= av.vel

    if keys[pygame.K_a] and av.x > 65:
        av.x -= av.vel

    if keys[pygame.K_s] and av.y < 535:
        av.y += av.vel

    if keys[pygame.K_d] and av.x < 530:
        av.x += av.vel

    if keys[pygame.K_SPACE]:
        if len(bullets) < 5:
            bullets.append(shots(round(av.x + av.width//2)), round(av.y + av.height//2), 6, (0,0,0))


    drawGrid()

window.blit(avatar, (x,y))

pygame.quit()


子弹应该被发射,不是,我也想垂直发射子弹,我猜我只需要把x换成y就可以了。

最佳答案

关于您的错误,您的播放器类应带有5个参数,并且在初始化时为其指定了4个参数:

您的课程应该是:

class player(object):
    def __init__(self, x, y, width, height, shots):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = shots


并且您的实例应为:

av = player(300, 500, 40, 60, 9)

关于python - 尝试发射子弹时我该如何解决我遇到的问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55854723/

10-12 23:24