質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1428閲覧

引数の数が合っていない?

kkashi89sk

総合スコア12

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2020/11/21 05:41

編集2020/11/21 07:04

dirとspeedの引数をball = Ball(ball_color, ball_rect, 320, 10)と合わせたいのですがどうすればいいでしょう

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("衝突した") pygame.display.update() FPSCLOCK.tick(30) コード

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

can110

2020/11/21 05:47

エラーの原因はタイトルのとおりですが、質問はなんでしょうか?
kkashi89sk

2020/11/21 06:49

Ballの引数を5つにしなければならないのでしょうか?やり方を教えてください。
Lhankor_Mhy

2020/11/21 06:56

いえ、2つだと思いますが。
guest

回答1

0

ベストアンサー

何か参考になるWebサイトか参考書を見て入力しているのでしょうが、__init__でオプション引数のdirspeedを与えるべきなのはBallクラスのほうで、Blockクラスではないのでは。

ほかにも、moveで使われているrectは、self.rectにしないと動かないし、speedself.speed(を__init__で用意して)使うべきでしょう。

Python

1import sys 2import pygame 3import math 4from pygame.locals import QUIT 5from pygame.locals import Rect 6 7pygame.init() 8 9SURFACE = pygame.display.set_mode((400, 300)) 10FPSCLOCK = pygame.time.Clock() 11 12 13class Block: 14 def __init__(self, color, rect): 15 self.color = color 16 self.rect = rect 17 18 def draw(self): 19 pygame.draw.rect(SURFACE, self.color, self.rect) 20 21 22class Ball: 23 def __init__(self, color, rect, dir=320, speed=10): 24 self.color = color 25 self.rect = rect 26 self.dir = dir 27 self.speed = speed 28 29 def draw(self): 30 pygame.draw.ellipse(SURFACE, self.color, self.rect) 31 32 def move(self): 33 if self.rect.centerx < 0 or self.rect.centerx > 400: 34 self.dir = 180 - self.dir 35 if self.rect.centery < 0 or self.rect.centery > 300: 36 self.dir = -self.dir 37 38 self.rect.centerx += math.cos(math.radians(self.dir)) * self.speed 39 self.rect.centery -= math.sin(math.radians(self.dir)) * self.speed 40 41 42left = 50 43top = 50 44width = 45 45height = 20 46block_color = (200, 50, 200) 47block_rect = Rect(left, top, width, height) 48block = Block(block_color, block_rect) 49 50 51ball_rect = Rect(150, 100, 10, 10) 52ball_color = (255, 255, 0) 53ball = Ball(ball_color, ball_rect, 320, 10) 54 55while True: 56 for event in pygame.event.get(): 57 if event.type == QUIT: 58 pygame.quit() 59 sys.exit() 60 61 SURFACE.fill((0, 0, 0)) 62 ball.move() 63 block.draw() 64 ball.draw() 65 66 if ball.rect.colliderect(block.rect): 67 print("衝突した") 68 69 pygame.display.update() 70 FPSCLOCK.tick(30)

投稿2020/11/21 07:03

Daregada

総合スコア11990

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kkashi89sk

2020/11/21 07:14

ありがとうございます。何とかなりました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問