在 PyGame 中移动精灵
Moving a Sprite in PyGame
我目前正在做一个学校项目,但我一直在努力让我的 sprite 移动。我的错误消息是说我缺少 1 个必需的位置参数:'self' in Mario.handle_keys().
这是我的主要代码:
import pygame
import sys
from pygame.locals import*
from Mario import Mario
from Ladder import Ladder
pygame.init()
b = Mario([0, 800])
c = Ladder([600, 800])
game_over = False
dispwidth = 600
dispheight = 800
cellsize = 10
white = (255, 255, 255)
black = (0, 0, 0)
bg = white
def main():
FPS = 30
while not game_over:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Mario.handle_keys()
Mario.draw(screen)
screen.fill(bg)
screen.blit(b.image, b.rect)
screen.blit(c.image, c.rect)
pygame.display.update()
fpstime.tick(FPS)
while True:
global fpstime
global screen
fpstime = pygame.time.Clock()
screen = pygame.display.set_mode((dispwidth, dispheight))
pygame.display.set_caption('Donkey Kong')
main()
还有我的精灵:
import pygame
from pygame.locals import*
class Mario(pygame.sprite.Sprite):
image = None
def __init__(self, location):
pygame.sprite.Sprite.__init__(self)
if Mario.image is None:
Mario.image = pygame.image.load('mario3.png')
self.image = Mario.image
self.rect = self.image.get_rect()
self.rect.bottomleft = location
self.x = 0
self.y = 0
def handle_keys(self):
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_LEFT]:
self.x -= 5
if keys_pressed[K_RIGHT]:
self.y += 5
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
提前致谢。
感谢任何建议!
Mario
是一个 class。方法 handle_keys(self)
是一个实例方法——这意味着它只能针对 Mario
实例调用。 (可以有一个 classmethod
,但这不是你想要的,因为你需要修改 self
。)
在顶部你做了一个 b = Mario([0, 800])
-- 我会把所有地方的 b
改成 mario
并且把 c
改成 ladder
.
mario = Mario([0, 800])
那么您将使用 mario.handle_keys()
.
而不是 Mario.handle_keys()
更多背景:
当你调用 mario.handle_keys()
时,下面实际发生的事情或多或少是 handle_keys(mario)
。对象 mario
最终成为参数 self
。由于您试图在 class Mario
上调用 handle_keys
,因此 Python 抱怨没有任何内容被传递给 handle_keys
self
参数.
更多杂草:
如果你定义一个class方法,你会这样做:
class Foo():
@classmethod
def my_class_method(cls):
...
你可以称它为 Foo.my_class_method()
,它会将 Foo
作为 cls
参数传递给 my_class_method
。
我目前正在做一个学校项目,但我一直在努力让我的 sprite 移动。我的错误消息是说我缺少 1 个必需的位置参数:'self' in Mario.handle_keys().
这是我的主要代码:
import pygame
import sys
from pygame.locals import*
from Mario import Mario
from Ladder import Ladder
pygame.init()
b = Mario([0, 800])
c = Ladder([600, 800])
game_over = False
dispwidth = 600
dispheight = 800
cellsize = 10
white = (255, 255, 255)
black = (0, 0, 0)
bg = white
def main():
FPS = 30
while not game_over:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Mario.handle_keys()
Mario.draw(screen)
screen.fill(bg)
screen.blit(b.image, b.rect)
screen.blit(c.image, c.rect)
pygame.display.update()
fpstime.tick(FPS)
while True:
global fpstime
global screen
fpstime = pygame.time.Clock()
screen = pygame.display.set_mode((dispwidth, dispheight))
pygame.display.set_caption('Donkey Kong')
main()
还有我的精灵:
import pygame
from pygame.locals import*
class Mario(pygame.sprite.Sprite):
image = None
def __init__(self, location):
pygame.sprite.Sprite.__init__(self)
if Mario.image is None:
Mario.image = pygame.image.load('mario3.png')
self.image = Mario.image
self.rect = self.image.get_rect()
self.rect.bottomleft = location
self.x = 0
self.y = 0
def handle_keys(self):
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_LEFT]:
self.x -= 5
if keys_pressed[K_RIGHT]:
self.y += 5
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
提前致谢。
感谢任何建议!
Mario
是一个 class。方法 handle_keys(self)
是一个实例方法——这意味着它只能针对 Mario
实例调用。 (可以有一个 classmethod
,但这不是你想要的,因为你需要修改 self
。)
在顶部你做了一个 b = Mario([0, 800])
-- 我会把所有地方的 b
改成 mario
并且把 c
改成 ladder
.
mario = Mario([0, 800])
那么您将使用 mario.handle_keys()
.
Mario.handle_keys()
更多背景:
当你调用 mario.handle_keys()
时,下面实际发生的事情或多或少是 handle_keys(mario)
。对象 mario
最终成为参数 self
。由于您试图在 class Mario
上调用 handle_keys
,因此 Python 抱怨没有任何内容被传递给 handle_keys
self
参数.
更多杂草:
如果你定义一个class方法,你会这样做:
class Foo():
@classmethod
def my_class_method(cls):
...
你可以称它为 Foo.my_class_method()
,它会将 Foo
作为 cls
参数传递给 my_class_method
。