質問編集履歴

1

codeを結果を挿入しました。

2018/06/28 12:15

投稿

saldjawoig
saldjawoig

スコア13

test CHANGED
File without changes
test CHANGED
@@ -5,3 +5,171 @@
5
5
  コマンドラインでpygameをインストールして、画面を表示させようとしているのですが、いつまでも読み込みをしていて画面が表示されません。どのようにすれば、画面をすぐに表示することができるでしょうか?
6
6
 
7
7
  開発環境:IDLE
8
+
9
+
10
+
11
+ ```
12
+
13
+ import sys
14
+
15
+ from random import randint
16
+
17
+ import pygame
18
+
19
+ from pygame.locals import QUIT,Rect,KEYDOWN,K_SPACE
20
+
21
+
22
+
23
+ pygame.init()
24
+
25
+ pygame.key.set_repeat(5,5)
26
+
27
+ SURFACE = pygame.display.set_mode((800,600))
28
+
29
+ FSPCLOCK = pygame.time.Clock()
30
+
31
+
32
+
33
+ def main():
34
+
35
+ walls = 80
36
+
37
+ ship_y = 250
38
+
39
+ velocity = 0
40
+
41
+ score = 0
42
+
43
+ slope = randint(1,6)
44
+
45
+ sysfont = pygame.font.SysFont(None,36)
46
+
47
+ ship_image = pygame.image.load('ship.png')
48
+
49
+ bang_image = pygame.image.load('bang.png')
50
+
51
+ holes = []
52
+
53
+ for xpos in range(walls):
54
+
55
+ holes.append(Rect(xpos*10,100,10,400))
56
+
57
+ game_over = False
58
+
59
+
60
+
61
+ while(1):
62
+
63
+ is_space_down = False
64
+
65
+ for event in pygame.event.get():
66
+
67
+ if event.type ==QUIT:
68
+
69
+ pygame.quit()
70
+
71
+ sys.exit()
72
+
73
+ elif event.type == KEYDOWN:
74
+
75
+ if event.key == K_SPACE:
76
+
77
+ is_space_down = True
78
+
79
+
80
+
81
+ if not game_over:
82
+
83
+ score += 10
84
+
85
+ velocity += -3 if is_space_down else 3
86
+
87
+ ship_y += velocity
88
+
89
+
90
+
91
+ edge = holes[-1].copy()
92
+
93
+ test = edge.move(0,slope)
94
+
95
+ if test.top <= 0 or test.bottom >=600:
96
+
97
+ slope = randint(1,6)*(-1 if slope >0 else 1)
98
+
99
+ edge.inflate_ip(0,-20)
100
+
101
+ edge.move_ip(10,slope)
102
+
103
+ holes.append(edge)
104
+
105
+ del holes[0]
106
+
107
+ holes = [x.move(-10,0) for x in holes]
108
+
109
+
110
+
111
+ if holes[0].top > ship_y or \
112
+
113
+ holes[0].bottom < ship_y + 80:
114
+
115
+ game_over = True
116
+
117
+
118
+
119
+ SURFACE.fill((0,255,0))
120
+
121
+ for hole in holes:
122
+
123
+ pygame.draw.rect(SURFACE,(0,0,0),hole)
124
+
125
+ SURFACE.blit(ship_image,(0,ship_y))
126
+
127
+ score_image = sysfont.render('score is {}'.format(score),True,(0,0,225))
128
+
129
+ SURFACE.blit(score_image,(600,20))
130
+
131
+
132
+
133
+ if game_over:
134
+
135
+ SURFACE.blit(bang_image,(0,ship_y-40))
136
+
137
+
138
+
139
+ pygame.display.update()
140
+
141
+ FSPCLOCK.tick(15)
142
+
143
+
144
+
145
+ if __name__ == '__main__':
146
+
147
+ main()
148
+
149
+ ```
150
+
151
+
152
+
153
+ そして、実行すると
154
+
155
+
156
+
157
+ ```
158
+
159
+ Traceback (most recent call last):
160
+
161
+ File "a.py", line 68, in <module>
162
+
163
+ main()
164
+
165
+ File "a.py", line 18, in main
166
+
167
+ ship_image = pygame.image.load('ship.png')
168
+
169
+ pygame.error: Couldn't open ship.png
170
+
171
+ ```
172
+
173
+
174
+
175
+ となり、ずっと読み込み中が続きます。