我目前正在研究一個學校項目,但我被困在試圖讓我的精靈移動。我的錯誤信息是說我缺少1個需要的位置參數:Mario.handle_keys()中的'self'。在PyGame中移動一個雪碧
這是我的主要代碼:
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))
在此先感謝。
我欣賞任何建議!
這有幫助,但我已經到了一個新的錯誤。現在它告訴我我缺少1個需要的位置參數:__init __()中的'location'。有什麼建議? –
取決於它的__init __()。可能你應該用'mario = Mario([0,800])'替換'b = Mario([0,800])',或者調用'b.handle_keys()' – rrauenza
謝謝。這真的有幫助。 –