Reputation: 78
I'm trying python with a simple game, but encounter this error.
The codes as below:
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""A class to manage bullets fired by the ship."""
def __init__(self, ai_game):
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
self.color = self.settings.bullet_color
# Create a bullet rect at (0, 0) and then set correct position.
# self.rect = pygame.Rect(0, 0, self.settings.bullet_width,
# self.settings.bullet_height)
self.rect = pygame.Rect(left=0, top=0, width=self.settings.bullet_width,
height=self.settings.bullet_height)
# ...
Then the error says:
File "my_codes/alien_invasion/bullet.py", line 15, in init
self.rect = pygame.Rect(left=0, top=0, width=self.settings.bullet_width,
TypeError: Argument must be rect style object
If I change the line as below it works fine:
self.rect = pygame.Rect(0, 0, self.settings.bullet_width,
self.settings.bullet_height)
I don't under stand why only positional args passing works here.
I would appreciate it if you can help me.
Upvotes: 1
Views: 935
Reputation: 210878
See the documentation of pygame.Rect
:
[...] A Rect can be created from a combination of left, top, width, and height values. Rects can also be created from python objects that are already a Rect or have an attribute named "rect".
See an implementation of the constructor of pygame.Rect
(from pygame_cffi
):
class Rect(object):
# [...]
def __init__(self, *args):
# [...]
Therefore, you cannot specify the arguments with keyword arguments.
Upvotes: 2