Skip to content

Latest commit

 

History

History
180 lines (160 loc) · 5.93 KB

211222.md

File metadata and controls

180 lines (160 loc) · 5.93 KB

Day 77

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

블록 격파 게임

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
import random
import time
from threading import Thread, Lock


class Ball_Bar_Block:
    def __init__(self, parent):
        # 공
        self.ball_x = 0
        self.ball_y = 0
        self.ball_xp = 0
        self.ball_yp = 0
        # 바
        self.bar_x = 0
        self.bar_y = 540
        # 블록
        self.block = []
        for i in range(5):
            self.block.append([1] * 10)
        for i in range(10):
            self.block.append([0] * 10)
        # 점수, 스테이지
        self.score = 0
        self.stage = 0

        self.parent = parent

    def move_ball(self):
        self.ball_x = self.ball_x + self.ball_xp
        if self.ball_x < 20:
            self.ball_x = 20
            self.ball_xp = -self.ball_xp
        if self.ball_x > 780:
            self.ball_x = 780
            self.ball_xp = -self.ball_xp
        x = int(self.ball_x / 80)
        y = int(self.ball_y / 40)
        if self.block[y][x] == 1:
            self.block[y][x] = 0
            self.ball_xp = -self.ball_xp
            self.score = self.score + 10

        self.ball_y = self.ball_y + self.ball_yp
        if self.ball_y >= 600:
            return
        if self.ball_y < 20:
            self.ball_y = 20
            self.ball_yp = -self.ball_yp
        x = int(self.ball_x / 80)
        y = int(self.ball_y / 40)
        if self.block[y][x] == 1:
            self.block[y][x] = 0
            self.ball_yp = -self.ball_yp
            self.score = self.score + 10

        if self.bar_y - 40 <= self.ball_y and self.ball_y <= self.bar_y:
            if self.bar_x - 80 <= self.ball_x and self.ball_x <= self.bar_x + 80:
                self.ball_yp = -10
                self.score = self.score + 1
            elif self.bar_x - 100 <= self.ball_x and self.ball_x <= self.bar_x - 80:
                self.ball_yp = -10
                self.ball_xp = random.randint(-20, -10)
                self.score = self.score + 2
            elif self.bar_x + 80 <= self.ball_x and self.ball_x <= self.bar_x + 100:
                self.ball_yp = -10
                self.ball_xp = random.randint(10, 20)
                self.score = self.score + 2
    
    def move_bar(self, key):
        if key == Qt.Key_Space and self.parent.bGame == False:
            self.thread.start()
        
        if key == Qt.Key_Left and self.bar_x > 80:
            self.bar_x = self.bar_x - 40
        if key == Qt.Key_Right and self.bar_x < 720:
            self.bar_x = self.bar_x + 40

    def block_color(self, x, y):
        col = QColor(15 - self.x - int(self.y /3), self.x + 1, self.y * 3 + 3)
        return col


class CMap:
    def __init__(self, parent):
        super().__init__()

        self.thread = Thread(target = self.playgame)
        self.bRun = True
        self.bGame = False
        self.is_clr = True
        self.ball = Ball_Bar_Block(self)
        self.parent = parent

        
    def draw(self, qp):
        if self.bRun == False:
            qp.setFont(QFont('Times New Roman', 20, QFont.Bold))
            qp.setPen(QPen(Qt.cyan))
            qp.drawText(400, 300, 'Press [Space] to Play')

        qp.eraseRect(0, 0, 800, 600)
        self.is_clr = True
        for y in range(15):
            for x in range(10):
                gx = x * 80
                gy = y * 40
                if self.ball.block[y][x] == 1:
                    qp.setBrush(self.ball.block_color(x, y))
                    qp.drawRect(gx + 1, gy + 4, gx + 79, gy + 32)
                    self.is_clr = False
        qp.setFont(QFont('Times New Roman', 20, QFont.Bold))
        qp.setPen(QPen(Qt.white))
        qp.drawText(200, 20, 'STAGE ' + str(self.ball.stage))
        qp.drawText(600, 2, 'SCORE ' + str(self.ball.score))

        qp.setBrush(QColor(255, 215, 0))
        qp.setPen(QPen(width=2))
        qp.drawEllipse(self.ball.ball_x - 20, self.ball.ball_y - 20, self.ball.ball_x + 20, self.ball.ball_y + 20)
        qp.setPen(QPen(width=0))
        qp.setBrush(QColor(247, 230, 0))
        qp.drawEllipse(self.ball.ball_x - 16, self.ball.ball_y - 16, self.ball.ball_x + 12, self.ball.ball_y + 12)

        qp.setBrush(QColor(181, 181, 189))
        qp.drawRect(self.ball.bar_x - 80, self.ball.bar_y - 12, self.ball.bar_x + 80, self.ball.bar_y + 12)
        qp.setBrush(QColor(181, 181, 189))
        qp.drawRect(self.ball.bar_x - 78, self.ball.bar_y - 14, self.ball.bar_x + 78, self.ball.bar_y + 14)
        qp.setBrush(QColor(230, 230, 233))
        qp.drawRect(self.ball.bar_x - 78, self.ball.bar_y - 12, self.ball.bar_x + 78, self.ball.bar_y + 12)

    def playgame(self):
        idx = 0
        tmr = 0
        while self.bRun:
            if idx == 0:
                tmr = tmr + 1
                if tmr == 1:
                    stage = 1
                    score = 0
                if tmr == 2:
                    self.ball.ball_x = 160
                    self.ball.ball_y = 240
                    self.ball.ball_xp = 10
                    self.ball.ball_yp = 10
                    self.ball.bar_x = 400
                    idx = 2
            elif idx == 1:
                self.ball.move_ball()
                self.ball.move_bar()
                if self.is_clr == True:
                    idx = 3
                    tmr = 0
            elif idx == 2:
                tmr = tmr + 1
                if tmr == 1:
                    pass

class WindowClass(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Block game')
        pal = QPalette()
        pal.setColor(QPalette.Background, QColor(0, 0, 0))
        self.setAutoFillBackground(True)
        self.setPalette(pal)

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

책의 마지막 부분에 나와있는 블록 격파 게임을 PyQt로 다시 만들어 보는 중이다.