Home>

I want to match the arguments of dir and speed with ball = Ball (ball_color, ball_rect, 320, 10), what should I do?

import sys
import pygame
import math
from pygame.locals import QUIT
from pygame.locals import Rect
pygame.init ()
SURFACE = pygame.display.set_mode ((400,300))
FPSCLOCK = pygame.time.Clock ()
class Block:
     def __init __ (self, color, rect):
          self.color = color
          self.rect = rect

     def draw (self):
        pygame.draw.rect (SURFACE, self.color, self.rect)
class Ball:
     def __init __ (self, color, rect):
          self.color = color
          self.rect = rect
          self.dir = 320
     def draw (self):
          pygame.draw.ellipse (SURFACE, self.color, self.rect)
     def move (self):
          speed = 10
          if rect.centerx<0 or rect.centerx>400:
                self.dir = 180-self.dir
          if rect.centery<0 or rect.centery>300:
                self.dir = -self.dir
          self.rect.centerx + = math.cos (math.radians (self.dir)) * speed
          self.rect.centery-= math.sin (math.radians (self.dir)) * speed

left = 50
top = 50
width = 45
height = 20
block_color = (200, 50, 200)
block_rect = Rect (left, top, width, height)
block = Block (block_color, block_rect)

ball_rect = Rect (150,100,10,10)
ball_color = (255,255,0)
ball = Ball (ball_color, ball_rect, 320, 10)
while True:
    for event in pygame.event.get ():
         if event.type == QUIT:
              pygame.quit ()
              sys.exit ()
    SURFACE.fill ((0,0,0))
    ball.move ()
    block.draw ()
    ball.draw ()
    if ball.rect.colliderect (block.rect) == True:
         print ("collision")
    pygame.display.update ()
    FPSCLOCK.tick (30)
code


colliderect.txt ", line 52, in
ball = Ball (ball_color, ball_rect, 320, 10)
TypeError:init() takes 3 positional arguments but 5 were given

  • Answer # 1

    I think I'm typing by looking at some helpful website or reference book,__init__With optional argumentsdirOrspeedIt is the Ball class that should be given, not the Block class.

    Besides,move moveUsed inrectIsself.rectIf you don't, it won't workspeedAlsoself.speed(The__init__Should be used (prepared in).

    import sys
    import pygame
    import math
    from pygame.locals import QUIT
    from pygame.locals import Rect
    pygame.init ()
    SURFACE = pygame.display.set_mode ((400, 300))
    FPSCLOCK = pygame.time.Clock ()
    class Block:
        def __init __ (self, color, rect):
            self.color = color
            self.rect = rect
        def draw (self):
            pygame.draw.rect (SURFACE, self.color, self.rect)
    class Ball:
        def __init __ (self, color, rect, dir = 320, speed = 10):
            self.color = color
            self.rect = rect
            self.dir = dir
            self.speed = speed
        def draw (self):
            pygame.draw.ellipse (SURFACE, self.color, self.rect)
        def move (self):
            if self.rect.centerx<0 or self.rect.centerx>400:
                self.dir = 180 --self.dir
            if self.rect.centery<0 or self.rect.centery>300:
                self.dir = -self.dir
            self.rect.centerx + = math.cos (math.radians (self.dir)) * self.speed
            self.rect.centery-= math.sin (math.radians (self.dir)) * self.speed
    left = 50
    top = 50
    width = 45
    height = 20
    block_color = (200, 50, 200)
    block_rect = Rect (left, top, width, height)
    block = Block (block_color, block_rect)
    ball_rect = Rect (150, 100, 10, 10)
    ball_color = (255, 255, 0)
    ball = Ball (ball_color, ball_rect, 320, 10)
    while True:
        for event in pygame.event.get ():
            if event.type == QUIT:
                pygame.quit ()
                sys.exit ()
        SURFACE.fill ((0, 0, 0))
        ball.move ()
        block.draw ()
        ball.draw ()
        if ball.rect.colliderect (block.rect):
            print ("collision")
        pygame.display.update ()
        FPSCLOCK.tick (30)