質問するログイン新規登録

質問編集履歴

1

マークダウンにし、プログラムの動きなどを記述

2017/08/01 10:34

投稿

DANNBU
DANNBU

スコア19

title CHANGED
File without changes
body CHANGED
@@ -9,7 +9,7 @@
9
9
  お願いします。
10
10
 
11
11
  ###該当のソースコード
12
-
12
+ ```lang-python
13
13
  from PyQt5.QtCore import (QLineF, QPointF, QRectF, Qt)
14
14
  from PyQt5.QtGui import (QBrush, QColor, QPainter)
15
15
  from PyQt5.QtWidgets import (QWidget, QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem,
@@ -21,6 +21,7 @@
21
21
  class TicTacToe(QGraphicsItem):
22
22
  def __init__(self):
23
23
  super(TicTacToe, self).__init__()
24
+ #盤の情報記憶
24
25
  self.board = [[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
25
26
  [-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
26
27
  [-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],[-1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
@@ -35,7 +36,8 @@
35
36
  self.siro = -2
36
37
  self.space = 0
37
38
  self.now = self.kuro
39
+
38
-
40
+ #盤の情報を設定
39
41
  def reset(self):
40
42
  for y in range(16):
41
43
  for x in range(16):
@@ -47,7 +49,8 @@
47
49
  def select(self, x, y):
48
50
  if x < 1 or y < 1 or x > 15 or y > 15:
49
51
  return
50
-
52
+ #self.board[y][x]が0であればself.turnを1にし黒の番にする
53
+ #self.board[y][y]が1であればself.turnを-2にし白の番にする
51
54
  if self.board[y][x] == -1:
52
55
  if self.turn == 0:
53
56
  self.turn = 1 - self.turn
@@ -67,12 +70,7 @@
67
70
 
68
71
  def paint(self, painter, option, widget):
69
72
  painter.setPen(Qt.black)
70
- """
73
+
71
- painter.drawLine(0,100,300,100)
72
- painter.drawLine(0,200,300,200)
73
- painter.drawLine(100,0,100,300)
74
- painter.drawLine(200,0,200,300)
75
- """
76
74
  self.goban_left_top_x = 30
77
75
  self.goban_left_top_y = 30
78
76
  self.length_of_between = 30
@@ -84,26 +82,14 @@
84
82
  painter.setBrush(QColor(255, 80, 0, 160))
85
83
  painter.drawRect(self.goban_left_top_x, self.goban_left_top_y ,self.goban_left_top_x * (self.goban_line_num - 1),self.goban_left_top_y * (self.goban_line_num - 1))
86
84
 
87
-
88
- #for x in range(30,300,30)
85
+ #盤の黒い縦線と横線の記述
89
- #painter.drawLine(x,30,x,270)
90
86
  for x in range(self.goban_left_top_x, self.goban_left_top_x + self.length_of_between * self.goban_line_num, self.length_of_between):
91
87
  painter.drawLine(x, self.goban_left_top_y, x, self.goban_left_top_y + self.length_of_between * (self.goban_line_num - 1))
92
- #for y in range(30,300,30)
88
+
93
89
  for y in range(self.goban_left_top_y, self.goban_left_top_y + self.length_of_between * self.goban_line_num, self.length_of_between):
94
90
  painter.drawLine(self.goban_left_top_x, y, self.goban_left_top_x + self.length_of_between * (self.goban_line_num - 1), y)
95
- #始点(30,30) 終点(270,270)
91
+
96
- """
92
+
97
- for y in range(3):
98
- for x in range(3):
99
- if self.board[y][x] == self.O:
100
- painter.setPen(Qt.red)
101
- painter.drawEllipse(QPointF(50+x*100, 50+y*100), 30, 30)
102
- elif self.board[y][x] == self.X:
103
- painter.setPen(Qt.blue)
104
- painter.drawLine(20+x*100, 20+y*100, 80+x*100, 80+y*100)
105
- painter.drawLine(20+x*100, 80+y*100, 80+x*100, 20+y*100)
106
- """
107
93
  for y in range(1, 16):
108
94
  for x in range(1, 16):
109
95
  cross_point_x = self.goban_left_top_x + self.length_of_between * (x - 1)
@@ -118,10 +104,10 @@
118
104
  def boundingRect(self):
119
105
  return QRectF(0,0,600,600)
120
106
 
107
+ #マウスを押したときのイベント(黒の縦線と横線の交差するところに碁を置くための記述)
121
108
  def mousePressEvent(self, event):
122
109
  pos = event.pos()
123
- #self.select(int(pos.x()/100), int(pos.y()/100))
110
+
124
-
125
111
  hantei_ryouiki_satan = int(self.goban_left_top_x - int(self.length_of_between / 2))
126
112
  x_from_hantei_ryouiki_satan = int(pos.x()) - hantei_ryouiki_satan
127
113
 
@@ -143,72 +129,38 @@
143
129
 
144
130
  def initUI(self):
145
131
  self.graphicsView = QGraphicsView()
146
- #scene = QGraphicsScene(self)
147
- scene = QGraphicsScene(self.graphicsView)
132
+ scene = QGraphicsScene(self.graphicsView)
148
- #scene.setSceneRect(0, 0, 100, 100)
149
133
  self.graphicsView.setScene(scene)
150
134
  self.tic_tac_toe = TicTacToe()
151
135
  scene.addItem(self.tic_tac_toe)
152
- """
136
+
153
- self.kuro_button = QPushButton('黒', self)
154
- self.kuro_button.setCheckable(True)
155
- self.kuro_button.clicked[bool].connect(self.setColor)
156
- #scene.addItem(self.kuro_button)
157
- self.siro_button = QPushButton('白', self)
158
- self.siro_button.setCheckable(True)
159
- self.siro_button.clicked[bool].connect(self.setColor)
160
- #scene.addItem(self.siro_button)
161
- self.space_button = QPushButton('なし', self)
162
- self.space_button.setCheckable(True)
163
- self.space_button.clicked[bool].connect(self.setColor)
164
- #scene.addItem(self.space_button)
165
- """
166
137
  vbox = QVBoxLayout()
167
- #vbox.addWidget(self.kuro_button)
138
+
168
- #vbox.addWidget(self.siro_button)
169
- #vbox.addWidget(self.space_button)
170
139
  vbox.addWidget(self.graphicsView)
171
140
  self.setLayout(vbox)
172
-
173
- #scene.setSceneRect(0, 0, 300, 300)
141
+
174
-
175
142
  #画面の大きさ
176
143
  scene.setSceneRect(0, 0, 600, 600)
177
-
178
- #self.setScene(scene)
179
- #self.graphicsView.setScene(scene)
180
- #self.setCacheMode(QGraphicsView.CacheBackground)
181
-
182
144
  mainLayout = QHBoxLayout()
183
145
  mainLayout.addLayout(vbox)
184
146
 
185
- #self.setLayout(mainLayout)
147
+
186
148
  self.setWindowTitle("Tic Tac Toe")
149
+
187
-
150
+ #Rキーを押したら終了する
188
151
  def keyPressEvent(self, event):
189
152
  key = event.key()
190
153
  if key == Qt.Key_R:
191
154
  self.tic_tac_toe.reset()
192
155
  super(MainWindow, self).keyPressEvent(event)
193
-
194
- def setColor(self, pressed):
156
+
195
- source = self.sender()
157
+
196
-
197
- if source.text() == "黒":
158
+
198
- self.tic_tac_toe.now = self.tic_tac_toe.kuro
199
- elif source.text() == "白":
200
- self.tic_tac_toe.now = self.tic_tac_toe.siro
201
- elif source.text() == "なし":
202
- self.tic_tac_toe.now = self.tic_tac_toe.space
203
-
204
-
205
-
206
-
207
-
208
159
  if __name__ == '__main__':
209
160
  import sys
210
161
  app = QApplication(sys.argv)
211
162
  mainWindow = MainWindow()
212
163
 
213
164
  mainWindow.show()
214
- sys.exit(app.exec_())
165
+ sys.exit(app.exec_())
166
+ ```