from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class WindowClass(QWidget):
def __init__(self):
super().__init__()
self.init_map = 0
self.setupUI()
self.show()
def setupUI(self):
self.setGeometry(100, 100,800, 560)
self.mx = 1
self.my = 1
self.all_filled = 1
self.maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
self.imagelabel = QLabel(self)
pix = QPixmap('Python_workspace\mini_game\image_dir\mimi_s.png').scaled(80, 80)
self.imagelabel.setPixmap(pix)
self.imagelabel.move(self.mx * 80, self.my * 80)
def paintEvent(self, e):
qp = QPainter(self)
self.draw_maze(qp)
self.draw_path(qp)
def draw_maze(self, qp):
for y in range(7):
for x in range(10):
if self.maze[y][x] == 1:
qp.setBrush(QColor(144, 213, 235))
qp.setPen(QPen(Qt.white, 0))
qp.drawRect(x * 80, y * 80, 79, 79)
elif self.maze[y][x] == 2:
qp.setBrush(QColor(249, 225, 236))
qp.setPen(QPen(Qt.white, 0))
qp.drawRect(x * 80, y * 80, 79, 79)
def draw_path(self, qp):
qp.setBrush(QColor(249, 225, 236))
qp.setPen(QPen(Qt.white, 0))
qp.drawRect(self.mx * 80, self.my * 80, 79, 79)
def keyPressEvent(self, e):
if e.key() == Qt.Key_Up and self.maze[self.my - 1][self.mx] == 0:
self.my -= 1
if e.key() == Qt.Key_Down and self.maze[self.my + 1][self.mx] == 0:
self.my += 1
if e.key() == Qt.Key_Left and self.maze[self.my][self.mx - 1] == 0:
self.mx -= 1
if e.key() == Qt.Key_Right and self.maze[self.my][self.mx + 1] == 0:
self.mx += 1
self.imagelabel.move(self.mx * 80, self.my * 80)
if self.maze[self.my][self.mx] == 0:
self.maze[self.my][self.mx] = 2
self.all_filled += 1
self.update()
if self.all_filled == 30:
QMessageBox.about(self, '클리어', '축하합니다! 모든 바닥을 칠하셨습니다.')
if __name__ == "__main__":
app = QApplication(sys.argv)
main_W = WindowClass()
sys.exit(app.exec_())
일단 지나온 길을 칠하는 것에는 성공했으나 기존의 그림에서 추가로 칠하는 것이 아니라 움직일 때마다 맵을 새로 그리는 느낌이라 비효율적이라고 생각하나 이 이외의 방법은 찾지 못했다. 안에 if문을 써서도 해봤는데 이 경우에는 애초에 첫번째 맵도 못그리는 현상이 발생했다. 좀 더 알아볼 필요가 있을 것 같다.