python - 有敌人射击玩家问题吗?游戏

标签 python pygame

我试图让我的敌人射击我的玩家,但由于某种原因,子弹没有显示,也没有向玩家射击我什至不知道为什么,我什至在我的 window 上画了子弹 VIDEO

bulls = []
runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False

    for bull in bulls:
        if bull.x < 500 and bull.x > 0:
            bull.x += bull.vel  # Moves the bullet by its vel
        else:
            for enemys in enemying:
                if playerman.x - 100 <= enemys.x <= playerman.x + 100 and playerman.y - 100 <= enemys.y <= playerman.y + 100:
                    bulls.pop(bulls.index(bull))
                    bulls.append(enemybullets(round(enemys.x+enemys.width//2), round(enemys.y + enemys.height//2), 6, (0,0,0), facing)) 


然后我在下面调用它

    for bull in bulls:
        bull.draw(window)

我的敌人射弹类

# enemys bullets
class enemybullets(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)



最佳答案

这是我前一段时间在教程的帮助下制作的一个非常相似的游戏的代码。

我的第一个建议是尝试降低子弹的速度。这样您就可以确保不会遗漏屏幕上的任何内容。

接下来确保您正确更新屏幕内容。

如果不查看您的完整代码,很难给出全面的答案。我已将我的代码包含在此处,以便您可以引用并解决问题。

希望这对您有所帮助。

import pygame

pygame.init()

win = pygame.display.set_mode((800, 480))

clock = pygame.time.Clock()

char = pygame.image.load('standing.png')

bulletSound = pygame.mixer.Sound('gshot.wav')
shotHit = pygame.mixer.Sound('bounce.wav')
backSound = pygame.mixer.music.load('music.mp3')
pygame.mixer.music.play(-1)


walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
            pygame.image.load('L4.png'), pygame.image.load('L5.png'),
            pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'),
            pygame.image.load('L9.png')]

walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
             pygame.image.load('R4.png'), pygame.image.load('R5.png'),
             pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'),
             pygame.image.load('R9.png')]

b_walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
            pygame.image.load('L4E.png'), pygame.image.load('L5E.png'),
            pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'),
            pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
b_walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'),
             pygame.image.load('R4E.png'), pygame.image.load('R5E.png'),
             pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'),
             pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]

pygame.display.set_caption("My Samurai(v1.0)")

x, y = 50, 50
bx, by = 250, 250
p_width, p_height = 40, 50
b_width, b_height = 40, 50

jumpCount = 5
walkCount = 0


class bullets:

    def __init__(self, x, y, surface, mov_speed):
        self.x = x
        self.y = y
        self.surface = surface
        self.mov_speed = mov_speed

    def draw(self):
        pygame.draw.circle(self.surface, (200, 20, 20), (self.x, self.y), 3, 1)
        self.x += self.mov_speed






class figure:

    def __init__(self, x, y, width, height, surface):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.surface = surface
        self.jumpCount = 5
        self.jump = False
        self.walkCount = 0
        self.move = False
        self.maxCount = 0
        self.hitboxWidth = 55
        self.hitboxHeight = 55
        self.hitbox = [self.x, self.y, self.hitboxWidth, self.hitboxHeight]

    def p_jump(self):
        if self.jump:
            print("i came here")
            if self.jumpCount > 0:
                self.y -= int((self.jumpCount ** 2) / 2) * 6
                self.jumpCount -= 1
                print("im here 1", self.jumpCount, "y pos", self.y)
            else:
                self.y += int((self.jumpCount ** 2) / 2) * 6
                self.jumpCount -= 1
                print("i came here 2", self.jumpCount)

            if self.jumpCount == -6:
                self.jumpCount = 5
                self.jump = False
                print(jump)

    def draw(self, dirL, dirR, surface, arrayL, arrayR):
        #pygame.draw.rect(self.surface, (255, 0, 0), (self.hitbox[0], self.hitbox[1], self.hitboxWidth, self.hitboxHeight), 1)
        if dirL:
            if self.move:
                surface.blit(arrayL[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            else:
                surface.blit(arrayL[self.walkCount // 3], (self.x, self.y))

        if dirR:
            if self.move:
                surface.blit(arrayR[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            else:
                surface.blit(arrayR[self.walkCount // 3], (self.x, self.y))

        if self.walkCount > self.maxCount:
            self.walkCount = 0


speed = 5
jump = False
run = True
left = False
right = True
player = figure(x, y, p_width, p_height, win)
beast = figure(bx, by, b_width, b_height, win)
player.maxCount = 24
beast.maxCount = 30
beast.move = True
player.hitboxWidth = 42
beast.hitboxWidth = 42
body = []
i = 0
bSpeed = 8
score = 0
score_font = pygame.font.SysFont('arial', 20, 1, 1)
hit_font = pygame.font.SysFont('calibri', 80, 1, 1)
while run:
    pygame.time.delay(50)
    pygame.display.update()
    clock.tick(27)
    bg = pygame.image.load('bg.jpg')
    win.blit(bg, (0, 0))
    score_display = score_font.render("Score: {}".format(score), 1, (0, 0, 0))
    win.blit(score_display, (700, 10))
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player.x > 0:
        player.x -= speed
        left = True
        right = False
        player.move = True
        bSpeed = -8
    elif keys[pygame.K_RIGHT] and player.x + p_width + speed + 5 < 800:
        player.x += speed
        left = False
        right = True
        player.move = True
        bSpeed = 8
    else:
        player.move = False
        player.walkCount = 0

    if keys[pygame.K_UP] and player.y > 0 and not jump:
        player.y -= speed
    if keys[pygame.K_DOWN] and player.y + p_height + speed + 5 < 480 and not jump:
        player.y += speed
    if keys[pygame.K_SPACE]:
        player.jump = True
    if keys[pygame.K_s] and i <= 0:
        body.append(bullets(player.x + 32, player.y + 40, win, bSpeed))
        i = 4
        bulletSound.play()
    for bullet in body:
        bullet.draw()
        if beast.x + 25 < bullet.x < beast.x + beast.hitboxWidth:
            if beast.y < bullet.y < beast.y + beast.hitboxHeight:
                body.pop(body.index(bullet))
                score += 1
                shotHit.play()


    # beast position cal
    if beast.x - player.x > 0:
        b_right = False
        b_left = True
        beast.x -= int((beast.x - player.x) * 0.025)
    else:
        b_right = True
        b_left = False
        beast.x -= int((beast.x - player.x) * 0.025)

    if beast.y - player.y > 0:
        beast.y -= int((beast.y - player.y) * 0.025)
    else:
        beast.y -= int((beast.y - player.y) * 0.025)

    if i > 0:
        i -= 1

    player.p_jump()
    player.draw(left, right, win, walkLeft, walkRight)
    beast.draw(b_left, b_right, win, b_walkLeft, b_walkRight)

    player.hitbox = [player.x + 10, player.y + 8, player.hitboxWidth, player.hitboxHeight]
    beast.hitbox = [beast.x + 10, beast.y + 5, beast.hitboxWidth, beast.hitboxHeight]

    if (player.hitbox[0] < beast.hitbox[0] < player.hitbox[0] + player.hitbox[2] - 20) or\
            (player.hitbox[0] + 15 < beast.hitbox[0] + beast.hitbox[2] <= player.hitbox[0] + player.hitbox[2]):
        if (player.hitbox[1] < beast.hitbox[1] < player.hitbox[1] + player.hitbox[3] - 10) or\
                (player.hitbox[1] + 10 < beast.hitbox[1] + beast.hitbox[3] < player.hitbox[1] + player.hitbox[3]):
            score -= 5
            hit_display = hit_font.render("-5", 1, (255, 0, 0))
            win.blit(hit_display, (400, 200))
            pygame.display.update()
            pygame.time.delay(1000)
            player.x, player.y = 100, 100
            beast.x, beast.y = 600, 300
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False



pygame.quit()

关于python - 有敌人射击玩家问题吗?游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62107874/

相关文章:

Python 创建矩阵并根据列表值选择行

python - 检测pygame中文本框和圆圈之间的碰撞

python - unident 不匹配任何外部缩进级别,我需要更改什么?

python - 安装 64 位 pygame 以使用 64 位 kivy

Python3 内存游戏随机洗牌图像

python - 有没有一种不同的方式可以根据使用 argparse 模块输入的参数显示我的表?

python - 如何在 Django 网页上获取 Facebook Places(签到)?

python - 为什么具有相同值的字符串引用相同的对象,而列表则不然?

python - Heroku 上的 django Whitenoise 静态文件在本地不可用

python - 在python 3中使用pygame移动屏幕的一部分