Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有敌人向玩家射击的问题吗?皮加梅_Python_Pygame - Fatal编程技术网

Python 有敌人向玩家射击的问题吗?皮加梅

Python 有敌人向玩家射击的问题吗?皮加梅,python,pygame,Python,Pygame,我试图让我的敌人射杀我的球员,但出于某种原因,子弹没有出现,也没有射向球员,我甚至不知道为什么,我甚至把子弹画在我的窗户上 我的敌人炮弹班 # enemys bullets class enemybullets(object): def __init__(self,x,y,radius,color,facing): self.x = x self.y = y self.radius = radius self.color =

我试图让我的敌人射杀我的球员,但出于某种原因,子弹没有出现,也没有射向球员,我甚至不知道为什么,我甚至把子弹画在我的窗户上

我的敌人炮弹班

# 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()
导入pygame
pygame.init()
win=pygame.display.set_模式((800480))
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_标题(“我的武士(v1.0)”)
x、 y=50,50
bx,by=250250
p_宽度,p_高度=40,50
b_宽度,b_高度=40,50
跳数=5
步行次数=0
类别项目符号:
定义初始速度(自、x、y、表面、移动速度):
self.x=x
self.y=y
self.surface=曲面
self.mov_速度=mov_速度
def牵引(自):
pygame.draw.circle(self.surface,(200,20,20),(self.x,self.y),3,1)
self.x+=self.mov_速度
班级人数:
定义初始(自、x、y、宽度、高度、表面):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.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_跳跃(自):
如果self.jump:
打印(“我来过这里”)
如果self.jumpCount>0:
self.y-=int((self.jumpCount**2)/2)*6
self.jumpCount-=1
打印(“我在这里1”,self.jumpCount,“y pos”,self.y)
其他:
self.y+=int((self.jumpCount**2)/2)*6
self.jumpCount-=1
打印(“我来这里2”,self.jumpCount)
如果self.jumpCount==-6:
self.jumpCount=5
self.jump=False
打印(跳转)
def绘图(自身、dirL、dirR、曲面、arrayL、arrayR):
#pygame.draw.rect(self.surface,(255,0,0),(self.hitbox[0],self.hitbox[1],self.hitboxWidth,self.hitboxHeight),1)
如果dirL:
如果self.move:
blit(arrayL[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
其他:
blit(arrayL[self.walkCount//3],(self.x,self.y))
如果dirR:
如果self.move:
blit(arrayR[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
其他:
blit(arrayR[self.walkCount//3],(self.x,self.y))
如果self.walkCount>self.maxCount:
self.walkCount=0
速度=5
跳转=错误
运行=真
左=假
右=真
玩家=数字(x,y,p_宽度,p_高度,胜利)
beast=数字(bx、by、b_宽度、b_高度、win)
player.maxCount=24
beast.maxCount=30
beast.move=True
player.hitboxWidth=42
beast.hitboxWidth=42
正文=[]
i=0
bSpeed=8
分数=0
score\u font=pygame.font.SysFont('arial',20,1,1)
hit_font=pygame.font.SysFont('calibri',80,1,1)
运行时:
pygame。时间。延迟(50)
pygame.display.update()
时钟滴答作响(27)
bg=pygame.image.load('bg.jpg')
赢。布利特(背景,(0,0))
score\u display=score\u font.render(“分数:{}”。格式(分数),1,(0,0,0))
win.blit(分数显示,(700,10))
keys=pygame.key.get_pressed()
如果键[pygame.K_LEFT]和player.x>0:
player.x-=速度
左=真
右=假
player.move=True
bSpeed=-8
elif键[pygame.K_RIGHT]和player.x+p_width+speed+5<800:
player.x+=速度
左=假
右=真
player.move=True
bSpeed=8
其他:
player.move=False
player.walkCount=0
如果键[pygame.K_UP]和player.y>0且未跳转:
player.y-=速度
如果键[pygame.K_DOWN]和player.y+p_heig
# 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()