简体   繁体   English

如何让我的角色移动,同时让子弹在他们上方产生?

[英]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.我一直在尝试在 Python 中制作子弹地狱(dankamu)游戏,当我尝试用子弹动画角色的动作时,我遇到了一些问题。 Depending on where I put my redrawGameWindow() function, either the bullets fail to spawn or my game character cannot move at all.根据我将redrawGameWindow() function 放在哪里,子弹无法生成或我的游戏角色根本无法移动。 I think it might be because of the fact that my movement function is separate from the for loop I used for the bullets?我认为这可能是因为我的运动 function 与我用于子弹的 for 循环是分开的? 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:由于子弹必须从底部移动到顶部,因此您必须增加子弹的 y 坐标:

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:使用pygame.Rect.colliderect进行碰撞测试:

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:完整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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在将json转换为tsv的过程中(“ \\ t”是我的delimineter),值中有换行符,我该如何摆脱它们 - While converting json to tsv( “\t” is my delimineter) there are new line character in values, How do i get rid of them 如何让玩家角色移动? - How do I get the player character to move? 如何获得在远程服务器上生成进程的悲哀? - How do I get pathos to spawn processes on my remote server? 如何同时运行两个不定循环,同时还要更改其中的变量? - How do I run two indefinite loops simultaneously, while also changing variables within them? 我无法让我的“玩家”在pygame中同时移动,我该怎么做? - I can't get my 'player' to move to move both ways in pygame, how do i do it? 如何通过按住按钮让pygame中的对象移动? - How do I get my object in pygame to move by holding the button? 为什么我的if语句没有得到正确的答案? 我也如何找到所有输入的平均值? - why am i not getting the right answer for my if statements? also how do i find the average of all the inputs? 为什么我的pygame精灵不独立移动? 我如何让他们独立地走动呢? - Why don't my pygame sprites move about independently? And how do I make them move about independently? 如果我的 pygame 角色动画开始时其中一些尺寸不同,如何使它们的尺寸相同? - How do I make my pygame character animations the same size if some of them are different sizes to start with? pygame - while 循环使 pygame 窗口冻结/如何将子弹射出玩家? - pygame - while loop makes pygame window freeze / how do I shoot the bullets out of the player?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM