简体   繁体   中英

How do I get my character to move while also getting bullets to spawn above them?

I've been trying to make a bullet hell (dankamu) game in Python, and when I tried to animate the movements of the character with the bullets, I encountered some issues. Depending on where I put my redrawGameWindow() function, either the bullets fail to spawn or my game character cannot move at all. I think it might be because of the fact that my movement function is separate from the for loop I used for the bullets? If that's the case, I don't know how to amalgamate them together in order to run both simultaneously.

class mc(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.hitbox = (self.x, self.y, 30, 30)
    def draw(self, win):
        win.blit(char, (self.x, self.y))
        self.hitbox = (self.x, self.y, 30, 30)
        pygame.draw.rect(win, (255,0,0), self.hitbox,2)
    def hit(self):
        print("Hit")
def redrawGameWindow(mainchar, bullet, bullets):
    win.blit(bg, (0,0))
    mainchar.draw(win)
    pygame.display.update()
    for bullet in bullets:
        bullet.draw(win)
class projectile(object):
    def __init__(self,x,y,radius,color,facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing
    def draw(self,win):
        pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
def dankamu(enemy):
    mainchar = mc(200, 410, 64, 64)
    run = True
    bullets = []
    while run:
        pygame.time.delay(10)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and mainchar.x > 0:
            mainchar.x -= mainchar.vel
        if keys[pygame.K_RIGHT] and mainchar.x < 470:
            mainchar.x += mainchar.vel
        if keys[pygame.K_DOWN] and mainchar.y < 470:
            mainchar.y += mainchar.vel
        if keys[pygame.K_UP] and mainchar.y > 0:
            mainchar.y -= mainchar.vel
        if keys[pygame.K_LSHIFT] and mainchar.vel > 0:
            mainchar.vel = 3
        if keys[pygame.K_RSHIFT]:
            mainchar.vel = 5
        win.fill((255, 255, 255))
        if enemy == "skeleton":
            for i in range (30):
                bullets.append(projectile(250, 250, 6, (0, 0, 0), 1))
            for bullet in bullets:
                if bullet.y - bullet.radius < mainchar.hitbox[1] + mainchar.hitbox[3] and bullet.y + bullet.radius > mainchar.hitbox[1]:
                    if bullet.x + bullet.radius > mainchar.hitbox[0] and bullet.x - bullet.radius < mainchar.hitbox[0] + mainchar.hitbox[2]:
                        mainchar.hit()
                        bullets.pop(bullets.index(bullet))
                elif bullet.x < 500 and bullet.x > 0:
                    bullet.x += bullet.vel
                else:
                    bullets.pop(bullets.index(bullet))
                redrawGameWindow(mainchar, bullet, bullets)
            run = False


    pygame.quit()


#Actual Running
dankamu("skeleton")
print("Dankamu success")

When you spawn a bullet, you must spawn the bullet above the palyer's location:

bullets.append(projectile(250, 250, 6, (0, 0, 0), 1))

bullets.append(projectile(mainchar.x, 0, 6, (0, 0, 0), 1))

Since the bullet has to move from the bottom to the top, you have to increment the y coordinate of the bullet:

bullet.x += bullet.vel

bullet.y += bullet.vel

Draw the bullets before you update the display:

def redrawGameWindow(mainchar, bullet, bullets):
    win.blit(bg, (0,0))
    mainchar.draw(win)
    for bullet in bullets:
        bullet.draw(win)
    pygame.display.update()

Do not add all the bullets at once:

if bullet_frame_count == 0:
    bullet_frame_count = 10
if len(bullets) < 30:
    bullets.append(projectile(mainchar.x, 0, 6, (0, 0, 0), 1))
else:
    bullet_frame_count -= 1

Use pygame.Rect.colliderect for the collision test:

bullet_rect = pygame.Rect(bullet.x-bullet.radius, bullet.y-bullet.radius, bullet.radius*2,bullet.radius*2)
if bullet_rect.colliderect(mainchar.hitbox):
    mainchar.hit()
    bullets.pop(bullets.index(bullet))

Please read How to remove items from a list while iterating?


Complete dankamu function:

def dankamu(enemy):
    mainchar = mc(200, 410, 64, 64)
    run = True
    bullets = []
    count = 0
    while run:
        pygame.time.delay(10)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys = pygame.key.get_pressed()
        mainchar.x = mainchar.x + (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * mainchar.vel
        mainchar.y = mainchar.y + (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * mainchar.vel
        mainchar.x = max(0, min(470, mainchar.x))
        mainchar.y = max(0, min(470, mainchar.y))
        if keys[pygame.K_LSHIFT] and mainchar.vel > 0:
            mainchar.vel = 3
        if keys[pygame.K_RSHIFT]:
            mainchar.vel = 5
        
        if enemy == "skeleton":
            if count == 0:
                count = 10
                if len(bullets) < 30:
                    bullets.append(projectile(mainchar.x, 0, 6, (0, 0, 0), 1))
            else:
                count -= 1
            for bullet in bullets[:]:
                bullet_rect = pygame.Rect(bullet.x-bullet.radius, bullet.y-bullet.radius, bullet.radius*2,bullet.radius*2)
                if bullet_rect.colliderect(mainchar.hitbox):
                    mainchar.hit()
                    bullets.pop(bullets.index(bullet))
                elif bullet.y < 500:
                    bullet.y += bullet.vel
                else:
                    bullets.pop(bullets.index(bullet))

        redrawGameWindow(mainchar, bullet, bullets)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM