我正在研究在pygame中制作精灵的更简单方法(仅限我)。我有这3个文件
文件1:Tests.py:
import pygame
from pygame.locals import *
import MySprites
import SpecialFunctions
Sprites = []
NewScreen = SpecialFunctions.Level('NewProject', '2D-AboveView', (960, 540), Sprites)
Sprites = []
imgDolu = [pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-8.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-9.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-10.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-11.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-12.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-13.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-14.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-15.png')]
imgNahoru = [pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-0.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-1.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-2.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-3.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-4.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-5.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-6.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-1-7.png')]
imgDoleva = [pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-0.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-1.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-2.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-3.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-4.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-5.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-6.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-7.png')]
imgDoprava = [pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-8.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-9.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-10.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-11.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-12.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-13.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-14.png'), pygame.image.load('HeroSprites/HeroSpriteSheet2 [www.imagesplitter.net]-0-15.png')]
imgUtociNahoru = []; imgUtociDolu = []; imgUtociDoleva = []; imgUtociDoprava = []
Player1 = MySprites.Player_Type_AboveView(movingUp=imgNahoru, movingDown=imgDolu, movingLeft=imgDoleva, movingRight=imgDoprava, swingingUp=imgUtociNahoru, swingingDown=imgUtociDolu, swingingLeft=imgUtociDoleva, swingingRight=imgUtociDoprava, controlUp=pygame.K_w, controlDown=pygame.K_s, controlLeft=pygame.K_a, controlRight=pygame.K_d, controlSwing=pygame.K_k, speed=5, x=960/2, y=540/2)
Sprites.append(Player1)
NewScreen.mainloop()
这是我的一般文件,这是我正在测试的地方。
文件2:SpecialFunctions.py:
import pygame
import MySprites
from pygame.locals import *
class Level:
def __init__(self, Name, Type, screen_size, AllSprites):
"""Inicialization of Project"""
black = (0, 0, 0)
screen_size = screen_size
self.screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption(Name)
self.screen.fill(black)
pygame.display.update()
self.ProjectSprites = AllSprites
self.Running = True
self.type = Type
self.keys_pressed_list = []
def screen_Update(self):
if self.Running:
self.screen.fill((0, 0, 0))
for sprite in self.ProjectSprites:
sprite.update(sprite, self.screen)
sprite.draw(self.screen)
pygame.display.update()
def mainloop(self):
"""Running program"""
while self.Running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
self.Running = False
break
elif event.type == pygame.KEYDOWN:
self.keys_pressed_list.append(event.key)
elif event.type == pygame.KEYUP:
self.keys_pressed_list.remove(event.key)
if self.Running:
self.screen_Update()
pygame.time.delay(100)
这是包含一些我可以用于屏幕的工具的文件。
文件3:MySprites.py:
import pygame
class Player_Type_AboveView (pygame.sprite.Sprite):
def __init__(self,
movingUp, movingDown, movingLeft, movingRight,
swingingUp, swingingDown, swingingLeft, swingingRight,
controlUp, controlDown, controlLeft, controlRight, controlSwing,
speed, x, y):
pygame.sprite.Sprite.__init__(self)
#Player controls:
self.controls_Up = controlUp
self.controls_Down = controlDown
self.controls_Left = controlLeft
self.controls_Right = controlRight
self.controls_Swing = controlSwing
#Player textures:
self.imgUp = movingUp
self.imgDown = movingDown
self.imgLeft = movingLeft
self.imgRight = movingRight
self.swingingUp = swingingUp
self.swingingDown = swingingDown
self.swingingLeft = swingingLeft
self.swingingRight = swingingRight
self.phase = 0
self.imgGroup = self.imgDown
self.image = self.imgGroup[self.phase]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = speed
self.Swinging = False
def update(self, Scene):
moved = False
x = 0
y = 0
#Player controls
if self.controls_Up in Scene.keys_pressed_list:
self.imageGroup = self.imgUp
y = -self.speed
moved = True
elif self.controls_Down in Scene.keys_pressed_list:
self.imageGroup = self.imgDown
y = self.speed
moved = True
elif self.controls_Left in Scene.keys_pressed_list:
self.imageGroup = self.imgLeft
x = -self.speed
moved = True
elif self.controls_Right in Scene.keys_pressed_list:
self.imageGroup = self.imgRight
x = self.speed
moved = True
elif self.controls_Swing in Scene.keys_pressed_list:
Swinging = {
self.imgDown : self.swingingDown,
self.imgUp: self.swingingUp,
self.imgLeft: self.swingingLeft,
self.imgRight: self.swingingRight
}
self.imageGroup = Swinging[self.imageGroup]
self.Swinging = True
moved = True
if moved or self.phase != 0:
if moved:
self.phase = (self.phase + 1) % 4
else:
self.phase = 0
self.image = self.imageGroup[self.phase]
self.rect.x += x
self.rect.y += y
self.Swinging = False
这是播放器类的文件 - 顺便说一句。摆动是WIP。
我正在尝试制作可以移动的窗口和播放器(WASD),但它只能制作黑色窗口。我需要知道的是为什么?!
我正在使用python 3.6
希望这个问题不是那么愚蠢
答案 0 :(得分:1)
您添加播放器精灵的列表与您在Level
实例中使用的列表不同。看看这里:
...
import SpecialFunctions
Sprites = []
NewScreen = SpecialFunctions.Level('NewProject', '2D-AboveView', (960, 540), Sprites)
Sprites = []
...
因此,当您将NewScreen
个实例添加到另一个未在任何地方使用的列表时,Player_Type_AboveView
只会看到第一个列表,该列表始终为空。
只需删除第二条Sprites = []
行。
此外,它(大多数情况下)最好使用Group
而不是列表来管理您的精灵。