Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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_Sprite - Fatal编程技术网

Python 游戏精灵没有出现

Python 游戏精灵没有出现,python,pygame,sprite,Python,Pygame,Sprite,我正在尝试学习pygame,现在只是尝试做一个非常简单的任务,加载一个精灵图像,但它没有出现。代码如下: # -*- coding: utf-8 -*- import pygame #Defining Sprite class class Character(pygame.sprite.Sprite): def __init__(self, location): # Call the parent class (Sprite) constructor su

我正在尝试学习pygame,现在只是尝试做一个非常简单的任务,加载一个精灵图像,但它没有出现。代码如下:

# -*- coding: utf-8 -*-
import pygame
#Defining Sprite class
class Character(pygame.sprite.Sprite):
    def __init__(self, location):
        # Call the parent class (Sprite) constructor
        super().__init__()
        self.image = pygame.transform.scale(pygame.image.load("mario-sprite.jpg"),(50,30))
        self.rect = self.image.get_rect()
        print(self.rect)
        self.rect.x = location[0]
        self.rect.y = location[1]
        
pygame.init()
# Set up the drawing window
screen = pygame.display.set_mode([1000, 650])
print(screen)
screen.fill((250,250,250))
bg = pygame.image.load('bk_cloud.jpg')
bg = pygame.transform.scale(bg, (1000, 650))
accesor = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
tiles = []
for c in accesor:
    temp = pygame.image.load('letter_'+c+'.png')
    tiles.append(pygame.transform.scale(temp, (100, 50)))
print(bg)
all_sprites_list = pygame.sprite.Group()
player = Character([10,570])
#player.rect.x = 10
#player.rect.y = 570
#all_sprites_list.add(player)
screen.blit(player.image,player.rect)
screen.blit(bg,(0,0))
def update_floor(i):
    for j in range(10):
        screen.blit(tiles[j],((((i+j)%10)*100),600))
pos = 0
update_floor(0)
clock=pygame.time.Clock()
# Update the display
pygame.display.update()
# Run until the user asks to quit
running = True
while running:
    # Did the user click the window close button?
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    #update_floor(pos)
    #pos = pos + 1
    #all_sprites_list.draw(screen)
    pygame.display.update()
    clock.tick(2)
# Done! Time to quit.
pygame.quit()

我提到了一堆教程,但它似乎是一些非常简单的东西,我错过了。有什么帮助吗?

您已经注释掉了相关代码:
所有精灵列表。添加(玩家)
所有精灵列表。绘制(屏幕)
。取消对相关代码的注释:

all\u sprite\u list=pygame.sprite.Group()
玩家=角色([10570])
player.rect.x=10
player.rect.y=570
所有精灵列表。添加(玩家)
def更新楼层(一):
对于范围(10)内的j:
屏风板(瓷砖[j],((i+j)%10)*100),600)
pos=0
clock=pygame.time.clock()
#运行,直到用户请求退出
运行=真
运行时:
#用户是否单击了窗口关闭按钮?
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
屏幕光点(背景,(0,0))
更新楼层(pos)
位置=位置+1
所有精灵列表。绘制(屏幕)
pygame.display.update()
时钟滴答作响(2)
#完成了!是时候退出了。
pygame.quit()

您已注释掉相关代码:
所有精灵列表。添加(玩家)
所有精灵列表。绘制(屏幕)
。谢谢!这似乎解决了主要问题。这是一件非常琐碎的事情,我错过了,但我觉得我不应该使用一个雪碧组,如果我只有一个雪碧。所以我试着不使用它,它让我很困惑什么该用,什么不该用。如果你还有其他问题,你应该多发一篇帖子。我的错。将编辑它。@rabbi76我记得我看到一些教程根本没有使用Sprite类,然后一些在类中声明了矩形属性,而一些在主程序中声明了类外属性。他们看到不同的教程时一定很困惑。我第一次尝试在pygame上使用精灵,所以我可能无法完全理解它。