Skip to content

Latest commit

 

History

History
188 lines (164 loc) · 6.49 KB

211212.md

File metadata and controls

188 lines (164 loc) · 6.49 KB

Day 68

파이썬으로 배우는 게임 개발 입문편

다시 시작 설정

shift 키를 누르면 초기 상태로 되돌아 가도록 설정했다.

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
        if (e.key() == Qt.Key_Shift) and (self.all_filled > 2):
            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.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_())

이렇게 미로를 색칠하는 게임은 끝이 났고 그 다음으로 넘어갔다.

마우스 이벤트

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

class WindowClass(QWidget):
    def __init__(self):
        super().__init__()
        self.setMouseTracking(True)
        self.setupUI()
        self.show()

    def setupUI(self):
        self.setGeometry(100, 100, 912, 768)
        self.mouse_x = 0
        self.mouse_y = 0
        self.cursor_x = 0
        self.cursor_y = 0
        self.neko = [
            [1, 0, 0, 0, 0, 0, 7, 7],
            [0, 2, 0, 0, 0, 0, 7, 7],
            [0, 0, 3, 0, 0, 0, 0, 0],
            [0, 0, 0, 4, 0, 0, 0, 0],
            [0, 0, 0, 0, 5, 0, 0, 0],
            [0, 0, 0, 0, 0, 6, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 2, 3, 4, 5, 6]
        ]
        
        # 커서 이미지
        pixmap = QPixmap('Python_workspace\\mini_game\\image_dir\\neko_cursor.png')
        self.cursor_image = QLabel(self)
        self.cursor_image.setPixmap(pixmap)
        self.cursor_image.move(24, 24)

        # 배경 이미지
        oImage = QImage('Python_workspace\\mini_game\\image_dir\\neko_bg.png')
        sImage = oImage.scaled(QSize(912, 768))
        palette = QPalette()
        palette.setBrush(10, QBrush(sImage))
        self.setPalette(palette)

        self.cat_img = [None] * 8
        # 고양이 이미지
        for i in range(1,7):
            self.cat_img[i] = QPixmap(f'Python_workspace\\mini_game\\image_dir\\neko{i}.png')
        self.cat_img[7] = QPixmap('Python_workspace\\mini_game\\image_dir\\neko_niku.png')
        self.k = QLabel(self)
        self.k.resize(100, 100)
        self.k.move(200, 200)
        self.draw_cat()

    def mouseMoveEvent(self, e):
        self.mouse_x = e.x()
        self.mouse_y = e.y()
        self.visualize_cursor()
        txt = str(self.mouse_x) + ', ' + str(self.mouse_y)
        self.k.setText(txt)

        
    def visualize_cursor(self):    
        if (24 <= self.mouse_x) and (self.mouse_x < 24 + 72 * 8) and (24 <= self.mouse_y) and (self.mouse_y < 24 + 72 * 10):
            self.cursor_x = int((self.mouse_x - 24) / 72)
            self.cursor_y = int((self.mouse_y - 24) / 72)
            self.cursor_image.move(self.cursor_x * 72 + 21, self.cursor_y * 72 + 21)
    
    def draw_cat(self):
        for y in range(10):
            for x in range(8):
                if self.neko[y][x] > 0:
                    tmp = QLabel(self)
                    img = self.cat_img[self.neko[y][x]]
                    tmp.setPixmap(img)
                    tmp.move(x * 72 + 21, y * 72 + 21)
                    tmp.setMouseTracking(True)



if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_W = WindowClass()
    sys.exit(app.exec_())

mouseMoveEvent를 사용했는데 단순하게 마우스가 움직이면 발생하는 이벤트이다. 하지만 setMouseTracking을 True로 해주지 않으면 클릭을 하고 움직일 때만 발생한다. MouseTracking은 위젯마다 따로 설정해 주어야 하는 것 같다.

또한 이미지를 배열에 넣어 배열과 반복문으로 원하는 위치에 원하는 이미지를 넣는 것에도 성공했다.