我对类并不感到惊讶,并且正在努力找出为什么仅在我静止不动时才绘制精灵的原因,我猜这是因为与for循环有关的是我设置了图像对于每个精灵运动,但不是很确定。当我将代码从类中移出时,它可以工作,但对我来说却无济于事,因为我想在此文件中拥有多个类,并且如果我在类外(正确设置动画的位置)具有animObjs代码,则我不能将其用于其他字符。
这是现在的代码:
# character.py
import pygame, pyganim, time, math
from colours import *
from pygame.math import Vector2
from settings import *
mainClock = pygame.time.Clock()
global player1
class Player(object):
def __init__(self, x, y, sprintMultiplier, fps):
self.x = x
self.y = y
self.vel = 1/fps * 150
self.pos = (self.x, self.y)
self.animTypes = "walkUp walkLeft walkRight walkDown".split()
self.animObjs = {}
#self.imagesAndDurations = []
self.width = self.height = 64
self.direction = "DOWN"
self.standing = True
self.sprint = sprintMultiplier * self.vel
self.sprintCounter = 0
self.ammo = 5
self.UP = "up"
self.DOWN = "down"
self.LEFT = "left"
self.RIGHT = "right"
self.upStanding = pygame.image.load("sprite/walk/player_walkUpStanding.png")
self.leftStanding = pygame.image.load("sprite/walk/player_walkLeftStanding.png")
self.rightStanding = pygame.image.load("sprite/walk/player_walkRightStanding.png")
self.downStanding = pygame.image.load("sprite/walk/player_walkDownStanding.png")
self.sprinting = self.moveUp = self.moveLeft = self.moveRight = self.moveDown = False
self.moveConductor = pyganim.PygConductor(self.animObjs)
for self.animType in self.animTypes:
self.imagesAndDurations = [("sprite/walk/player_%s.%s.png" % (self.animType, str(num).rjust(3, "0")), 0.1) for num in
range(9)]
self.animObjs[self.animType] = pyganim.PygAnimation(self.imagesAndDurations)
def draw(self, window):
if self.moveUp or self.moveDown or self.moveRight or self.moveLeft:
self.moveConductor.play()
if self.direction == self.UP:
self.animObjs["walkUp"].blit(window, (self.x, self.y))
elif self.direction == self.DOWN:
self.animObjs["walkDown"].blit(window, (self.x, self.y))
elif self.direction == self.RIGHT:
self.animObjs["walkRight"].blit(window, (self.x, self.y))
elif self.direction == self.LEFT:
self.animObjs["walkLeft"].blit(window, (self.x, self.y))
if self.sprinting:
rate = self.sprint
else:
rate = self.vel
if self.moveUp:
self.y -= rate
if self.moveDown:
self.y += rate
if self.moveLeft:
self.x -= rate
if self.moveRight:
self.x += rate
elif not self.moveUp or self.moveDown or self.moveRight or self.moveLeft:
self.moveConductor.stop()
if self.direction == self.UP:
window.blit(self.upStanding, (self.x, self.y))
if self.direction == self.DOWN:
window.blit(self.downStanding, (self.x, self.y))
if self.direction == self.RIGHT:
window.blit(self.rightStanding, (self.x, self.y))
if self.direction == self.LEFT:
window.blit(self.leftStanding, (self.x, self.y))
if self.x < 0:
self.x = 0
if self.x > sWidth - self.width:
self.x = sWidth - self.width
if self.y < 0:
self.y = 0
if self.y > sHeight - self.width:
self.y = sHeight - self.width
class Enemy(object):
pos = (20,20)
def __init__(self, fps, pos, difficulty):
self.vel = 1/fps * 100 * difficulty
self.animTypes = "walkUp walkLeft walkRight walkDown".split()
self.animObjs = {}
self.width = self.height = 64
self.x = 20
self.y = 20
self.direction = "DOWN"
self.standing = True
self.UP = "up"
self.DOWN = "down"
self.LEFT = "left"
self.RIGHT = "right"
self.upStanding = pygame.image.load("enemy sprite/walk/enemyOrc1_standingUp.png")
self.leftStanding = pygame.image.load("enemy sprite/walk/enemyOrc1_standingLeft.png")
self.rightStanding = pygame.image.load("enemy sprite/walk/enemyOrc1_standingRight.png")
self.downStanding = pygame.image.load("enemy sprite/walk/enemyOrc1_standingDown.png")
self.sprinting = self.moveUp = self.moveDown = self.moveLeft = self.moveRight = False
self.moveConductor = pyganim.PygConductor(self.animObjs)
self.rect = self.downStanding.get_rect(center=pos)
self.pos = Vector2(pos)
self.heading = (0,0)
def draw(self, window):
for self.animType in self.animTypes:
self.imagesAndDurations = [("enemy sprite/walk/enemyOrc1_%s.%s.png" % (self.animType, str(num).rjust(3, "0")), 0.1) for num in
range(9)]
self.animObjs[self.animType] = pyganim.PygAnimation(self.imagesAndDurations)
if self.heading[0] > 0 or self.heading[0] < 0 or self.heading[1] > 0 or self.heading[1] < 0:
self.moveConductor.play()
if self.heading[0] > 0:
self.moveRight = True
self.direction = self.RIGHT
self.animObjs["walkRight"].blit(window, (self.pos))
elif self.heading[0] < 0:
self.moveLeft = True
self.direction = self.LEFT
self.animObjs["walkLeft"].blit(window, (self.pos))
elif self.heading[1] > 0:
self.moveDown = True
self.direction = self.DOWN
self.animObjs["walkDown"].blit(window, (self.pos))
elif self.heading[1] < 0:
self.moveUp = True
self.direction = self.UP
self.animObjs["walkUp"].blit(window, (self.pos))
else:
self.moveConductor.stop()
if self.direction == self.UP:
window.blit(self.upStanding, (self.pos))
if self.direction == self.DOWN:
window.blit(self.downStanding, (self.pos))
if self.direction == self.RIGHT:
window.blit(self.rightStanding, (self.pos))
if self.direction == self.LEFT:
window.blit(self.leftStanding, (self.pos))
def moveTowardsPlayer(self, player):
self.heading = player.pos - self.pos
self.pos += self.heading * 0.01
self.rect.center = self.pos
我要更具体地询问涉及
的代码部分self.animTypes = "walkUp"
self.animObjs = {}
for self.animTypes in ...
因为这是如果我将它放在类之外的部分,它将可以正常工作,但是如果我将其包含在类中,则当精灵移动时,它不会被显示。 (即,仅在静止时精灵才会发白)