当前位置: 首页 > 知识库问答 >
问题:

游戏运行时看不到子弹

秦昂然
2023-03-14

你好,所以我只能看到子弹,当我关闭我的游戏的窗口,但我没有看到错误。导入py游戏,数学,随机,操作系统pygame.init()时钟=pygame.time.时钟()宽度=800高度=608屏幕=pygame.display.set_mode((宽度,高度))运行=真正的游戏结束=假水平=假水平=0费迪格=假跳转=假

levels =[[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
         [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
         [1,0,8,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
         [1,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1],
         [1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1],
         [1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1],
         [1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1],
         [1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0],
         [1,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0],
         [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0],
         [1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0],
         [1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0],
         [1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
         [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,7,0,0,0],
         [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
         [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0],
         [1,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
         [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
         [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]],
        [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
         [0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
         [9,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,1],
         [0,2,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1],
         [0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1],
         [0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1],
         [0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0],
         [0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0],
         [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0],
         [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0],
         [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0],
         [0,1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
         [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
         [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0],
         [0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,1,1]]]
       
ghostgruppe = pygame.sprite.Group()
tilegruppe = pygame.sprite.Group()
utganggruppe = pygame.sprite.Group()
backgroundgruppe_1 = pygame.sprite.Group()
backgroundgruppe_2 = pygame.sprite.Group()
moving_sprites = pygame.sprite.Group()
spillergruppe = pygame.sprite.Group()


bullet_img = pygame.transform.scale(pygame.image.load(os.path.join('Bullets', 'bullet.png.png')), (10,10))
background_image_2 = pygame.image.load('skog.png').convert()
background_image_1 = pygame.image.load("beach.png").convert()
pygame.display.set_caption('pygame') #setter navnet på spillet når du åpner spillet
icon = pygame.image.load('menneske.png') 
pygame.display.set_icon(icon)

font = pygame.font.Font('freesansbold.ttf', 72 ) #font som skal brukes
gameovertekst = font.render('Game over', True, (255,0,0)) #farge og tekst gameover
gameoverrect = gameovertekst.get_rect()
gameoverrect.center = (width/2, height/2)

font = pygame.font.Font('freesansbold.ttf', 72 ) #font som skal brukes
levelferdigtekst = font.render('Ferdig', True, (0,255,0)) #farge og tekst gameover
levelferdigrect = levelferdigtekst.get_rect()
levelferdigrect.center = (width/2, height/2)

ferdigtekst = font.render('Victory', True, (0,0,255))
ferdigrect = levelferdigtekst.get_rect()
ferdigrect.center = (width/2, height/2)


class Spiller(pygame.sprite.Sprite):
    def __init__(self,x,y):
        super().__init__()
        self.spiller_animation = False
        self.sprites = []
        self.sprites.append(pygame.image.load('spiller.rett(1).png'))
        self.sprites.append(pygame.image.load('spiller.høyre(1).png'))
        self.sprites.append(pygame.image.load('spiller.venstre(1).png'))
        self.sprites.append(pygame.image.load('spiller.hopp(1).png'))
        self.current_sprite = 0
        self.image = self.sprites[self.current_sprite]
        self.rect = self.image.get_rect()
        self.rect.topleft =[x,y]
        self.bullets = [] #list
        self.x = x
        self.y = y
        self.stepIndex = 0
        self.face_right = True
        self.face_left = False
        self.fy = 10
        self.fx = 10
        self.jump = False


    def direction(self):
        if self.face_right:
            return 1
        if self.face_left:
            return -1
        
    def animation(self):
        self.spiller_animation = True

        
    def skytt(self):
        if  trykketliste[pygame.K_RIGHT] and not gameover:
            bullet = Bullet(self.x, self.y, self.direction())
            self.bullets.append(bullet)
        for bullet in self.bullets:
            bullet.move()
            
    def reset(self):
        self.fartx = 0
        self.farty = 0
 
    def update(self,speed):
        if self.spiller_animation == True:
                self.current_sprite += speed
                if int(self.current_sprite) >= len(self.sprites):
                        self.current_sprite = 0
                        self.is_animating = False
            
        self.image = self.sprites[int(self.current_sprite)]
        
      
        self.rect.x += self.fartx
        tiletreffliste = pygame.sprite.spritecollide(self,tilegruppe, False)
        if tiletreffliste:
           if self.fartx > 0:
               self.rect.right = tiletreffliste[0].rect.left
           else: 
               self.rect.left = tiletreffliste[0].rect.right

         
        self.rect.y += self.farty
        tiletreffliste = pygame.sprite.spritecollide(self, tilegruppe, False)
        if tiletreffliste:
           if self.farty > 0:
              self.rect.bottom = tiletreffliste[0].rect.top
           else:
              self.rect.top = tiletreffliste[0].rect.bottom
            
           utgangtreffliste = pygame.sprite.spritecollide(self, utganggruppe, False)
           if utgangtreffliste:
              global levelferdig
              levelferdig = True
              global levelnr
              levelnr += 1
              restart()
              
class Bullet:
    def __init__(self,x,y, direction):
        super().__init__()
        self.x = x + 15
        self.y = y + 25
        self.direction = direction
    
    def draw_bullet(self):
        screen.blit(bullet_img, (self.x, self.y))
        
    def move(self):
        if self.direction == 1:
            self.x += 15
        if self.direction == -1:
            self.x -= 15
            
class Ghost(pygame.sprite.Sprite):
    def __init__(self, x, y, fx,fy):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('ghost.png').convert_alpha()
        self.rect = self.image.get_rect() #self rect rektangler, fin rektangel bilde som passer
        self.rect.x = x
        self.rect.y = y
        self.fartx = fx
        self.farty = fy 

    def flytt(self):
        self.rect.x = self.rect.x + self.fartx
        self.rect.y = self.rect.y + self.farty
        
    def update(self):
        # Flytter høyre/venstre og sjekker kollisjon
        self.rect.x += self.fartx
        tiletreffliste = pygame.sprite.spritecollide(self,tilegruppe, False)
        if tiletreffliste:
           if self.fartx > 0:
               self.rect.right = tiletreffliste[0].rect.left
           else: 
               self.rect.left = tiletreffliste[0].rect.right

           self.fartx *= -1

        self.rect.y += self.farty
        tiletreffliste = pygame.sprite.spritecollide(self, spillergruppe, False)
        if tiletreffliste:
            if self.farty > 0:
                self.rect.bottom = tiletreffliste[0].rect.top
            else:
                self.rect.top = tiletreffliste[0].rect.bottom

            self.farty *= -1
            
              
    def treffTile(self):
        if self.rect.x > width or self.rect.x < 0:
            self.fartx = self.fartx * -1

        if self.rect.y > height or self.rect.y < 0:
            self.farty = self.farty * -1
            
class Tile(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('sand.png').convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        
class Utgang(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('dør.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        
def restart():
    global gameover
    global levelferdig

    gameover = False
    levelferdig = False


    for tile in tilegruppe:
        tile.kill()
    for utgang in utganggruppe:
        utgang.kill()
    for ghost in ghostgruppe:
        ghost.kill()
    spiller.reset() 

    for i in range(5): #hvor mange ghosts skal vi ha
        ghost = Ghost(random.randint(0,width),random.randint(0, height), random.randint(1,8),random.randint(1,8)) 
        ghostgruppe.add(ghost)


    if levelnr < len(levels):
       for y,rad in enumerate(levels[levelnr]):
           for x,verdi in enumerate(rad):
               if verdi == 1:
                  tile = Tile(x*32,y*32)
                  tilegruppe.add(tile)
               if verdi == 9:
                  utgang = Utgang(x*32, y*32)
                  utganggruppe.add(utgang)
               if verdi == 8:
                  spiller.rect.x = x*32

                  spiller.rect.y = y*32
    else:     
        global ferdig
        
        ferdig = True

#skytt
spiller = Spiller(100, 100)
moving_sprites.add(spiller)
restart()


while running:
    pygame.display.bullet()
    spiller.farty = 0
    spiller.fartx = 0
    trykketliste = pygame.key.get_pressed()
    spiller.skytt()
    background_image_1 = pygame.image.load("beach.png").convert()
    screen.blit(background_image_1, [0,0])

    if trykketliste[pygame.K_RIGHT] and not gameover:
        spiller.farty = -1
        spiller.sprites.append(pygame.image.load('spiller.venstre(1).png'))
        spiller.animation()
        spiller.skytt() 
    if trykketliste[pygame.K_LEFT] and not gameover:
        spiller.farty = 1
        spiller.sprites.append(pygame.image.load('spiller.høyre(1).png'))
        spiller.animation()
    if trykketliste[pygame.K_UP] and not gameover:
        spiller.fartx = -1
        spiller.animation()
        spiller.sprites.append(pygame.image.load('spiller.hopp(1).png'))
    if trykketliste[pygame.K_DOWN]and not gameover:
        spiller.fartx = 1
        spiller.animation()
        spiller.sprites.append(pygame.image.load('spiller.rett(1).png'))
    if jump is False and trykketliste[pygame.K_SPACE] and not gameover:
       jump = True
       spiller.animation()
       spiller.sprites.append(pygame.image.load('spiller.hopp(1).png'))
    if jump is True: 
       spiller.rect.y -= spiller.farty *4 
       spiller.farty -= 1
    if spiller.farty < -10:
       jump = False
       spiller.farty = 10       
    if levelnr == 1:
       background_image_1 = pygame.image.load("skog.png").convert()
       screen.blit(background_image_2, [0,0])


        
    spillertruffet = pygame.sprite.groupcollide(spillergruppe, ghostgruppe, False, False, pygame.sprite.collide_mask)
    if spillertruffet:
        gameover = True

    ghosttruffet = pygame.sprite.groupcollide(ghostgruppe, spillergruppe, True, True, pygame.sprite.collide_mask)
    if ghosttruffet:
        spiller.kill()

    #Gruppene oppdateres
    ghostgruppe.update()
    tilegruppe.update()
    utganggruppe.update() 
    spillergruppe.update()
    moving_sprites.draw(screen)
    moving_sprites.update(0.25)
    #gruppene vises på skjermen.
    ghostgruppe.draw(screen)
    tilegruppe.draw(screen)
    utganggruppe.draw(screen)
    spillergruppe.draw(screen)
    if gameover: 
        screen.blit(gameovertekst,gameoverrect)
    if levelferdig:
        screen.blit(levelferdigtekst, levelferdigrect)
    spillergruppe.draw(screen)
    clock.tick(60)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.K_n:
            levelnr = O
            restart()
        for bullet in spiller.bullets:
            bullet.draw_bullet()
        pygame.time.delay(30)
   
pygame.display.flip()
pygame.quit()

共有1个答案

吕琪
2023-03-14

画完背景还要画子弹。使用循环遍历项目符号列表,并为每个项目符号调用< code>draw_bullet方法:

while running:
    # pygame.display.bullet()         <--- DELETE
    spiller.farty = 0
    spiller.fartx = 0
    trykketliste = pygame.key.get_pressed()
    spiller.skytt()
    background_image_1 = pygame.image.load("beach.png").convert()
    screen.blit(background_image_1, [0,0])

    # INSERT
    for bullet in spiller.bullets: 
        bullet.draw_bullet()        

    # [...]

    pygame.display.update()
    # [...]

screen.blit(background_image_1, [0,0]) 绘制背景并隐藏之前绘制的任何内容。pygame.display.update() 更新显示并使绘制的所有内容可见。因此,您需要在screen.blit(background_image_1,[0,0])之后和pygame.display.update()之前绘制场景的所有对象

 类似资料:
  • 问题内容: 我在应用程序中使用AsyncTask下载URL。我在onPreExecute()上使用ProgressDialog进行等待。但是我在进程完成时看不到ProgressDialog,我看了一会儿。想要在下载之后看到它。谁能帮我。谢谢我的代码是这样的: 问题答案: 首先,请注意所有AsyncTask实现的方法附带的“ @override”标头,例如 除非您必须在UI上执行某些操作,否则还要从

  • 我正在尝试为一个迷你项目创建一个时钟接龙游戏,以获得好友编程课程的指导方针。我已经运行了我的套牌和纸牌类。然而,我应该实现一个堆叠类。我不知道人们对时钟接龙有多熟悉,因为这是我第一次听说它...在时钟接龙中,有十三堆卡片。(在说明中,它是一个圆形中的堆叠阵列,由四张面朝下的卡片和圆形中间的一堆卡片组成)。在游戏开始时,卡片都将面朝下。圆圈中间有一堆被认为是“国王堆”。你必须从国王堆中“移除”顶部的

  • #非技术2023笔面经# 游戏运营必问的8大题 1、自我介绍 (考核点:是否還辑清晰、与岗位有关联、求职意愿) 2、你的优缺点是什么? (分开回答,个人是否多面丰富,对自我的总结,个人优缺点) 3、你为什么要申请这个职位呢?描述下你的校园生活。课余时间做什么事情呢? (看工作的匹配度) 4、分享一段工作经历,你做了什么业绩?这段工作中,你最不喜欢做什么事情。为什么离职?短期1-3年的职业规划是什么

  • 让我们一起动手完成一个项目,来快速上手 Rust!本章将介绍 Rust 中常用的一些概念,并通过真实的程序来展示如何运用它们。你将会学到更多诸如 let、match、方法、关联函数、外部 crate 等很多的知识!后继章节会深入探索这些概念的细节。在这一章,我们将练习基础。 我们会实现一个经典的新手编程问题:猜猜看游戏。它是这么工作的:程序将会随机生成一个 1 到 100 之间的随机整数。接着它会

  • 利用cocos2d框架写的推箱子游戏。仅能在真机3.5寸Retina屏幕下测试。 [Code4App.com]

  • 五子棋(Gomoku)是一款经典的线上五子棋游戏,5 子连成一线者胜。