質問編集履歴
8
#背景色を塗りつぶすの下にコードを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
コイン集めゲームにスコア表示を作りたいです
|
1
|
+
pygameでコイン集めゲームにスコア表示を作りたいです
|
2
2
|
どのようなコードを書けばよいでしょうか
|
3
3
|
試したこと#背景色を塗りつぶすの下に
|
4
4
|
def draw(self):
|
7
#背景色を塗りつぶすの下にコードをくわえました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,13 +1,22 @@
|
|
1
|
-
```ここに言語を入力
|
2
|
-
コード
|
3
|
-
|
1
|
+
コイン集めゲームにスコア表示を作りたいです
|
2
|
+
どのようなコードを書けばよいでしょうか
|
3
|
+
試したこと#背景色を塗りつぶすの下に
|
4
|
+
def draw(self):
|
5
|
+
text = self.font.render("{:04d}".format(self.point), True, (63,255,63))
|
6
|
+
create_text = screen.draw
|
7
|
+
create_text(score, [10, 5])
|
8
|
+
というコードを加えましたがスコア表示ができません
|
9
|
+
|
4
10
|
import pygame
|
5
11
|
import random
|
12
|
+
|
13
|
+
#変数
|
6
|
-
|
14
|
+
score = 0 #スコア用の変数
|
7
15
|
#初期化
|
8
16
|
pygame.init()
|
9
17
|
screen = pygame.display.set_mode([800,600])
|
10
18
|
pygame.display.set_caption("Coin Collector Game")
|
19
|
+
|
11
20
|
|
12
21
|
# プレイヤー関連
|
13
22
|
player_image = pygame.image.load("date/player.png")# 画像を読み込む
|
@@ -17,8 +26,7 @@
|
|
17
26
|
coin_image = pygame.image.load("date/coin.png")# 画像を読み込む
|
18
27
|
coin_image = pygame.transform.scale(coin_image, [80, 80])# 画像サイズを指定する
|
19
28
|
coins_rect = []
|
20
|
-
def place_coin():
|
21
|
-
|
29
|
+
for i in range(6):
|
22
30
|
x = random.randint(0, 700)
|
23
31
|
y = random.randint(0,500)
|
24
32
|
coins_rect.append(pygame.Rect(x, y, coin_image.get_width(), coin_image.get_height()))
|
@@ -28,16 +36,14 @@
|
|
28
36
|
collected_coins = 0
|
29
37
|
|
30
38
|
|
39
|
+
|
40
|
+
|
31
41
|
#ゲームループ
|
32
42
|
running = True
|
33
43
|
while running == True:
|
34
44
|
for event in pygame.event.get():
|
35
45
|
if event.type == pygame.QUIT:
|
36
46
|
running = False
|
37
|
-
# テキストを描画
|
38
|
-
|
39
|
-
def update():
|
40
|
-
global score
|
41
47
|
# キー操作を受け付けてプレイヤーを動かす
|
42
48
|
keys = pygame.key.get_pressed()
|
43
49
|
if keys[pygame.K_LEFT] == True:
|
@@ -50,23 +56,26 @@
|
|
50
56
|
player_rect.y += 1
|
51
57
|
# プレイヤーがコインに触れた時の処理
|
52
58
|
for coin_rect in coins_rect:
|
53
|
-
if player_rect.colliderect(coin
|
59
|
+
if player_rect.colliderect(coin_rect) == True:
|
54
60
|
coins_rect.remove(coin_rect)
|
55
61
|
collected_coins += 1
|
56
62
|
|
57
63
|
#背景色を塗りつぶす
|
58
|
-
screen.fill("blue")
|
64
|
+
screen.fill("blue")
|
65
|
+
def draw(self):
|
66
|
+
text = self.font.render("{:04d}".format(self.point), True, (63,255,63))
|
67
|
+
create_text = screen.draw
|
68
|
+
create_text(score, [10, 5])
|
69
|
+
|
59
70
|
score = collected_coins
|
60
|
-
|
71
|
+
|
61
|
-
|
72
|
+
|
62
73
|
# コインとプレイヤーを描画。
|
63
74
|
for coin_rect in coins_rect:
|
64
75
|
screen.blit(coin_image, coin_rect)
|
65
76
|
screen.blit(player_image, player_rect)
|
66
77
|
pygame.display.update()
|
67
78
|
|
68
|
-
|
69
|
-
|
70
79
|
|
71
80
|
|
72
81
|
# クリア時の処理
|
@@ -76,12 +85,4 @@
|
|
76
85
|
|
77
86
|
# ゲーム終了
|
78
87
|
pygame.quit()
|
79
|
-
スコア表示をしたい
|
80
|
-
例外が発生しました: AttributeError
|
81
|
-
'pygame.surface.Surface' object has no attribute 'draw'
|
82
|
-
File "C:\Users\Owner\Desktop\CoinCollector\coincollector_0825.py", line 57, in <module>
|
83
|
-
screen.draw.txt("score: " +str(score), color="black", topleft=(10, 10))
|
84
|
-
AttributeError: 'pygame.surface.Surface' object has no attribute 'draw'
|
85
|
-
このようなエラーメッセージがでてきますdraw属性がないみたいなんですがどうすれば
|
86
|
-
drawが使えるようになりますか?
|
87
88
|
|
6
#背景色を塗りつぶすの2つ下にコードを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
+
```ここに言語を入力
|
2
|
+
コード
|
1
|
-
pygameでスコア表示を行いたい
|
3
|
+
``` pygameでスコア表示を行いたい
|
2
4
|
import pygame
|
3
5
|
import random
|
4
|
-
|
6
|
+
import pygame.draw
|
5
7
|
#初期化
|
6
8
|
pygame.init()
|
7
9
|
screen = pygame.display.set_mode([800,600])
|
8
10
|
pygame.display.set_caption("Coin Collector Game")
|
9
|
-
|
10
11
|
|
11
12
|
# プレイヤー関連
|
12
13
|
player_image = pygame.image.load("date/player.png")# 画像を読み込む
|
@@ -16,7 +17,8 @@
|
|
16
17
|
coin_image = pygame.image.load("date/coin.png")# 画像を読み込む
|
17
18
|
coin_image = pygame.transform.scale(coin_image, [80, 80])# 画像サイズを指定する
|
18
19
|
coins_rect = []
|
20
|
+
def place_coin():
|
19
|
-
for i in range(6):
|
21
|
+
for i in range(6):
|
20
22
|
x = random.randint(0, 700)
|
21
23
|
y = random.randint(0,500)
|
22
24
|
coins_rect.append(pygame.Rect(x, y, coin_image.get_width(), coin_image.get_height()))
|
@@ -24,20 +26,7 @@
|
|
24
26
|
|
25
27
|
# スコア用
|
26
28
|
collected_coins = 0
|
27
|
-
# スコアクラス
|
28
|
-
class Score():
|
29
|
-
#初期化メソッド
|
30
|
-
def __init__(self):
|
31
|
-
self.font = pygame.font.SysFont("hgep006")
|
32
|
-
self.point = 0
|
33
|
-
# スコア計算
|
34
|
-
def cal_score(self, point):
|
35
|
-
self.point += point * 100
|
36
29
|
|
37
|
-
# スコア描画
|
38
|
-
def draw(self, surface):
|
39
|
-
text = self.font.render("{:04d}".format(self.point), True, (63,255,63))
|
40
|
-
surface.blit(text, [10, 5])
|
41
30
|
|
42
31
|
#ゲームループ
|
43
32
|
running = True
|
@@ -45,6 +34,10 @@
|
|
45
34
|
for event in pygame.event.get():
|
46
35
|
if event.type == pygame.QUIT:
|
47
36
|
running = False
|
37
|
+
# テキストを描画
|
38
|
+
|
39
|
+
def update():
|
40
|
+
global score
|
48
41
|
# キー操作を受け付けてプレイヤーを動かす
|
49
42
|
keys = pygame.key.get_pressed()
|
50
43
|
if keys[pygame.K_LEFT] == True:
|
@@ -57,19 +50,23 @@
|
|
57
50
|
player_rect.y += 1
|
58
51
|
# プレイヤーがコインに触れた時の処理
|
59
52
|
for coin_rect in coins_rect:
|
60
|
-
if player_rect.colliderect(coin_rect) == True:
|
53
|
+
if player_rect.colliderect(coins_rect) == True:
|
61
54
|
coins_rect.remove(coin_rect)
|
62
55
|
collected_coins += 1
|
63
56
|
|
64
57
|
#背景色を塗りつぶす
|
65
58
|
screen.fill("blue")
|
66
|
-
|
59
|
+
score = collected_coins
|
60
|
+
screen.draw.txt("score: " +str(score), color="black", topleft=(10, 10))
|
61
|
+
|
67
62
|
# コインとプレイヤーを描画。
|
68
63
|
for coin_rect in coins_rect:
|
69
64
|
screen.blit(coin_image, coin_rect)
|
70
65
|
screen.blit(player_image, player_rect)
|
71
66
|
pygame.display.update()
|
72
67
|
|
68
|
+
|
69
|
+
|
73
70
|
|
74
71
|
|
75
72
|
# クリア時の処理
|
@@ -80,7 +77,11 @@
|
|
80
77
|
# ゲーム終了
|
81
78
|
pygame.quit()
|
82
79
|
スコア表示をしたい
|
80
|
+
例外が発生しました: AttributeError
|
81
|
+
'pygame.surface.Surface' object has no attribute 'draw'
|
82
|
+
File "C:\Users\Owner\Desktop\CoinCollector\coincollector_0825.py", line 57, in <module>
|
83
|
+
screen.draw.txt("score: " +str(score), color="black", topleft=(10, 10))
|
84
|
+
AttributeError: 'pygame.surface.Surface' object has no attribute 'draw'
|
85
|
+
このようなエラーメッセージがでてきますdraw属性がないみたいなんですがどうすれば
|
86
|
+
drawが使えるようになりますか?
|
83
87
|
|
84
|
-
pygameで上のコードにスコア表示を出したいのですがどうすればいいですか
|
85
|
-
|
86
|
-
|
5
ワークスペースに問題ありませんとなりましたが、スコアが表示されません。あとなにがたりないでしょうか?
test
CHANGED
File without changes
|
test
CHANGED
@@ -27,15 +27,15 @@
|
|
27
27
|
# スコアクラス
|
28
28
|
class Score():
|
29
29
|
#初期化メソッド
|
30
|
-
def __init__(self):
|
30
|
+
def __init__(self):
|
31
31
|
self.font = pygame.font.SysFont("hgep006")
|
32
32
|
self.point = 0
|
33
33
|
# スコア計算
|
34
|
-
def cal_score(self, point):
|
34
|
+
def cal_score(self, point):
|
35
35
|
self.point += point * 100
|
36
36
|
|
37
37
|
# スコア描画
|
38
|
-
def draw(self, surface):
|
38
|
+
def draw(self, surface):
|
39
39
|
text = self.font.render("{:04d}".format(self.point), True, (63,255,63))
|
40
40
|
surface.blit(text, [10, 5])
|
41
41
|
|
@@ -79,7 +79,6 @@
|
|
79
79
|
|
80
80
|
# ゲーム終了
|
81
81
|
pygame.quit()
|
82
|
-
|
83
82
|
スコア表示をしたい
|
84
83
|
|
85
84
|
pygameで上のコードにスコア表示を出したいのですがどうすればいいですか
|
4
インデントされたブロックがひつようです と出ます、どこを直せばいいのでしょうか?
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
pygameでスコア表示を行いたい
|
2
2
|
import pygame
|
3
3
|
import random
|
4
|
-
|
5
|
-
#変数
|
6
|
-
score = 0 #スコア用の変数
|
7
4
|
|
8
5
|
#初期化
|
9
6
|
pygame.init()
|
@@ -27,10 +24,20 @@
|
|
27
24
|
|
28
25
|
# スコア用
|
29
26
|
collected_coins = 0
|
30
|
-
# スコア
|
27
|
+
# スコアクラス
|
28
|
+
class Score():
|
29
|
+
#初期化メソッド
|
30
|
+
def __init__(self):
|
31
|
+
self.font = pygame.font.SysFont("hgep006")
|
32
|
+
self.point = 0
|
33
|
+
# スコア計算
|
31
|
-
def
|
34
|
+
def cal_score(self, point):
|
35
|
+
self.point += point * 100
|
36
|
+
|
37
|
+
# スコア描画
|
38
|
+
def draw(self, surface):
|
32
|
-
txt = font.render(
|
39
|
+
text = self.font.render("{:04d}".format(self.point), True, (63,255,63))
|
33
|
-
sc
|
40
|
+
surface.blit(text, [10, 5])
|
34
41
|
|
35
42
|
#ゲームループ
|
36
43
|
running = True
|
3
スコア表示のみ実行したいです
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
pygameで
|
1
|
+
pygameでスコア表示を行いたい
|
2
|
-
|
3
2
|
import pygame
|
4
3
|
import random
|
5
4
|
|
2
スコア表示処理だけしたいです。
test
CHANGED
File without changes
|
test
CHANGED
@@ -74,10 +74,8 @@
|
|
74
74
|
# ゲーム終了
|
75
75
|
pygame.quit()
|
76
76
|
|
77
|
-
制限時間を表示したい
|
78
|
-
スコア表示したい
|
77
|
+
スコア表示をしたい
|
78
|
+
|
79
|
+
pygameで上のコードにスコア表示を出したいのですがどうすればいいですか
|
79
80
|
|
80
81
|
|
81
|
-
pygameで上のコードに制限時間とスコア表示を出したいのですがどうすればいいですか
|
82
|
-
|
83
|
-
|
1
#スコア用のコードの下に#スコア表示処理というコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
|
6
6
|
#変数
|
7
7
|
score = 0 #スコア用の変数
|
8
|
+
|
8
9
|
#初期化
|
9
10
|
pygame.init()
|
10
11
|
screen = pygame.display.set_mode([800,600])
|
@@ -28,8 +29,9 @@
|
|
28
29
|
# スコア用
|
29
30
|
collected_coins = 0
|
30
31
|
# スコア表示処理
|
31
|
-
|
32
|
+
def display_score_collected_coins():
|
32
|
-
|
33
|
+
txt = font.render(str(score),True,"black")
|
34
|
+
screen.blit(txt[800, 600])
|
33
35
|
|
34
36
|
#ゲームループ
|
35
37
|
running = True
|