我一直在尝试pygame,这是我迄今为止所做的。它给我带来了错误"属性错误:'口袋妖怪'对象没有属性' velx'",我无法弄清楚原因。追溯:
追踪(最近一次呼叫最后一次):
File "C:/Users/Joe/Desktop/GameDev/Main.py", line 37, in <module>
pokemon.motion()
File "C:/Users/Joe/Desktop/GameDev\classes.py", line 31, in motion
self.rect.x += self.velx
AttributeError: 'Pokemon' object has no attribute 'velx'
顺便说一句,口袋妖怪是我的精灵。我的主要代码:
import pygame,sys
from classes import *
pygame.init()
WIDTH,HEIGHT = 640, 360
screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)
img_pokemon = pygame.image.load("pokemon.png")
#colours
clr1 = (22,122,211)
clr2 = (0,44,166)
clr3 = (34,55,245)
clrvar = 1
spritemovex = 1
spritemovey = 1
#clock
clock = pygame.time.Clock()
FPS = 24
fivesecondinterval = FPS * 5
totalframes = 0
pokemon = Pokemon(0, 100, 40, 30, "pokemon.png")
while True:
#processes
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()
clock.tick(FPS)
#logic
pokemon.motion()
totalframes += 1
clrvar +=3
if clrvar > 255:
clrvar = 1
spritemovex += 2
spritemovey += 2
if spritemovex > WIDTH:
spritemovex %= WIDTH
if spritemovey > HEIGHT:
spritemovey %= HEIGHT
#draw
screen.fill( (90,clrvar,180) )
screen.blit(img_pokemon, (spritemovex,spritemovey))
我的课程代码:
import pygame
class BaseClass(pygame.sprite.Sprite):
allsprites = pygame.sprite.Group()
def __init__(self, x, y, width, height, image_string):
pygame.sprite.Sprite.__init__(self)
BaseClass.allsprites.add(self)
self.image = pygame.image.load(image_string)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = width
self.height = height
class Pokemon(BaseClass):
List = pygame.sprite.Group()
def __init___(self, x, y, width, height, image_string):
BaseClass.__init__(self, x, y, width, height, image_string)
Pokemon.List.add(self)
self.velx = 3
def motion(self):
self.rect.x += self.velx
答案 0 :(得分:0)
你拼错了__init__
:
def __init___(self, x, y, width, height, image_string):
# 123
Python不会调用该方法。删除第三个下划线:
def __init__(self, x, y, width, height, image_string):