[英]I have an argument issue and I can't figure out why?
我正在與一些班級一起嘗試編寫一個小游戲。 我創建了一種在屏幕上移動 object 並添加鍵綁定的方法。
我希望方塊移動,但它只是給出一個錯誤。 你能解釋一下為什么它給我這個錯誤嗎?
代碼:
class shape():
def __init__(self, place, colour, x, y):
self.place = place
self.colour = colour
self.x = x
self.y = y
#class for a rectangle
class rectangle(shape):
def __init__(self, place, colour, x, y, length, width):
super().__init__(place, colour, x, y)
self.length = length
self.width = width
pygame.draw.rect(screen, colour, pygame.Rect(x, y, length, width))
def move_up():
self.y = self.y + 3
def move_down():
self.y = self.y - 3
def move_right():
self.x = self.x + 3
def move_left():
self.x = left.x - 3
#creating a rectangle
Rectangle = rectangle(screen, yellow, x, y, 60, 60)
#main loop
while not done:
#checking for game events
for event in pygame.event.get():
#quitting gamw when window is closed
if event.type == pygame.QUIT:
done = True
#detecting key presses
key_press = pygame.key.get_pressed()
if key_press[pygame.K_UP]: Rectangle.move_up()
if key_press[pygame.K_DOWN]:Rectangle.move_down()
if key_press[pygame.K_LEFT]:Rectangle.move_left()
if key_press[pygame.K_RIGHT]:Rectangle.move_right()
pygame.display.flip()
我收到此錯誤:
Traceback (most recent call last):
File "Pygame.py", line 73, in <module>
if key_press[pygame.K_RIGHT]:Rectangle.move_right()
TypeError: move_right() takes 0 positional arguments but 1 was given
我不知道為什么。
矩形是 class 和 move_right 是 class 的方法。 因此,您必須將其 self 作為參數傳遞。
所有這些方法
def move_up():
def move_down():
def move_right():
def move_left():
實際上是 class矩形的方法,所以它們都需要“self”參數,你必須將它們編輯為:
def move_up(self):
self.y = self.y + 3
def move_down(self):
self.y = self.y - 3
def move_right(self):
self.x = self.x + 3
def move_left(self):
self.x = left.x - 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.