TypeError 在 Pygame 中在后台 blit 代码中创建
TypeError created in Pygame within background blit code
我正在尝试将背景图像 blit 到屏幕上,但是,我似乎一直 运行 陷入这个错误:
TypeError: unbound method update() must be called with Background instance as first argument (got nothing instead)
错误消息指向这行代码:
Background.update()
我试过在 class 的外部和内部移动 blit,但不断出现相同的错误消息。我的猜测是这也会影响船舶代码。
这是整个 py 文件:
import pygame
from pygame.locals import *
# Colors
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
# Screen Size
SCREEN_X = 400
SCREEN_Y = 400
# Loading Images
background = pygame.image.load('StarBackground.png')
powership = pygame.image.load('PowerShip.png')
class Ship(pygame.sprite.Sprite):
# Movement rate of change
change_x = 0
change_y = 0
# Methods
def __init__(self):
# Starts the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Sets the ship's image
self.image = powership
# Sets the ship's rect
self.rect = self.image.get_rect()
# Set the ship's start location
self.rect.x = 0
self.rect.y = 0
def move_right(self):
self.change_x = 1
def move_left(self):
self.change_x = 1
def move_up(self):
self.change_y = 1
def move_down(self):
self.change_y = 1
def stop_x(self):
self.change_x = 0
def stop_y(self):
self.change_y = 0
def update(self, screen):
self.rect.x += self.change_x
self.rect.y += self.change_y
screen.blit(self.image,self.rect)
class Background(pygame.sprite.Sprite):
def __init__(self):
# Starts the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Sets the ship's image
self.image = background
# Sets the ship's rect
self.rect = self.image.get_rect()
# Sets the background's starting location
self.rect.x = 0
self.rect.y = 0
def update(self, screen):
screen.blit(self.image,self.rect)
def main():
pygame.init()
# Set the height and width of the screen
size = [SCREEN_X, SCREEN_Y]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Space Adventure")
# Creating the game objects
background = Background()
ship = Ship()
# Close button exit code
finished = False
# Manages the frames per second
clock = pygame.time.Clock()
# Game loop
while not finished:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
keyboardInput = pygame.key.get_pressed()
if event.type == pygame.KEYDOWN:
if keyboardInput[K_RIGHT]:
ship.move_right()
if keyboardInput[K_LEFT]:
ship.move_left()
if keyboardInput[K_UP]:
ship.move_up()
if keyboardInput[K_DOWN]:
ship.move_down()
if event.type == pygame.KEYUP:
if keyboardInput[K_RIGHT]:
ship.stop_x()
if keyboardInput[K_LEFT]:
ship.stop_x()
if keyboardInput[K_UP]:
ship.stop_y()
if keyboardInput[K_DOWN]:
ship.stop_y()
clock.tick(60)
pygame.display.flip()
Background.update()
Ship.update()
pygame.quit()
if __name__ == "__main__":
main()
编辑:将更新代码更改为以下修复了问题:
Background.update(screen)
Ship.update(screen)
这正是消息所说的:
TypeError: unbound method update() must be called with Background
instance as first argument (got nothing instead)
这意味着您正在调用 类 而不是实例(对象)。在 python 中,模块是大写的,按照惯例,实例不应该大写。
未大写的行应如下所示:
background.update(screen)
ship.update(screen)
我正在尝试将背景图像 blit 到屏幕上,但是,我似乎一直 运行 陷入这个错误:
TypeError: unbound method update() must be called with Background instance as first argument (got nothing instead)
错误消息指向这行代码:
Background.update()
我试过在 class 的外部和内部移动 blit,但不断出现相同的错误消息。我的猜测是这也会影响船舶代码。
这是整个 py 文件:
import pygame
from pygame.locals import *
# Colors
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
# Screen Size
SCREEN_X = 400
SCREEN_Y = 400
# Loading Images
background = pygame.image.load('StarBackground.png')
powership = pygame.image.load('PowerShip.png')
class Ship(pygame.sprite.Sprite):
# Movement rate of change
change_x = 0
change_y = 0
# Methods
def __init__(self):
# Starts the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Sets the ship's image
self.image = powership
# Sets the ship's rect
self.rect = self.image.get_rect()
# Set the ship's start location
self.rect.x = 0
self.rect.y = 0
def move_right(self):
self.change_x = 1
def move_left(self):
self.change_x = 1
def move_up(self):
self.change_y = 1
def move_down(self):
self.change_y = 1
def stop_x(self):
self.change_x = 0
def stop_y(self):
self.change_y = 0
def update(self, screen):
self.rect.x += self.change_x
self.rect.y += self.change_y
screen.blit(self.image,self.rect)
class Background(pygame.sprite.Sprite):
def __init__(self):
# Starts the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Sets the ship's image
self.image = background
# Sets the ship's rect
self.rect = self.image.get_rect()
# Sets the background's starting location
self.rect.x = 0
self.rect.y = 0
def update(self, screen):
screen.blit(self.image,self.rect)
def main():
pygame.init()
# Set the height and width of the screen
size = [SCREEN_X, SCREEN_Y]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Space Adventure")
# Creating the game objects
background = Background()
ship = Ship()
# Close button exit code
finished = False
# Manages the frames per second
clock = pygame.time.Clock()
# Game loop
while not finished:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
keyboardInput = pygame.key.get_pressed()
if event.type == pygame.KEYDOWN:
if keyboardInput[K_RIGHT]:
ship.move_right()
if keyboardInput[K_LEFT]:
ship.move_left()
if keyboardInput[K_UP]:
ship.move_up()
if keyboardInput[K_DOWN]:
ship.move_down()
if event.type == pygame.KEYUP:
if keyboardInput[K_RIGHT]:
ship.stop_x()
if keyboardInput[K_LEFT]:
ship.stop_x()
if keyboardInput[K_UP]:
ship.stop_y()
if keyboardInput[K_DOWN]:
ship.stop_y()
clock.tick(60)
pygame.display.flip()
Background.update()
Ship.update()
pygame.quit()
if __name__ == "__main__":
main()
编辑:将更新代码更改为以下修复了问题:
Background.update(screen)
Ship.update(screen)
这正是消息所说的:
TypeError: unbound method update() must be called with Background instance as first argument (got nothing instead)
这意味着您正在调用 类 而不是实例(对象)。在 python 中,模块是大写的,按照惯例,实例不应该大写。
未大写的行应如下所示:
background.update(screen)
ship.update(screen)