Reputation:
I'm working on an assignment for my grade 11 comp. sci class and I keep getting this error no matter what I do. Basically I need to modify pre-existing code to give keyboard commands to the cube instead of mouse commands using the given "player" class, I tried making the "Block" class a parent to the "Player" class but I keep getting the 3 positional arguments but 4 given error.
import pygame
import random
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Block(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
class Player(Block):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([15, 15])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.change_x = 0
self.change_y = 0
def changespeed(self, x, y):
self.change_x += x
self.change_y += y
def update(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
pygame.init()
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
for i in range(50):
block = Block(200, 20,20)
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
block_list.add(block)
all_sprites_list.add(block)
player = Player(RED, 20, 20)
all_sprites_list.add(player)
done = False
clock = pygame.time.Clock()
score = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.rect_x = player.change_x
screen.fill(WHITE)
pos = pygame.mouse.get_pos()
player.rect.x = player.rect.x
player.rect.y = player.rect.y
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
for block in blocks_hit_list:
score += 1
print(score)
all_sprites_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Upvotes: 2
Views: 956
Reputation: 211268
You want to create a player with a color and a position:
player = Player(RED, 20, 20)
The constructor of the base class (Block
) has 3 arguments:
class Block(pygame.sprite.Sprite): def __init__(self, color, width, height): self.image = pygame.Surface([width, height]) self.image.fill(color) self.rect = self.image.get_rect()
Therefore, the constructor has to have the parameters color
, x
and y
. Furthermore you have to pass arguments to the base class. You don't need to create the Surface (image
) and the rectangle (rect
) in the subclass. This is done in the base class:
class Player(Block):
def __init__(self, color, x, y):
super().__init__(color, 15, 15)
self.rect.x = x
self.rect.y = y
self.change_x = 0
self.change_y = 0
Upvotes: 1