缩进错误,试试这样:
import pygame
class MyPlane(pygame.sprite.Sprite): # inherit sprite from pygame
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('Images/me1.png').convert_alpha()
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left, self.rect.top = (
self.width - self.rect.width) // 2, self.height - self.rect.height - 60 # -60: we need this space to show something
self.speed = 10
# up
def MoveUp(self):
if self.rect.top > 0: # top is 0, bottom is 'height'
self.rect.top = self.rect.top - self.speed # if not, move up
else:
self.rect.top = 0 # can only be 0, not smaller than 0
# down
def MoveDown(self):
if self.rect.bottom < self.height - 60: # top is 0, bottom is 'height'
self.rect.bottom = self.rect.bottom + self.speed # if not, move down
else:
self.rect.bottom = self.height - 60 # -60: we need this space to show something
# left
def MoveLeft(self):
if self.rect.left > 0: # top is 0, bottom is 'height'
self.rect.left = self.rect.left - self.speed # if not, move left
else:
self.rect.left = 0
# right
def MoveRight(self):
if self.rect.right < self.width: # top is 0, bottom is 'height'
self.rect.right = self.rect.right + self.speed # if not, move right
else:
self.rect.left = self.width
|