Skip to content

Latest commit

 

History

History
136 lines (112 loc) · 4.82 KB

211215.md

File metadata and controls

136 lines (112 loc) · 4.82 KB

Day 70

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

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

class WindowClass(QWidget):
    def __init__(self):
        super().__init__()
        self.setMouseTracking(True)
        self.setupUI()
        self.worker = Cat(self)
        self.worker.psignal.connect(self.draw_cat)
        self.worker.start()

        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 = [
            [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, 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],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0]
        ]

        self.labels = {}
        for y in range(10):
            for x in range(8):
                txt = str(x) + str(y)
                self.labels[txt] = QLabel(self)
                self.labels[txt].resize(78, 78)
                self.labels[txt].setMouseTracking(True)
                self.labels[txt].move(x * 72 + 21, y * 72 + 21)

        # 커서 이미지
        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)

    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 mousePressEvent(self, e):
        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.neko[self.cursor_y][self.cursor_x] = random.randint(1, 6)
        
    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)

    @pyqtSlot()
    def draw_cat(self):
        for y in range(10):
            for x in range(8):
                txt = str(x) + str(y)
                if self.neko[y][x] > 0:
                    img = self.cat_img[self.neko[y][x]]
                    self.labels[txt].setPixmap(img)
                else:
                    self.labels[txt].setPixmap(QPixmap())



class Cat(QThread):
    psignal = pyqtSignal()

    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent
        
    
    def run(self):
        while(True):
            self.drop_cat()
            self.psignal.emit()
            time.sleep(0.1)

    def drop_cat(self):
        for y in range(8, -1, -1):
            for x in range(8):
                if self.parent.neko[y][x] != 0 and self.parent.neko[y + 1][x] == 0:
                    self.parent.neko[y + 1][x] = self.parent.neko[y][x]
                    self.parent.neko[y][x] = 0


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

오늘은 클릭해서 블록 떨어뜨리는 부분을 제작했다.

우선 저번에 했던 부분에서 잘못한 부분이 있었다.

라벨을 리스트로 반복시켜서 할당한 부분이었는데 오늘 계속 뭔가가 이상해서 Label 객체들을 출력시켜 봤는데 8개의 같은 객체를 돌려쓰고 있는 것을 확인했다. 즉 같은 열은 같은 라벨을 쓰고 있었다는 것이다. 이를 찾는데 너무 많은 시간을 쏟았다. 해결 자체는 간단했는데 xy를 키값으로 딕셔너리로 객체를 만들어주니까 성공했다.

객체의 메모리 영역을 확인하려고 하지 않았으면 해결하지 못했을 문제였다. 이런 특성을 가지고 있는 것을 몰라서 이랬는데 앞으로는 금방 찾아낼 수 있을 것이다.