我可以让我的子弹向上射击或向左射击

发布于 2025-02-12 22:25:23 字数 6314 浏览 0 评论 0原文

我是Pygame的新手,正在尝试用枪制作一个小角色,但是子弹不会向上射击,或者在

这里的左侧是我的代码:

import pygame
pygame.init()

gameScreen = pygame.display.set_mode()
pygame.display.set_caption('solo attempt')

clock = pygame.time.Clock()

class player(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 8
        self.facing = 1
    
    def draw(self, gameScreen):
        pygame.draw.rect(gameScreen, (255, 0, 0), (self.x, self.y, self.width, self.height))

class gun(object):
    def __init__(self, startx, starty):
        self.startx = startx
        self.starty = starty
        self.endx = self.startx
        self.endy = self.starty
        self.up = False
        self.down = False
        self.left = False
        self.right = False

    
    def draw(self, gameScreen):
        pygame.draw.line(gameScreen, (0, 0, 255), (self.startx, self.starty), (self.endx, self.endy))

class bulletThing(object):
    def __init__(self, x, y, radius):
        self.x = x
        self.y = y
        self.radius = radius
        self.verticalVel = 12 * vertical
        self.horizontalVel = 12 * horizontal

        print(self.verticalVel, self.horizontalVel)
    
    def draw(self, gameScreen):
        pygame.draw.circle(gameScreen, (0, 255, 0), (self.x, self.y), self.radius)

#updates screen
def drawScreen(gameScreen):
    gameScreen.fill((0, 0, 0))
    player.draw(gameScreen)
    gun.draw(gameScreen)
    for bullet in bullets:
        bullet.draw(gameScreen)
    pygame.display.update()

#create player and gun and bullets list
player = player(50, 50, 64, 64)
gun = gun(player.x + (player.width / 2), player.y + (player.height / 2))
bullets = []

vertical = 0
horizontal = 1

shootLoop = 0
dimensions = gameScreen.get_size()

#main loop
running = True
while running:
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    #shot delay
    if shootLoop > 0:
        shootLoop += 1
    if shootLoop > 3:
        shootLoop = 0

    #bullet movement
    for bullet in bullets:
        if bullet.x < dimensions[0] and bullet.x > 0:
            bullet.x += bullet.horizontalVel
        else:
            bullets.pop(bullets.index(bullet))

        if bullet.y < dimensions[1] and bullet.y > 0:
            bullet.y += bullet.verticalVel
        else:
            bullets.pop(bullets.index(bullet))


    keys = pygame.key.get_pressed()

    #move left
    if keys[pygame.K_a]:
        if player.x > 0:
            player.x -= player.vel
        if gun.startx > 0 + player.width / 2:
            gun.startx -= player.vel
            gun.endx -= player.vel

    #move right
    if keys[pygame.K_d]:
        if player.x < dimensions[0] - player.width:
            player.x += player.vel
        if gun.startx < dimensions[0] - player.width / 2:
            gun.startx += player.vel
            gun.endx += player.vel

    #move up
    if keys[pygame.K_w]:
        if player.y > 0 + player.height / 2:
            player.y -= player.vel
        if gun.starty > 0:
            gun.starty -= player.vel
            gun.endy -= player.vel

    #move down
    if keys[pygame.K_s]:
        if player.y < dimensions[1] - player.height:
            player.y += player.vel
        if gun.starty < dimensions[1] - player.height / 2:
            gun.starty += player.vel
            gun.endy += player.vel

#top "gun"'s for visuals
    #aim up and left
    if keys[pygame.K_UP] and keys[pygame.K_LEFT]:
        gun.endy = gun.starty
        gun.endy -= 64
        gun.endx = gun.startx
        gun.endx -= 64

        gun.up = True
        gun.down = False
        gun.left = True
        gun.right = False

    #aim up and right
    elif keys[pygame.K_UP] and keys[pygame.K_RIGHT]:
        gun.endy = gun.starty
        gun.endy -= 64
        gun.endx = gun.startx
        gun.endx += 64

        gun.up = True
        gun.down = False
        gun.left = False
        gun.right = True

    #aim down and left
    elif keys[pygame.K_DOWN] and keys[pygame.K_LEFT]:
        gun.endy = gun.starty
        gun.endy += 64
        gun.endx = gun.startx
        gun.endx -= 64

        gun.up = False
        gun.down = True
        gun.left = True
        gun.right = False

    # aim down and right
    elif keys[pygame.K_DOWN] and keys[pygame.K_RIGHT]:
        gun.endy = gun.starty
        gun.endy += 64
        gun.endx = gun.startx
        gun.endx += 64

        gun.up = False
        gun.down = True
        gun.left = False
        gun.right = True

    #aim up
    elif keys[pygame.K_UP]:
        gun.endx = gun.startx
        gun.endy = gun.starty
        gun.endy -= 64

        gun.up = True
        gun.down = False
        gun.left = False
        gun.right = False

    #aim down
    elif keys[pygame.K_DOWN]:
        gun.endx = gun.startx
        gun.endy = gun.starty
        gun.endy += 64

        gun.up = False
        gun.down = True
        gun.left = False
        gun.right = False

    #aim left
    elif keys[pygame.K_LEFT]:
        gun.endy = gun.starty
        gun.endx = gun.startx
        gun.endx -= 64

        gun.up = False
        gun.down = False
        gun.left = True
        gun.right = False

    #aim right
    elif keys[pygame.K_RIGHT]:
        gun.endy = gun.starty
        gun.endx = gun.startx
        gun.endx += 64
    
        gun.up = False
        gun.down = False
        gun.left = False
        gun.right = True

    #bullet creation
    if keys[pygame.K_SPACE] and shootLoop == 0:
        bullets.append(bulletThing(round(player.x + player.width / 2), round(player.y + player.height / 2), 6))

        #sets bullet movement to up, down, left, or right
        if gun.up:
            vertical = -1
        else:
            vertical = 0
        if gun.down:
            vertical = 1
        else:
            vertical = 0
        if gun.left:
            horizontal = -1
        else:
            horizontal = 0
        if gun.right:
            horizontal = 1
        else:
            horizontal = 0

        shootLoop = 1
    drawScreen(gameScreen)

pygame.quit()

子弹可以向右移动,所以我不知道有什么问题

对不起,如果我的代码难以理解并且缺乏信息,但是就像我在刚开始新手之前所说的那样,我很感谢您的帮助

I am new to pygame and am trying to make a little character with a gun, but the bullets won't shoot upwards or to the left

Here is my code:

import pygame
pygame.init()

gameScreen = pygame.display.set_mode()
pygame.display.set_caption('solo attempt')

clock = pygame.time.Clock()

class player(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 8
        self.facing = 1
    
    def draw(self, gameScreen):
        pygame.draw.rect(gameScreen, (255, 0, 0), (self.x, self.y, self.width, self.height))

class gun(object):
    def __init__(self, startx, starty):
        self.startx = startx
        self.starty = starty
        self.endx = self.startx
        self.endy = self.starty
        self.up = False
        self.down = False
        self.left = False
        self.right = False

    
    def draw(self, gameScreen):
        pygame.draw.line(gameScreen, (0, 0, 255), (self.startx, self.starty), (self.endx, self.endy))

class bulletThing(object):
    def __init__(self, x, y, radius):
        self.x = x
        self.y = y
        self.radius = radius
        self.verticalVel = 12 * vertical
        self.horizontalVel = 12 * horizontal

        print(self.verticalVel, self.horizontalVel)
    
    def draw(self, gameScreen):
        pygame.draw.circle(gameScreen, (0, 255, 0), (self.x, self.y), self.radius)

#updates screen
def drawScreen(gameScreen):
    gameScreen.fill((0, 0, 0))
    player.draw(gameScreen)
    gun.draw(gameScreen)
    for bullet in bullets:
        bullet.draw(gameScreen)
    pygame.display.update()

#create player and gun and bullets list
player = player(50, 50, 64, 64)
gun = gun(player.x + (player.width / 2), player.y + (player.height / 2))
bullets = []

vertical = 0
horizontal = 1

shootLoop = 0
dimensions = gameScreen.get_size()

#main loop
running = True
while running:
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    #shot delay
    if shootLoop > 0:
        shootLoop += 1
    if shootLoop > 3:
        shootLoop = 0

    #bullet movement
    for bullet in bullets:
        if bullet.x < dimensions[0] and bullet.x > 0:
            bullet.x += bullet.horizontalVel
        else:
            bullets.pop(bullets.index(bullet))

        if bullet.y < dimensions[1] and bullet.y > 0:
            bullet.y += bullet.verticalVel
        else:
            bullets.pop(bullets.index(bullet))


    keys = pygame.key.get_pressed()

    #move left
    if keys[pygame.K_a]:
        if player.x > 0:
            player.x -= player.vel
        if gun.startx > 0 + player.width / 2:
            gun.startx -= player.vel
            gun.endx -= player.vel

    #move right
    if keys[pygame.K_d]:
        if player.x < dimensions[0] - player.width:
            player.x += player.vel
        if gun.startx < dimensions[0] - player.width / 2:
            gun.startx += player.vel
            gun.endx += player.vel

    #move up
    if keys[pygame.K_w]:
        if player.y > 0 + player.height / 2:
            player.y -= player.vel
        if gun.starty > 0:
            gun.starty -= player.vel
            gun.endy -= player.vel

    #move down
    if keys[pygame.K_s]:
        if player.y < dimensions[1] - player.height:
            player.y += player.vel
        if gun.starty < dimensions[1] - player.height / 2:
            gun.starty += player.vel
            gun.endy += player.vel

#top "gun"'s for visuals
    #aim up and left
    if keys[pygame.K_UP] and keys[pygame.K_LEFT]:
        gun.endy = gun.starty
        gun.endy -= 64
        gun.endx = gun.startx
        gun.endx -= 64

        gun.up = True
        gun.down = False
        gun.left = True
        gun.right = False

    #aim up and right
    elif keys[pygame.K_UP] and keys[pygame.K_RIGHT]:
        gun.endy = gun.starty
        gun.endy -= 64
        gun.endx = gun.startx
        gun.endx += 64

        gun.up = True
        gun.down = False
        gun.left = False
        gun.right = True

    #aim down and left
    elif keys[pygame.K_DOWN] and keys[pygame.K_LEFT]:
        gun.endy = gun.starty
        gun.endy += 64
        gun.endx = gun.startx
        gun.endx -= 64

        gun.up = False
        gun.down = True
        gun.left = True
        gun.right = False

    # aim down and right
    elif keys[pygame.K_DOWN] and keys[pygame.K_RIGHT]:
        gun.endy = gun.starty
        gun.endy += 64
        gun.endx = gun.startx
        gun.endx += 64

        gun.up = False
        gun.down = True
        gun.left = False
        gun.right = True

    #aim up
    elif keys[pygame.K_UP]:
        gun.endx = gun.startx
        gun.endy = gun.starty
        gun.endy -= 64

        gun.up = True
        gun.down = False
        gun.left = False
        gun.right = False

    #aim down
    elif keys[pygame.K_DOWN]:
        gun.endx = gun.startx
        gun.endy = gun.starty
        gun.endy += 64

        gun.up = False
        gun.down = True
        gun.left = False
        gun.right = False

    #aim left
    elif keys[pygame.K_LEFT]:
        gun.endy = gun.starty
        gun.endx = gun.startx
        gun.endx -= 64

        gun.up = False
        gun.down = False
        gun.left = True
        gun.right = False

    #aim right
    elif keys[pygame.K_RIGHT]:
        gun.endy = gun.starty
        gun.endx = gun.startx
        gun.endx += 64
    
        gun.up = False
        gun.down = False
        gun.left = False
        gun.right = True

    #bullet creation
    if keys[pygame.K_SPACE] and shootLoop == 0:
        bullets.append(bulletThing(round(player.x + player.width / 2), round(player.y + player.height / 2), 6))

        #sets bullet movement to up, down, left, or right
        if gun.up:
            vertical = -1
        else:
            vertical = 0
        if gun.down:
            vertical = 1
        else:
            vertical = 0
        if gun.left:
            horizontal = -1
        else:
            horizontal = 0
        if gun.right:
            horizontal = 1
        else:
            horizontal = 0

        shootLoop = 1
    drawScreen(gameScreen)

pygame.quit()

The bullets can move to the right and downwards just fine so I have no idea what's wrong

Sorry if my code is hard to understand and for the lack of information but like I said before I'm new to pygame and I would appreciate the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃安静 2025-02-19 22:25:23

如果 - ELIF - else语句,而不是2 如果 - 如果 - 如果 - - - else 语句语句语句:

while running:
    # [...]

    if keys[pygame.K_SPACE] and shootLoop == 0:
        
        #sets bullet movement to up, down, left, or right
        if gun.up:
            vertical = -1
        elif gun.down:
            vertical = 1
        else:
            vertical = 0
        
        if gun.left:
            horizontal = -1
        elif gun.right:
            horizontal = 1
        else:
            horizontal = 0
        
        bullets.append(bulletThing(round(player.x + player.width / 2), round(player.y + player.height / 2), 6))
        shootLoop = 1

但是,您可以很大的代码:

while running:
    # [...]

    if keys[pygame.K_SPACE] and shootLoop == 0:
        vertical = gun.down - gun.up
        horizontal = gun.right - gun.left        
        bullets.append(bulletThing(round(player.x + player.width / 2), round(player.y + player.height / 2), 6))
        shootLoop = 1

Use a if-elif-else statement instead of 2 if-else statements:

while running:
    # [...]

    if keys[pygame.K_SPACE] and shootLoop == 0:
        
        #sets bullet movement to up, down, left, or right
        if gun.up:
            vertical = -1
        elif gun.down:
            vertical = 1
        else:
            vertical = 0
        
        if gun.left:
            horizontal = -1
        elif gun.right:
            horizontal = 1
        else:
            horizontal = 0
        
        bullets.append(bulletThing(round(player.x + player.width / 2), round(player.y + player.height / 2), 6))
        shootLoop = 1

However, you can greatly the code:

while running:
    # [...]

    if keys[pygame.K_SPACE] and shootLoop == 0:
        vertical = gun.down - gun.up
        horizontal = gun.right - gun.left        
        bullets.append(bulletThing(round(player.x + player.width / 2), round(player.y + player.height / 2), 6))
        shootLoop = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文