python3でpygameを使っています。
質問が漠然としていて申し訳ございませんが、
君のコードは汚い、オブジェクト指向がわかってないとよく言われます。
具体的にオブジェクト指向が何なのか
そしてこの関数の少なさ、ループの多さをどう解決したらいいのでしょうか。
アドバイス頂きたいと思います。
python
1import pygame 2from pygame.locals import * 3import sys 4import PygameSupportLibrary 5from PygameSupportLibrary import Image 6import math 7 8def color(Num): 9 if Num == 1: 10 return "white" 11 elif Num == 2: 12 return "black" 13 14def load_image(target): 15 return pygame.image.load(target).convert_alpha() 16 17pygame.init() 18pygame.display.set_caption(u"Othelo") 19 20board = [[0 for i in range(8)] for i in range(8)] 21board[3][3] = board[4][4] = 1 22board[3][4] = board[4][3] = 2 23 24turn = 1 25 26screen_SIZE = (500,500) 27screen = pygame.display.set_mode(screen_SIZE) 28 29boardImg = load_image('pic/board.png') 30whiteImg = load_image('pic/white.png') 31blackImg = load_image('pic/black.png') 32# 白石 = 1 , 黒石 = 2 として扱う 33 34myfont = pygame.font.SysFont(None, 60) 35 36def put_stone(ImgNum,PosNum1,PosNum2): 37 global Img 38 if ImgNum == 1: 39 Img = whiteImg 40 elif ImgNum == 2: 41 Img = blackImg 42 Pos1 = PosNum1 * 50 + 20 + (PosNum1 + 1) 43 Pos2 = PosNum2 * 50 + 20 + (PosNum2 + 1) 44 45 return screen.blit(Img,(Pos1,Pos2)) 46 47def switch_turn(p_turn): 48 global turn 49 if p_turn == 1: 50 return 2 51 elif p_turn == 2: 52 return 1 53 54def ret_stone(sta1,sta2,end1,end2,direction): 55 while True: 56 p_sta1 = sta1 + direction[0] 57 p_sta2 = sta2 + direction[1] 58 board[p_sta1][p_sta2] = turn 59 if p_sta1 == end1 and p_sta2 == end2: 60 print(p_sta1,p_sta2,end1,end2,direction) 61 break 62 return 63 64def set_stone(x, y): 65 global turn 66 67 direction = [] 68 69# 座標からクリックされたマスを割り出す 70 Rx = x - 20 71 Ry = y - 20 72 squ1 = math.floor(Rx / (50 + 1)) 73 squ2 = math.floor(Ry / (50 + 1)) 74 75 Con1 = bool # 石があらかじめそこにないこと 76 Con2 = bool # 石に隣接すること 77 Con3 = bool # 盤面の中にあること 78 Con4 = bool # 自分の石で相手の石を挟んでいること 79 80# すでに石が置いてないか → 正常作動 81 if board[squ1][squ2] == 0: 82 Con1 = True 83 else: 84 Con1 = False 85 86# 石に隣接するか → listの外の数が定義されてしまう 87 for i in range(-1,2): 88 for j in range(-1,2): 89 if board[squ1 + i][squ2 + j] == 0 or board[squ1 + i][squ2 + j] == turn: 90 pass 91 else: 92 Con2 = True 93 direction.append([i,j]) 94 95 if Con2 != True: 96 Con2 = False 97 98# ボード上にますがにますが存在するか → 正常作動 99 if 0 < Rx < 409 and 0 < Ry < 409: 100 Con3 = True 101 else: 102 Con3 = False 103 104# 相手の石を挟んでいるか → 製作中 105 for direct in direction: 106 p_squ1 = squ1 107 p_squ2 = squ2 108 while True: 109 p_squ1 = p_squ1 + direct[0] 110 p_squ2 = p_squ2 + direct[1] 111 if p_squ1 == 8 or p_squ1 == -1 or p_squ2 == 8 or p_squ2 == -1: 112 break 113 if board[p_squ1][p_squ2] == turn: 114 Con4 = True 115 ret_stone(squ1,squ2,p_squ1,p_squ2,direct) 116 break 117 else: 118 pass 119 120 if Con4 != True: 121 Con4 = False 122 123# 上記4条件を満たした時実行 124 if Con1 and Con2 and Con3 and Con4 == True: 125 board[squ1][squ2] = turn 126 turn = switch_turn(turn) 127 else: 128 pass 129 130while True: 131 screen.fill((0,0,0)) 132 screen.blit(boardImg,(20,20)) 133 134 str_turn = myfont.render("turn=" + str(color(turn)), True, (225,225,225)) 135 screen.blit(str_turn, (20,434)) 136 137 for i in range(0,8): 138 for j in range(0,8): 139 if board[i][j] == 0: 140 pass 141 elif board[i][j] == 1 or 2: 142 put_stone(board[i][j],i,j) 143 144 pygame.display.update() 145 146 for event in pygame.event.get(): 147 if event.type == QUIT: 148 sys.exit() 149 150 if event.type == MOUSEBUTTONDOWN and event.button == 1: 151 x, y = event.pos 152 set_stone(x, y) 153 154 155 156
補足(2015/8/13):オセロの盤面を作っています。
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/08/13 04:52
2015/08/13 05:29
2015/08/14 15:57 編集