Created
October 1, 2015 12:36
-
-
Save RavenKyu/32987b53b89373c700dd to your computer and use it in GitHub Desktop.
핑퐁1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'raven' | |
import sys | |
from PyQt5 import QtWidgets | |
from PyQt5 import QtCore | |
from PyQt5 import QtGui | |
import random | |
class Bar: | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
self.r = 10 | |
self.mx = 0 | |
self.my = 0 | |
def draw(self, qp): | |
# 원을 그리는 함수 | |
pen = QtGui.QPen(QtCore.Qt.red, 1, QtCore.Qt.SolidLine) | |
qp.setPen(pen) | |
qp.drawRect(self.x, self.y, self.x + 10, self.x + 30) | |
class Ball: | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
self.r = 10 | |
self.mx = (random.sample([-1,1],1))[0] | |
self.my = (random.sample([-1,1],1))[0] | |
def draw(self, qp): | |
# 원을 그리는 함수 | |
pen = QtGui.QPen(QtCore.Qt.red, 1, QtCore.Qt.SolidLine) | |
qp.setPen(pen) | |
qp.drawEllipse(self.x, self.y, self.r, self.r) | |
class Form(QtWidgets.QWidget): | |
def __init__(self, parent=None): | |
QtWidgets.QWidget.__init__(self, parent) | |
self.setGeometry(100, 100, 720, 480) | |
self.init_objects() | |
self.rt = QtCore.QBasicTimer() | |
self.rt.start(11, self) | |
def init_objects(self): | |
self.ww = self.width() | |
self.wh = self.height() | |
x = 10 | |
y = 10 | |
self.ball = Ball(x, y) | |
self.ball.x = random.randrange(100, self.ww) | |
self.ball.y = random.randrange(0, self.wh) | |
self.bar = Bar(10, 10) | |
def keyPressEvent(self, QKeyEvent): | |
if QKeyEvent.key() == QtCore.Qt.Key_Escape: | |
self.init_objects() | |
if QKeyEvent.key() == QtCore.Qt.Key_Up: | |
self.key_up() | |
elif QKeyEvent.key() == QtCore.Qt.Key_Down: | |
self.key_down() | |
def key_up(self): | |
self.bar.y -= 30 | |
def key_down(self): | |
self.bar.y += 30 | |
# 매 턴 마다 실행 | |
def timerEvent(self, e): | |
self.check_crash() | |
self.moving_ball() # 공 움직임 계산 | |
self.repaint() | |
def check_crash(self): | |
if self.ball.x <= (20 + (self.ball.r / 2)): | |
if self.ball.y > self.bar.y and self.ball.y < self.bar.y + 30: | |
self.ball.mx *= -1 | |
if self.ball.x >= self.ww or self.ball.x <= 0: # 화면의 최우측 선에 닿았을 때 | |
self.ball.mx *= -1 | |
if self.ball.y >= self.wh or self.ball.y <= 0: # 화면의 최우측 선에 닿았을 때 | |
self.ball.my *= -1 | |
def draw_bar(self, event, qp): | |
self.bar.draw(qp) | |
def draw_balls(self, event, qp): | |
self.ball.draw(qp) | |
def moving_ball(self): | |
self.ball.x += self.ball.mx | |
self.ball.y += self.ball.my | |
def paintEvent(self, QPaintEvent): | |
qp = QtGui.QPainter() | |
qp.begin(self) | |
self.draw_bar(QPaintEvent, qp) | |
self.draw_balls(QPaintEvent, qp) | |
qp.end() | |
if __name__ == '__main__': | |
app = QtWidgets.QApplication(sys.argv) | |
# app.processEvents(QtCore.QEventLoop.AllEvents) | |
w = Form() | |
w.show() | |
sys.exit(app.exec()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment