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

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

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

Pygameは、ビデオゲームの製作用に設計されたクロスプラットフォームのPythonモジュールセットです。Pythonでコンピューターグラフィックスと音声を扱うためのライブラリが含まれています。

Python

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

Q&A

解決済

1回答

1979閲覧

pygameで処理が終わらない

iPatch

総合スコア9

Pygame

Pygameは、ビデオゲームの製作用に設計されたクロスプラットフォームのPythonモジュールセットです。Pythonでコンピューターグラフィックスと音声を扱うためのライブラリが含まれています。

Python

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

0グッド

0クリップ

投稿2019/10/02 07:58

エラーは出ないが処理が終わらないのですが、どこかインデントに間違いがあるなど、問題点がありますでしょうか。

python

1import sys 2from math import floor 3from random import randint 4import pygame 5from pygame.locals import QUIT,MOUSEBUTTONDOWN 6 7WIDTH = 20 8HEIGHT = 15 9SIZE = 50 10NUM_OF_BOMBS = 20 11EMPTY = 0 12BOMB = 1 13OPENED = 2 14OPEN_COUNT = 0 15CHECKED = [[0 for _ in range(WIDTH)]for _ in range(HEIGHT)] 16 17 18pygame.init() 19SURFACE=pygame.display.set_mode([WIDTH*SIZE,HEIGHT*SIZE]) 20FPSCLOCK=pygame.time.Clock() 21 22 23def num_of_bomb(field,x_pos,y_pos): 24 count=0 25 for yoffset in range(-1,2): 26 for xoffset in range(-1,2): 27 xpos,ypos = (x_pos+xoffset,y_pos+yoffset) 28 if 0 <=xpos<WIDTH and 0<=ypos<HEIGHT and field[xpos][ypos]==BOMB: 29 count+=1 30 return count 31 32 33def open_tile(field,x_pos,y_pos): 34 global OPEN_COUNT 35 if CHECKED[y_pos][x_pos]: 36 return 37 CHECKED[y_pos][x_pos]=True 38 39 for yoffset in range(-1,2): 40 for xoffset in range(-1,2): 41 xpos,ypos = (x_pos + xoffset,y_pos + yoffset) 42 if 0 <=xpos<WIDTH and 0<=ypos<HEIGHRT and filed[xpos][ypos] == EMPTY: 43 filed[xpos][ypos]=OPENED 44 OPEN_COUNT += 1 45 count = num_of_bomb(field,xpos,ypos)num_of_bomb 46 if count == 0 and not(xpos == x_pos and ypos == y_pos): 47 open_tile(field,xpos,ypos) 48 49def main(): 50 smallfont=pygame.font.SysFont(None,36) 51 largefont=pygame.font.SysFont(None,72) 52 message_clear=largefont.render("!!CLEARED!!",True,(0,255,255)) 53 message_over=largefont.render("GAME OVER!!",True,(0,255,255)) 54 message_rect=message_clear.get_rect() 55 56 message_rect.center=(WIDTH*SIZE/2,HEIGHT*SIZE/2) 57 game_over=False 58 field=[[EMPTY for xpos in range(WIDTH)]for ypos in range(HEIGHT)] 59 60 count=0 61 while count < NUM_OF_BOMBS: 62 xpos,ypos=randint(0,WIDTH-1),randint(0,HEIGHT-1) 63 if field[xpos][ypos] == EMPTY: 64 field[xpos][ypos] = BOMB 65 count += 1 66 67 while True: 68 for event in pygame.event.get(): 69 if event.type == QUIT: 70 pygame.quit() 71 sys.exit() 72 if event.type == MOUSEBOTTONDOWN and event.button == 1: 73 xpos,ypos = floor(event.pos[0]/SIZE),floor(event.pos[1]/SIZE) 74 if fleld[ypos][xpos]==BOMB: 75 game_over = True 76 else: 77 open_tile(field,xpos,ypos) 78 79 80 SURFACE.fill((0,0,0)) 81 for ypos in range(HEIGHT): 82 for xpos in range(WIDTH): 83 tile = field[xpos][ypos] 84 rect=(xpos*SIZE,ypos*SIZE,SIZE,SIZE) 85 86 if tile == EMPTY or tile == BOMB: 87 pygame.draw.rect(SURFACE,(192,192,192),rect) 88 89 if game_over and tile == BOMB: 90 pygame.draw.ellipse(SURFACE,(225,225,0),rect) 91 92 elif tile == OPENED: 93 count = num_of_bomb(field,xpos,ypos) 94 if count>0: 95 num_image = smallfont.render("%d"%(count),True,(255,255,0)) 96 SURFACE.blit(num_image,(xpos*SIZE+10,ypos*SIZE+10)) 97 98 for index in range(0,WIDTH*SIZE,SIZE): 99 pygame.draw.line(SURFACE,(96,96,96),(index,0),(index,HEIGHT*SIZE)) 100 for index in range(0,HEIGHT*SIZE,SIZE): 101 pygame.draw.line(SURFACE,(96,96,96),(0,index),(WIDTH*SIZE,index)) 102 103 104 105 if OPEN_COUNT == WIDTH*HEIGHT-NUM_OF_BOMBS: 106 SURFACE.blit(message_clear,message_rect.topleft) 107 108 elif game_over: 109 SURFACE.blit(message_over,message_rect.topleft) 110 111 pygame.display.update() 112 FPSCLOCK.tick(15) 113 114 115if __name__ == '__main__': 116 main()

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

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

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

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

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

guest

回答1

0

ベストアンサー

致命的なのは2点です。

#不具合1
配列の1次元目と2次元時限目誤り。y軸(HEIGHT)が2次元目なので内側にある必要あり。

field=[[EMPTY for xpos in range(WIDTH)]for ypos in range(HEIGHT)]

field=[[EMPTY for ypos in range(HEIGHT)] for xpos in range(WIDTH)]

です。

#不具合2
インデント誤り。

while count < NUM_OF_BOMBS: xpos,ypos=randint(0,WIDTH-1),randint(0,HEIGHT-1) if field[xpos][ypos] == EMPTY: field[xpos][ypos] = BOMB count += 1

while count < NUM_OF_BOMBS: xpos,ypos=randint(0,WIDTH-1),randint(0,HEIGHT-1) if field[xpos][ypos] == EMPTY: field[xpos][ypos] = BOMB count += 1

あとは細かいスペルミスやxpos/yposの指定誤りがありますがそれを直せばマインスイーパーとして遊べました。

投稿2019/10/02 11:17

nomuken

総合スコア1627

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

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

iPatch

2019/10/02 13:08

回答ありがとうございました。多次元配列について理解できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問