我正在尝试使用PyGame
在Python中制作游戏,我仍然是一个菜鸟,因此可能存在错误的代码。我正在尝试在类plane
内调整精灵sprite
的大小。但我最终得到了错误消息:
错误讯息:
Traceback (most recent call last):
File "/home/frei/Desktop/Projects/main.py", line 45, in <module>
plane.resize(200,200)
File "/home/frei/Desktop/Projects/main.py", line 23, in resize
pygame.transform.scale(self(sizex,sizey))
AttributeError: sprite instance has no __call__ method
代码是:
#Import the library PyGame
import pygame; pygame.init()
window = pygame.display.set_mode((800, 800))
pygame.display.set_caption('Space Invaders')
class sprite:
"""A class that consists of a x value and a y value
Y is up and X is down"""
def __init__(self):
self.x = 250
self.y = 200
def draw(self, string, window):
global image
image = pygame.image.load(string).convert()
window.fill(0)
window.blit(image, (self.x, self.y))
pygame.display.flip()
def resize(self,sizex,sizey):
pygame.transform.scale(self(sizex,sizey))
# Load all images and assign them to variables
bakgrunn = pygame.image.load("stars.png").convert()
#Assign the variable plane to the class "sprite"
plane = sprite()
def draw_on_screen(image, x, y):
window.fill(0)
# Draw the image loaded
window.blit(image, (x, y))
pygame.display.flip()
#pygame.transform.scale(bakgrunn, (800, 500))
clock = pygame.time.Clock()
while True:
clock.tick(30)
plane.draw("plane.gif", window)
plane.resize(200,200)
keys = pygame.key.get_pressed() # checking pressed keys
if keys[pygame.K_LEFT]:
plane.x -= 2
if keys[pygame.K_RIGHT]:
plane.x += 2
if keys[pygame.K_UP]:
plane.y -= 2
if keys[pygame.K_DOWN]:
plane.y += 2
if keys[pygame.K_RETURN]:
pygame.quit()
exit()
if keys[pygame.K_DOWN]:
pass
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit();
exit()