from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class WindowClass(QWidget):
def __init__(self):
super().__init__()
self.setupUI()
self.show()
def setupUI(self):
self.setGeometry(100, 100,800, 600)
self.cx = 100
self.cy = 100
self.setStyleSheet('background-color: lightgreen')
self.imagelabel = QLabel(self)
pix = QPixmap('Python_workspace\mini_game\image_dir\mimi.png')
self.imagelabel.setPixmap(pix)
self.imagelabel.move(self.cx, self.cy)
def keyPressEvent(self, e):
if e.key() == Qt.Key_Up:
self.cy -= 20
self.imagelabel.move(self.cx, self.cy)
if e.key() == Qt.Key_Down:
self.cy += 20
self.imagelabel.move(self.cx, self.cy)
if e.key() == Qt.Key_Left:
self.cx -= 20
self.imagelabel.move(self.cx, self.cy)
if e.key() == Qt.Key_Right:
self.cx += 20
self.imagelabel.move(self.cx, self.cy)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_W = WindowClass()
sys.exit(app.exec_())
QPainter를 이용한다.
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class WindowClass(QWidget):
def __init__(self):
super().__init__()
self.setupUI()
self.show()
def setupUI(self):
self.setGeometry(100, 100,800, 560)
self.maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 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]
]
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.draw_maze(qp)
qp.end()
def draw_maze(self, qp):
for y in range(7):
for x in range(10):
if self.maze[y][x] == 1:
qp.setBrush(Qt.gray)
qp.setPen(QPen(Qt.black, 3))
qp.drawRect(QRect(x * 80, y * 80, 80, 80))
if __name__ == "__main__":
app = QApplication(sys.argv)
main_W = WindowClass()
sys.exit(app.exec_())
이번에 삽질한 구간은 사각형을 그리는 구간인 drawRect에서 인자는 x좌표, y좌표, width, height 순이라는 것이다.
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class WindowClass(QWidget):
def __init__(self):
super().__init__()
self.setupUI()
self.show()
def setupUI(self):
self.setGeometry(100, 100,800, 560)
self.mx = 1
self.my = 1
self.maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 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.setStyleSheet('background-color: lightgreen')
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()
qp.begin(self)
self.draw_maze(qp)
qp.end()
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)
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
def main_proc(self):
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
main_W = WindowClass()
sys.exit(app.exec_())
여기서 문제가 어떻게 지나온 길을 다시 칠하느냐인데 좀 더 찾아봐야 할 것 같다.