Processingでオセロを作りたい。

Python

1import random 2piece = [[0 for _ in range(8)] for _ in range(8)]3place = ["A", "B", "C", "D", "E", "F", "G", "H"]4input = ""5Game = 16local_name1 = u"Player 1"7local_name2 = u"Player 2"8round_number = 09Input = False10delay_time = False11 12def setup():13 size(600, 600)14 textAlign(CENTER, CENTER)15 if 1 == 2:16 for x in range(10, width, 10):17 line(x, 0, x, height)18 for y in range(10, width, 10):19 line(0, y, width, y)20 if 1==2:21 for i in range(1, 9, 1):22 if i==1:23 print("\t\tif key == \""+str(i)+"\":")24 else:25 print("\t\telif key == \""+str(i)+"\":")26 print("\t\t\t\tif len(input) < 3:")27 print("\t\t\t\t\t\tinput += \""+str(i-1)+"\"")28 print("\t\t\t\telse:")29 print("\t\t\t\t\t\tprint(\"Press ENTER key\")")30 print("\t\telif key == ENTER:")31 reset()32 33def reset():34 global Game, piece, delay_time 35 Game = random.randint(1, 2)36 for x in range(8):37 for y in range(8):38 piece[x][y] = 039 piece[3][3] = piece[4][4] = 140 piece[3][4] = piece[4][3] = 241 if frameCount > 1:42 print(u"Resetしました。")43 delay_time = 144 45def draw():46 global sizes, Input, delay_time 47 background(125)48 sizes = width*0.0549 textAlign(CENTER, CENTER)50 for i in range(8):51 fill(0)52 textSize(25)53 text(place[i]+"("+str(i+1)+")", (width-sizes*2)/8*i+sizes*2, sizes/2-3)54 text(str(i+1), sizes/2, (height-sizes*2)/8*i+sizes*2)55 textSize(20)56 text("("+place[i]+")", height-sizes/2+1, (height-sizes*2)/8*i+sizes*2)57 stroke(125, 255, 125, 100)58 stroke(0)59 strokeWeight(3)60 fill(0,140,0)61 sizes = width*0.0562 rect(sizes, sizes, width-sizes*2, height-sizes*2)63 for i in range(1, 8):64 line(sizes, i * (height-sizes*2) / 8 + sizes, width-sizes, i * (height-sizes*2) / 8 + sizes)65 line(i * (width-sizes*2) / 8 + sizes, sizes, i * (width-sizes*2) / 8 + sizes, height-sizes)66 noStroke()67 sum_b = 068 sum_w = 069 for y in range(8):70 for x in range(8):71 if piece[y][x] == 1:72 fill(0)73 sum_b += 174 elif piece[y][x] == 2:75 fill(255)76 sum_w += 177 elif piece[y][x] == 3:78 fill(100, 100, 100, 100)79 else:80 noFill()81 ellipse(x * (width-sizes*2) / 8 + sizes*2.1 , y * (width-sizes*2) / 8 + sizes*2.1, 33, 33)82 textAlign(RIGHT, BASELINE)83 if sum_b < sum_w:84 fill(255)85 elif sum_b > sum_w:86 fill(0)87 else:88 fill(255, 0, 0)89 text("Black: "+ str(sum_b) +" White: "+ str(sum_w), width-5, height-5)90 textAlign(CENTER, BASELINE)91 if Game:92 fill(0,0,255)93 if Game==1 and sum_b + sum_w < 64:94 text(u"It's "+local_name1+u"'s turn.", 250, height-5)95 elif Game==2 and sum_b + sum_w < 64:96 text(u"It's "+local_name2+u"'s turn.", 250, height-5)97 else:98 text(u"Please press \"R\"", 250, height-5)99 if Game == 1 and sum_b + sum_w < 64:100 fill(255)101 stroke(255)102 elif Game == 2 and sum_b + sum_w < 64:103 fill(0)104 stroke(0)105 else:106 fill(255-frameCount%255)107 noStroke()108 ellipse(90, height-15, 20, 20)109 ellipse(390, height-15, 20, 20)110 noStroke()111 textAlign(RIGHT, BASELINE) 112 if 1 == 2:113 if frameCount%500 == 1:114 for l in range(8):115 print piece[l]116 print("-"*14)117 for y in range(8):118 for x in range(8):119 x2 = x * (width-sizes*2) / 8 + sizes 120 y2 = y * (height-sizes*2) / 8 + sizes 121 noFill()122 if (x2 <= mouseX <= x2+(width-sizes*2) / 8 and y2 <= mouseY <= y2+(height-sizes*2) / 8) and piece[y][x] == 3:123 fill(100, 100, 100, 150)124 elif len(input) == 2:125 if (x == int(input[0]) and y == int(input[1])) and piece[int(input[1])][int(input[0])] != 3:126 fill(255, 0, 0, 80)127 elif x == int(input[0]) and y == int(input[1]):128 fill(100, 100, 100, 150)129 rect(x2, y2, (width-sizes*2) / 8, (height-sizes*2) / 8)130 textSize(20)131 fill(50)132 if piece[y][x] == 3:133 text(str(x+1)+place[y].swapcase(), x2+(width-sizes*2) / 8-3, y2+(height-sizes*2) / 8-5)134 textAlign(LEFT,CENTER)135 if len(input) == 2:136 text(str(int(input[0])+1)+str(int(input[1])+1), 15, height-15)137 elif len(input) == 1:138 text(str(int(input[0])+1), 15, height-15)139 else:140 if sum_b + sum_w < 64:141 text("INPUT", 10, height-15)142 if delay_time != False and delay_time > 360:143 delay_time = False144 if 0 <= delay_time <= 360:145 fill(0, 0, 255)146 arc( 17, 17, 29, 29, radians(270), radians(270+delay_time))147 if delay_time != False:148 delay_time += 0.5149 150def keyPressed():151 global input, Game, Input, delay_time 152 if key == "1" or key == "a" or key == "A":153 if len(input) < 2:154 input += "0"155 else:156 print("Press ENTER key")157 elif key == "2" or key == "b" or key == "B":158 if len(input) < 2:159 input += "1"160 else:161 print("Press ENTER key")162 elif key == "3" or key == "c" or key == "C":163 if len(input) < 2:164 input += "2"165 else:166 print("Press ENTER key")167 elif key == "4" or key == "d" or key == "D":168 if len(input) < 2:169 input += "3"170 else:171 print("Press ENTER key")172 elif key == "5" or key == "e" or key == "E":173 if len(input) < 2:174 input += "4"175 else:176 print("Press ENTER key")177 elif key == "6" or key == "f" or key == "F":178 if len(input) < 2:179 input += "5"180 else:181 print("Press ENTER key")182 elif key == "7" or key == "g" or key == "G":183 if len(input) < 2:184 input += "6"185 else:186 print("Press ENTER key")187 elif key == "8" or key == "h":188 if len(input) < 2:189 input += "7"190 else:191 print("Press ENTER key")192 elif key == "R":193 if delay_time == False:194 reset()195 input = ""196 elif key == DELETE:197 if len(input) >= 1:198 input = ""199 print("Input Clear")200 elif key == BACKSPACE:201 if len(input) >= 1:202 input = input[:-1]203 elif key == ENTER:204 if len(input) == 2:205 try:206 if Game == 1:207 if piece[int(input[1])][int(input[0])] == 3:208 piece[int(input[1])][int(input[0])] = 1209 print("Successful")210 print(int(input[1])+1, int(input[0])+1)211 Input = (1, input[1], input[1])212 Game = 2213 else:214 print("Please re-enter") 215 elif Game == 2:216 if piece[int(input[1])][int(input[0])] == 3:217 piece[int(input[1])][int(input[0])] = 2218 print("Successful")219 print(int(input[1])+1, int(input[0])+1)220 Input = (2, input[1], input[1])221 Game = 1222 else:223 print("Please re-enter") 224 except:225 print("Please re-enter")226 else:227 print("Please re-enter")228 input = ""229 if len(input) >= 1:230 print(input)231 232def mousePressed():233 global Game 234 for y in range(8):235 for x in range(8):236 x2 = x * (width-sizes*2) / 8 + sizes 237 y2 = y * (height-sizes*2) / 8 + sizes 238 noFill()239 if (x2 <= mouseX <= x2+(width-sizes*2) / 8 and y2 <= mouseY <= y2+(height-sizes*2) / 8) and piece[y][x] == 3:240 if Game == 1:241 piece[y][x] = 1242 print("Successful")243 print(x+1, y+1)244 Game = 2245 elif Game == 2:246 piece[y][x] = 2247 print("Successful")248 print(x+1, y+1)249 Game = 1

コメントを投稿

0 コメント