質問編集履歴

3

コードをみやすくした・・はず

2018/10/05 11:21

投稿

websoler
websoler

スコア39

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,158 @@
1
+ ```python
2
+
3
+ class GridScreen(object):
4
+
5
+ SCREEN_RECT = Rect(0, 0, 640, 480)
6
+
7
+ ROW, COL = 15, 20
8
+
9
+ GRID_SIZE = 32
10
+
11
+
12
+
13
+ def __init__(self):
14
+
15
+ self._screen = pygame.display.set_mode(GridScreen.SCREEN_RECT.size)
16
+
17
+ pygame.display.set_caption('test')
18
+
19
+
20
+
21
+ def draw(self, image, column, row):
22
+
23
+ self._screen.blit(
24
+
25
+ image,
26
+
27
+ (column * GridScreen.GRID_SIZE,
28
+
29
+ row * GridScreen.GRID_SIZE))
30
+
31
+
32
+
33
+ class Image(object):
34
+
35
+ def __init__(self, filename, colorkey=None):
36
+
37
+ filename = os.path.join("data", filename)
38
+
39
+ try:
40
+
41
+ self._image = pygame.image.load(filename)
42
+
43
+ self._image = self._image.convert()
44
+
45
+ except pygame.error:
46
+
47
+ print('failed to load image = {}'.format(filename))
48
+
49
+ raise
50
+
51
+
52
+
53
+ if colorkey is not None:
54
+
55
+ if colorkey is -1:
56
+
57
+ colorkey = self._image.get_at((0, 0))
58
+
59
+ self._image.set_colorkey(colorkey, RLEACCEL)
60
+
61
+
62
+
63
+ def image(self):
64
+
65
+ return self._image
66
+
67
+ class Map(object):
68
+
69
+ def __init__(self, grid_screen):
70
+
71
+ self._grid_screen = grid_screen
72
+
73
+ self._grass_image = Image('grass.png')
74
+
75
+ self._water_image = Image('water.png')
76
+
77
+
78
+
79
+ def draw(self):
80
+
81
+ for row in range(self._grid_screen.ROW):
82
+
83
+ for column in range(self._grid_screen.COL):
84
+
85
+ if map[row][column] == 0:
86
+
87
+ self._grid_screen.draw(
88
+
89
+ self._grass_image.image(), column, row)
90
+
91
+ if map[row][column] == 1:
92
+
93
+ self._grid_screen.draw(
94
+
95
+ self._water_image.image(), column, row)
96
+
97
+
98
+
99
+ class Character(object):
100
+
101
+ def __init__(self, grid_screen, filename):
102
+
103
+ self._grid_screen = grid_screen
104
+
105
+ self._image = Image(filename, -1)
106
+
107
+
108
+
109
+ def draw(self, column, row):
110
+
111
+ self._grid_screen.draw(self._image.image(), column, row)
112
+
113
+
114
+
115
+
116
+
117
+ class Game(object):
118
+
119
+ def __init__(self):
120
+
121
+ pygame.init()
122
+
123
+ self._grid_screen = GridScreen()
124
+
125
+ self._map = Map(self._grid_screen)
126
+
127
+ self._character = Character(self._grid_screen, 'player1.png')
128
+
129
+
130
+
131
+ def run(self):
132
+
133
+ while True:
134
+
135
+ self._map.draw()
136
+
137
+ self._character.draw(5, 5)
138
+
139
+ pygame.display.update()
140
+
141
+ for event in pygame.event.get():
142
+
143
+ if event.type == QUIT:
144
+
145
+ sys.exit()
146
+
147
+
148
+
149
+ if __name__ == '__main__':
150
+
151
+ game = Game()
152
+
153
+ game.run()
154
+
1
- ### 前提・実現したいこと
155
+ ```### 前提・実現したいこと
2
156
 
3
157
 
4
158
 
@@ -50,158 +204,6 @@
50
204
 
51
205
 
52
206
 
53
- class GridScreen(object):
54
-
55
- SCREEN_RECT = Rect(0, 0, 640, 480)
56
-
57
- ROW, COL = 15, 20
58
-
59
- GRID_SIZE = 32
60
-
61
-
62
-
63
- def __init__(self):
64
-
65
- self._screen = pygame.display.set_mode(GridScreen.SCREEN_RECT.size)
66
-
67
- pygame.display.set_caption('test')
68
-
69
-
70
-
71
- def draw(self, image, column, row):
72
-
73
- self._screen.blit(
74
-
75
- image,
76
-
77
- (column * GridScreen.GRID_SIZE,
78
-
79
- row * GridScreen.GRID_SIZE))
80
-
81
-
82
-
83
- class Image(object):
84
-
85
- def __init__(self, filename, colorkey=None):
86
-
87
- filename = os.path.join("data", filename)
88
-
89
- try:
90
-
91
- self._image = pygame.image.load(filename)
92
-
93
- self._image = self._image.convert()
94
-
95
- except pygame.error:
96
-
97
- print('failed to load image = {}'.format(filename))
98
-
99
- raise
100
-
101
-
102
-
103
- if colorkey is not None:
104
-
105
- if colorkey is -1:
106
-
107
- colorkey = self._image.get_at((0, 0))
108
-
109
- self._image.set_colorkey(colorkey, RLEACCEL)
110
-
111
-
112
-
113
- def image(self):
114
-
115
- return self._image
116
-
117
- class Map(object):
118
-
119
- def __init__(self, grid_screen):
120
-
121
- self._grid_screen = grid_screen
122
-
123
- self._grass_image = Image('grass.png')
124
-
125
- self._water_image = Image('water.png')
126
-
127
-
128
-
129
- def draw(self):
130
-
131
- for row in range(self._grid_screen.ROW):
132
-
133
- for column in range(self._grid_screen.COL):
134
-
135
- if map[row][column] == 0:
136
-
137
- self._grid_screen.draw(
138
-
139
- self._grass_image.image(), column, row)
140
-
141
- if map[row][column] == 1:
142
-
143
- self._grid_screen.draw(
144
-
145
- self._water_image.image(), column, row)
146
-
147
-
148
-
149
- class Character(object):
150
-
151
- def __init__(self, grid_screen, filename):
152
-
153
- self._grid_screen = grid_screen
154
-
155
- self._image = Image(filename, -1)
156
-
157
-
158
-
159
- def draw(self, column, row):
160
-
161
- self._grid_screen.draw(self._image.image(), column, row)
162
-
163
-
164
-
165
-
166
-
167
- class Game(object):
168
-
169
- def __init__(self):
170
-
171
- pygame.init()
172
-
173
- self._grid_screen = GridScreen()
174
-
175
- self._map = Map(self._grid_screen)
176
-
177
- self._character = Character(self._grid_screen, 'player1.png')
178
-
179
-
180
-
181
- def run(self):
182
-
183
- while True:
184
-
185
- self._map.draw()
186
-
187
- self._character.draw(5, 5)
188
-
189
- pygame.display.update()
190
-
191
- for event in pygame.event.get():
192
-
193
- if event.type == QUIT:
194
-
195
- sys.exit()
196
-
197
-
198
-
199
- if __name__ == '__main__':
200
-
201
- game = Game()
202
-
203
- game.run()
204
-
205
207
  ### 試したこと
206
208
 
207
209
  ### 補足情報(FW/ツールのバージョンなど)

2

コードを追記しました

2018/10/05 11:21

投稿

websoler
websoler

スコア39

test CHANGED
@@ -1 +1 @@
1
- オブジェクト指向で必ず必要なリソースの受け渡しにつ
1
+ オブジェクト指向で必ず必要なリソースの受け渡しの好まし設計パターンを知りたい
test CHANGED
@@ -38,6 +38,170 @@
38
38
 
39
39
  ### 該当のソースコード
40
40
 
41
+ こちらのサイト様のプログラムからオブジェクト化させて頂いています。
42
+
43
+ http://aidiary.hatenablog.com/
44
+
45
+
46
+
47
+ プログラム中の「self._grid_screen」をその他クラスへいちいち渡すのが
48
+
49
+ 合っているのか、もっと良い設計があるのか・・
50
+
51
+
52
+
53
+ class GridScreen(object):
54
+
55
+ SCREEN_RECT = Rect(0, 0, 640, 480)
56
+
57
+ ROW, COL = 15, 20
58
+
59
+ GRID_SIZE = 32
60
+
61
+
62
+
63
+ def __init__(self):
64
+
65
+ self._screen = pygame.display.set_mode(GridScreen.SCREEN_RECT.size)
66
+
67
+ pygame.display.set_caption('test')
68
+
69
+
70
+
71
+ def draw(self, image, column, row):
72
+
73
+ self._screen.blit(
74
+
75
+ image,
76
+
77
+ (column * GridScreen.GRID_SIZE,
78
+
79
+ row * GridScreen.GRID_SIZE))
80
+
81
+
82
+
83
+ class Image(object):
84
+
85
+ def __init__(self, filename, colorkey=None):
86
+
87
+ filename = os.path.join("data", filename)
88
+
89
+ try:
90
+
91
+ self._image = pygame.image.load(filename)
92
+
93
+ self._image = self._image.convert()
94
+
95
+ except pygame.error:
96
+
97
+ print('failed to load image = {}'.format(filename))
98
+
99
+ raise
100
+
101
+
102
+
103
+ if colorkey is not None:
104
+
105
+ if colorkey is -1:
106
+
107
+ colorkey = self._image.get_at((0, 0))
108
+
109
+ self._image.set_colorkey(colorkey, RLEACCEL)
110
+
111
+
112
+
113
+ def image(self):
114
+
115
+ return self._image
116
+
117
+ class Map(object):
118
+
119
+ def __init__(self, grid_screen):
120
+
121
+ self._grid_screen = grid_screen
122
+
123
+ self._grass_image = Image('grass.png')
124
+
125
+ self._water_image = Image('water.png')
126
+
127
+
128
+
129
+ def draw(self):
130
+
131
+ for row in range(self._grid_screen.ROW):
132
+
133
+ for column in range(self._grid_screen.COL):
134
+
135
+ if map[row][column] == 0:
136
+
137
+ self._grid_screen.draw(
138
+
139
+ self._grass_image.image(), column, row)
140
+
141
+ if map[row][column] == 1:
142
+
143
+ self._grid_screen.draw(
144
+
145
+ self._water_image.image(), column, row)
146
+
147
+
148
+
149
+ class Character(object):
150
+
151
+ def __init__(self, grid_screen, filename):
152
+
153
+ self._grid_screen = grid_screen
154
+
155
+ self._image = Image(filename, -1)
156
+
157
+
158
+
159
+ def draw(self, column, row):
160
+
161
+ self._grid_screen.draw(self._image.image(), column, row)
162
+
163
+
164
+
165
+
166
+
167
+ class Game(object):
168
+
169
+ def __init__(self):
170
+
171
+ pygame.init()
172
+
173
+ self._grid_screen = GridScreen()
174
+
175
+ self._map = Map(self._grid_screen)
176
+
177
+ self._character = Character(self._grid_screen, 'player1.png')
178
+
179
+
180
+
181
+ def run(self):
182
+
183
+ while True:
184
+
185
+ self._map.draw()
186
+
187
+ self._character.draw(5, 5)
188
+
189
+ pygame.display.update()
190
+
191
+ for event in pygame.event.get():
192
+
193
+ if event.type == QUIT:
194
+
195
+ sys.exit()
196
+
197
+
198
+
199
+ if __name__ == '__main__':
200
+
201
+ game = Game()
202
+
203
+ game.run()
204
+
41
205
  ### 試したこと
42
206
 
43
207
  ### 補足情報(FW/ツールのバージョンなど)

1

誤字修正

2018/10/05 11:17

投稿

websoler
websoler

スコア39

test CHANGED
@@ -1 +1 @@
1
- オブジェクト指向について
1
+ オブジェクト指向で必ず必要なリソースの受け渡しについて
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  screen = pygame.display.set_mode(GridScreen.SCREEN_RECT.size)
16
16
 
17
- で得られたscreenを描画関連のクラス全てにリレー(クラスインスタンスで渡して、
17
+ で得られたscreenを描画関連のクラス全てにリレー(コンストラク引数で渡して、
18
18
 
19
19
  プライベートメンバに保持)していく形です。
20
20