【问题标题】:Bullet not moving upwards because of OOP由于 OOP,子弹没有向上移动
【发布时间】:2020-11-02 09:35:46
【问题描述】:

我有一个使用 pygame 的简单射击游戏。我在使子弹的 y 坐标缓慢增加时遇到了一些问题。我知道这与我编写 Player 类的方式有关,即使其中有一个子弹 Rect。我想我必须更改其中的更新功能。这是我的代码:

import pygame, random, sys, time

pygame.init()

#Constants
WIDTH = 800
HEIGHT = 500

BLACK = (0, 0, 0)
WHITE = (255, 255, 255) # Background Colour
RED = (255, 0, 0)
GREEN = (0, 255, 0)

window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame Shooter Game")

clock = pygame.time.Clock()
fps = 60

run = True

class Player():
    def __init__(self, width, colour, x, y):
        self.width = width
        self.colour = colour
        self.x = x
        self.y = y
        self.vel = 5
        self.shoot = False
        self.player = pygame.Rect(self.x, self.y, self.width, self.width)
        self.cartridge = pygame.Rect(0, 0, self.width/2, self.width/2)
        self.bullet = pygame.Rect(0, 0, 10, 20)
        self.shoot = False

    def draw(self, win):
        self.win = win
        pygame.draw.rect(self.win, self.colour, self.player) # Draw player(rect)
        pygame.draw.rect(self.win, GREEN, self.cartridge) #Draw cartridge
        if self.shoot:
            pygame.draw.rect(self.win, BLACK, self.bullet)
        
    def move(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT] and self.x > 0: self.x -= self.vel # We don't do elif cuz we want them to be able to move diagonally
        if keys[pygame.K_RIGHT] and self.x < WIDTH-self.width: self.x += self.vel
        if keys[pygame.K_UP] and self.y > 0: self.y -= self.vel
        if keys[pygame.K_DOWN] and self.y < HEIGHT-self.width: self.y += self.vel

        if keys[pygame.K_SPACE]:
            self.shoot = True
    
    def update(self):
        self.player = pygame.Rect(self.x, self.y, self.width, self.width)
        self.cartridge.midbottom = self.player.midtop
        self.bullet.midbottom = self.cartridge.midtop

        if self.shoot:
            while self.bullet.y > 0:
                self.bullet.y -= 1

def main(win):
    run = True 

    player = Player(50, RED, WIDTH/2, HEIGHT/2)

    while run:
        win.fill(WHITE)
        clock.tick(fps)

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

        player.move()
        player.update()
        player.draw(win)
        pygame.display.update()

    pygame.quit()
    sys.exit()

main(window)

另外,我怎样才能为每个单独的 Cartridge 和 Bullet 创建类,以使整个代码更高效?

【问题讨论】:

    标签: python oop pygame


    【解决方案1】:

    update 在主应用程序循环中连续调用。因此,更新不需要额外的动画循环。将循环更改为选择(将while更改为if):

    while self.bullet.y &gt; 0:

    if self.bullet.y > 0:
        self.bullet.y -= 1
    

    子弹的起始位置必须在子弹发射时设置,而不是在子弹更新时连续设置:

    class Player():
        # [...]
    
        def move(self):
            # [...]
    
            if keys[pygame.K_SPACE]:
                self.shoot = True
                self.bullet.midbottom = self.cartridge.midtop # <--- INSERT
    
        def update(self):
            self.player = pygame.Rect(self.x, self.y, self.width, self.width)
            self.cartridge.midbottom = self.player.midtop
            # self.bullet.midbottom = self.cartridge.midtop     <--- DELETE
            
            if self.shoot:
                if self.bullet.y > 0:                         # <--- if (not while)
                    self.bullet.y -= 1
    

    另见:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多