teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

追記

2020/05/18 22:17

投稿

ForestSeo
ForestSeo

スコア2724

title CHANGED
File without changes
body CHANGED
@@ -15,5 +15,4 @@
15
15
  ```classAの引数にclassBのインスタンスであるbを入れたくて、
16
16
  classBの引数にはclassAのインスタンスであるaを入れたい。
17
17
  ### Version
18
- Python: 3.7.7
18
+ Python: 3.7.7
19
- Pygame: 1.9.6

4

追記

2020/05/18 22:17

投稿

ForestSeo
ForestSeo

スコア2724

title CHANGED
@@ -1,1 +1,1 @@
1
- Pythonのインスタンスでよくわからないことがある。
1
+ Pythonのインスタンスでよくわからないことがある。
body CHANGED
File without changes

3

追記

2020/05/18 22:17

投稿

ForestSeo
ForestSeo

スコア2724

title CHANGED
@@ -1,1 +1,1 @@
1
- PygamePingPongゲームでよくわからないことがある。
1
+ Pythonインスタンスでよくわからないことがある。
body CHANGED
File without changes

2

追記

2020/05/18 22:17

投稿

ForestSeo
ForestSeo

スコア2724

title CHANGED
File without changes
body CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
  a = A(b)
13
13
  b = B(a)
14
- # エラー
14
+ # まあエラーですよね
15
15
  ```classAの引数にclassBのインスタンスであるbを入れたくて、
16
16
  classBの引数にはclassAのインスタンスであるaを入れたい。
17
17
  ### Version

1

追記

2020/05/18 22:16

投稿

ForestSeo
ForestSeo

スコア2724

title CHANGED
File without changes
body CHANGED
@@ -1,119 +1,19 @@
1
1
  ### 前提・実現したいこと
2
- Pythonは初心者ではない(はず)ですが、Pygameは初心者です。
3
- [こちら](https://algorithm.joho.info/programming/python/pygame/)のサイトを参考に勉強してみました。そして、PingPongゲーム的なものを作ってみたのですが...
4
- 長くてすみません。質問は一部だけなのでどうかお読ください
2
+ 回答が得られかったので質問を変えてました(簡潔に)
5
3
  ```Python
4
+ class A():
6
- # -*- coding: utf-8 -*-
5
+ def __init__(self, b):
7
- import sys, time, itertools
8
- from pygame.locals import *
9
- import pygame.mixer
6
+ ・・・
10
7
 
11
- def close():
8
+ class B():
12
- pygame.quit()
9
+ def __init__(self, a):
13
- sys.exit()
10
+ ・・・
14
11
 
15
- tpl = (0, 0, 900, 600)
16
- base = Rect(tpl)
17
- cntr = Rect(tuple(map(lambda num: num/2, tpl))) # 全て割る2
18
- bar_rect = Rect(0, 0, 11, 83)
19
-
20
- class HmnBar(pygame.sprite.Sprite):
21
- def __init__(self):
22
- pygame.sprite.Sprite.__init__(self, self.containers)
23
- self.image = pygame.image.load("img/bar.png").convert()
24
- self.rect = self.image.get_rect()
25
- self.rect.right = base.right - 20 # バーのx座標固定
26
- def update(self):
27
- self.rect.centery = pygame.mouse.get_pos()[1]
28
- self.rect.clamp_ip(base)
29
-
30
- class CpuBar(pygame.sprite.Sprite):
31
- def __init__(self, ball):
32
- pygame.sprite.Sprite.__init__(self, self.containers)
33
- self.image = pygame.image.load("img/bar.png").convert()
34
- self.rect = self.image.get_rect()
35
- self.rect.left = 20 # バーのx座標固定
36
- self.ball = ball
37
- def update(self):
38
- self.rect.centery = self.ball.centery
39
- self.rect.clamp_ip(base)
40
-
41
- class Ball(pygame.sprite.Sprite):
42
- def __init__(self, speed, hmn, cpu):
43
- pygame.sprite.Sprite.__init__(self, self.containers)
44
- self.image = pygame.image.load("img/tennis.png").convert()
45
- self.rect = self.image.get_rect()
46
- self.sx, self.sy = speed
47
- self.hmn, self.cpu = hmn, cpu
48
- def update(self):
49
- # cpuのバーの反射
50
- if self.rect.left <= 20 + bar_rect.width:
51
- if self.cpu.rect.top < self.rect.centery < self.cpu.rect.bottom:
52
- self.sx *= -1
53
- else:
54
- self.rect.centerx, self.rect.centery = cntr.size
55
- # hmnのバーの反射
56
- if base.width - (20 + bar_rect.width) <= self.rect.right:
57
- if self.hmn.rect.top < self.rect.centery < self.hmn.rect.bottom:
58
- self.sx *= -1
59
- else:
60
- self.rect.centerx, self.rect.centery = cntr.size
61
- # 上下壁反射
62
- if self.rect.top <= 0 or base.height <= self.rect.bottom:
63
- self.sy *= -1
64
- self.rect.centerx += self.sx
65
- self.rect.centery += self.sy
66
- self.rect.clamp_ip(base)
67
-
68
- start = time.time()
69
- def main():
70
- # Pygameの設定
71
- pygame.init()
72
- screen = pygame.display.set_mode(base.size)
73
- font = pygame.font.SysFont(None, 30)
74
- # 初期数値
75
- ball_sx, ball_sy = 4, 7
76
- cpu_sy = 7
77
- # スプライトグループ作成、追加
78
- group = pygame.sprite.RenderUpdates()
79
- HmnBar.containers = group
80
- CpuBar.containers = group
81
- Ball.containers = group
82
-
83
- hmn = HmnBar()
84
- "質問の部分はここ"
85
- cpu = CpuBar(ball)
86
- ball = Ball((ball_sx, ball_sy), hmn, cpu)
87
-
88
- clock = pygame.time.Clock()
89
- while True:
90
- clock.tick(60) # 60fps
91
- screen.fill((90, 90, 90)) # 灰色に塗りつぶし
92
- text = font.render(str(round(time.time() - start, 2)), True, (255, 255, 255)) # カウント
93
- screen.blit(text, [cntr.width - 20, 20])
94
- pygame.draw.line(screen,(255, 255, 255), (cntr.width, 45), (cntr.width, base.height - 20), 5) # 中央線
95
-
96
- group.update() # groupをアップデート
97
- group.draw(screen) # グループを描画
98
- # イベント
99
- for event in pygame.event.get():
100
- if event.type == QUIT:
101
- close()
12
+ a = A(b)
102
- if event.type == KEYDOWN and event.key == K_ESCAPE:
103
- close()
13
+ b = B(a)
104
- # 更新
14
+ # エラー
105
- pygame.display.update()
106
-
107
- if __name__ == "__main__":
108
- main()
109
-
110
- ```このコードの、"質問の部分はここ" の下の
111
- ```
112
- cpu = CpuBar(ball)
113
- ball = Ball((ball_sx, ball_sy), hmn, cpu)
114
- ```部分で、cpuとball情報交互に入れようとしいます。
15
+ ```classA引数にclassBインスタンスであるbを入れたく
115
- もちろんこままではエラーなります。なぜ交互に入れたいかと言うと、
16
+ classB引数はclassAのインスタンスであるaを入れたい
116
- ballはcpuのバーのy座標で跳ね返るか、アウトかが分かりますし、cpuはballのy座標についていくことで、自動操縦ができるのです。人間がバーにあてられなかった場合、ボールは真ん中から出てくるので、cpuの動きは一定ではありません。ちなみに自動操縦なしだとちゃんと動きます。不明点がありましたら追記・修正でコメントをください。
117
- ### バージョン
17
+ ### Version
118
18
  Python: 3.7.7
119
19
  Pygame: 1.9.6