class Game:
    def __init__(self, time):
        
        self.time = time
        self.canv = Canvas(40,30)
        self.ball = Ball(2, int(self.canv.getHeight()/2) )
        self.direction = "left"
        self.gameover = False
        self.updatecounter = 0
        self.score = 0
        #threading.Thread(target=self.updateDirection).start() #put this in commentary to run test_game.py
        self.velocityballx = -1
        self.velocitybally = -1
        self.receivedInput = False
        
        self.walls = []
        self.tails = []
        self.goals = []
        self.snake = Snake(int(self.canv.getWidth()/2),int(self.canv.getHeight()/2))
        self.createWalls()
        #self.game_loop() #put this in commentary to run test_game.py
        os.system("clear")
        print("GAME OVER\nYour score was: " + str(self.score)+ "\nPress any key to go back to the terminal.\n")
        

    def createWalls(self):
        for y in range(self.canv.getHeight()):
            for x in range(self.canv.getWidth()):
                if(x==0 or  y==0 or y == self.canv.getHeight() -1):
                    self.walls.append(Wall(x,y))
                if( x == self.canv.getWidth()-1):
                    self.goals.append(Goal(x,y))



    def game_loop(self):
        
        while not self.gameover:
            
            self.update()
                
            self.render()
            time.sleep(self.time)
            

    def update(self):
        
        if (self.updatecounter == 2):
            self.updateBall()
            self.updatecounter = 0
        self.updatecounter += 1
        
        self.updateTail()
        self.updateSnake()
        self.collisionDetection()

    def ballMovement(self):
        self.ball.move(self.ball.x()+self.velocityballx,self.ball.y()+self.velocitybally)
        

    def updateBall(self):
        for tail in self.tails:
            if((tail.y() == self.ball.y()) & ((tail.x()-1 == self.ball.x()) or (tail.x()-1 == self.ball.x())) and self.velocityballx > 0):
                self.velocityballx = -(self.velocityballx)
                self.score += 1

        if(self.snake.x()-1 == self.ball.x() & self.snake.y() == self.ball.y()):
            self.velocityballx = -(self.velocityballx)
            self.score += 1

        if (self.ball.x() == 1):
            #ball hitting left wall 
            self.velocityballx = -(self.velocityballx)
        if(self.ball.y() == self.canv.getHeight() -2 or self.ball.y() == 1):
            #ball hitting lower or upper wall
            self.velocitybally = -(self.velocitybally)
        
        self.ballMovement()
        
    def updateDirection(self):
        while not self.gameover:
            key = getwch()
            if (key == "q") & (self.direction is not "right") & (not self.receivedInput) :
                self.direction = "left"
                self.receivedInput = True
            if (key == "d") & (self.direction is not "left") & (not self.receivedInput):
                self.direction = "right"
                self.receivedInput = True
            if (key == "z") & (self.direction is not "down") & (not self.receivedInput):
                self.direction = "up"
                self.receivedInput = True
            if (key == "s") & (self.direction is not "up") & (not self.receivedInput):
                self.direction = "down"
                self.receivedInput = True


    def updateSnake(self):
        
        if self.direction =="left":
            self.snake.move(self.snake.x()-1,self.snake.y()) #left
        if self.direction == "right":
            self.snake.move(self.snake.x()+1,self.snake.y()) #right
        if self.direction == "up":
            self.snake.move(self.snake.x(),self.snake.y()-1) #right
        if self.direction == "down":
            self.snake.move(self.snake.x(),self.snake.y()+1) #right
        self.receivedInput = False
        
    def updateTail(self):
        taillength = 10
        if len(self.tails) < taillength:
            self.addTailPiece()
        if len(self.tails) >= taillength:
            self.addTailPiece()
            self.deleteLastTailPiece()

    def render(self):
        os.system("clear")
        self.canv.clearCanvas()

        for goal in self.goals:
            goal.render(self.canv)

        for wall in self.walls:
            wall.render(self.canv)

        for tail in self.tails:
            tail.render(self.canv)

        self.snake.render(self.canv,self.direction)

        self.ball.render(self.canv)

        self.canv.outputCanvasTerminal()
        print("Score: " + str(self.score))

    def addTailPiece(self):
        self.tails.append(Tail(self.snake.x(),self.snake.y()))

    def deleteLastTailPiece(self):
        self.tails.pop(0)

    def collisionDetection(self):
        self.collisionWall()
        self.collisionTail()
        self.collisionGoal()

    def collisionWall(self):
        for wall in self.walls:
            if((wall.x() == self.snake.x()) & (wall.y() == self.snake.y())):
                self.gameover = True

    def collisionTail(self):
        for tail in self.tails:
            if((tail.x() == self.snake.x()) & (tail.y() == self.snake.y())):
                self.gameover = True

    def collisionGoal(self):
        #ball with goal:
        if(self.ball.x() == self.canv.getWidth()-1):
            self.gameover = True
        
        #snake with goal:
        if(self.snake.x() == self.canv.getWidth()-1):
            self.gameover = True
Пример #2
0
class MainWindow(QWidget):

    rgba_changed_sgnl = pyqtSignal(int, int, int, int)
    stroke_width_changed_sgnl = pyqtSignal(int)

    def __init__(self):
        super().__init__()

        self.pages = Pages()
        self.pages.page_changed_sgnl.connect(self.pageChanged)

        self.background_color = QColor('#000000')
        self.foreground_col = QColor('#00FF00')

        #        self.setStyleSheet("QLabel { color : #FFFFFF; }; QPushButton {background-color : #00FF00; color : #FF0000}");
        self.setStyleSheet(
            """  QLabel { color: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #AABBAA, stop:1 #AAFFAA); } 
                                QPushButton { background-color : #002200; color : #AAFFAA; }
                                QLineEdit { background-color : #004400; color : #AAFFAA; }
                                
                                QSlider::groove:horizontal {
                                    border: 1px solid #000000;
                                    height: 8px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
                                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #005500, stop:1 #008800);
                                    margin: 2px 0;
                                }
                                
                                QSlider::handle:horizontal {
                                    background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #00BB55, stop:1 #00DD88);
                                    border: 1px solid #5c5c5c;
                                    width: 20px;
                                    margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                                    border-radius: 3px;
                                }
                           """)

        self.initUI()

    def paintEvent(self, event):
        painter = QPainter(self)
        self.painter = painter

        self.paintBackground()

        painter.end()
        del painter

    def paintBackground(self):
        self.painter.setBrush(QBrush(self.background_color))
        self.painter.drawRect(0, 0, 10000, 10000)

    def initUI(self):
        mainLay = widgets.QVBoxLayout()
        self.mainLay = mainLay
        mainLay.setContentsMargins(0, 0, 0, 0)
        mainLay.setSpacing(0)

        self.optionslay = widgets.QVBoxLayout()
        self.optionslay.setContentsMargins(0, 0, 0, 0)
        self.optionslay.setSpacing(0)
        self.optionswidget = widgets.QWidget()
        self.optionswidget.setLayout(self.optionslay)
        self.optionswidget.setMaximumHeight(220)

        self.canvas = Canvas()
        self.canvas.setPointsData(self.pages.getPage())
        self.connectCanvas()

        mainLay.addWidget(self.canvas)
        self.genHideBar()
        mainLay.addWidget(self.optionswidget)
        self.genDoUndoBar()
        self.genPageBar()
        self.genSliderGrid()
        self.generateColorPicker()
        self.genColorPickerGrid()
        self.generateStrokeWidthSlider()
        self.genSaveBar()

        self.setLayout(mainLay)

        self.optionswidget.setVisible(not self.optionsHidden)

        self.setGeometry(300, 300, 800, 600)
        self.setWindowTitle('Hand Writing Notes')
        self.show()

    def genSliderGrid(self):
        self.slider_grid = widgets.QGridLayout()
        self.slider_grid.setHorizontalSpacing(0)

        self.slider_grid.setContentsMargins(10, 10, 10, 10)
        self.slider_grid.setColumnStretch(0, 0)
        self.slider_grid.setColumnStretch(1, 1)
        self.slider_grid.setColumnStretch(2, 1)

        self.optionslay.addLayout(self.slider_grid)

    def genDoUndoBar(self):
        self.clearBtn = widgets.QPushButton('Clear Canvas')
        self.clearBtn.clicked.connect(self.clearCanvas)
        self.undoBtn = widgets.QPushButton('Undo')
        self.undoBtn.clicked.connect(self.undo)
        self.redoBtn = widgets.QPushButton('Redo')
        self.redoBtn.clicked.connect(self.redo)
        clearlay = widgets.QHBoxLayout()
        clearlay.setSpacing(20)
        clearlay.setContentsMargins(5, 5, 5, 5)
        clearlay.addWidget(self.clearBtn)
        clearlay.addWidget(self.undoBtn)
        clearlay.addWidget(self.redoBtn)

        self.optionslay.addLayout(clearlay)

    def genSaveBar(self):
        self.saveBtn = widgets.QPushButton('Save')
        self.saveBtn.clicked.connect(self.save)
        self.saveAsBtn = widgets.QPushButton('Save As')
        self.saveAsBtn.clicked.connect(self.saveAs)
        self.openBtn = widgets.QPushButton('Open')
        self.openBtn.clicked.connect(self.openPages)
        savelay = widgets.QHBoxLayout()
        savelay.setSpacing(20)
        savelay.setContentsMargins(5, 5, 5, 5)
        savelay.addWidget(self.saveBtn)
        savelay.addWidget(self.saveAsBtn)
        savelay.addWidget(self.openBtn)

        self.optionslay.addLayout(savelay)

    @pyqtSlot()
    def connectCanvas(self):
        self.rgba_changed_sgnl.connect(self.canvas.rgba_changed)
        self.stroke_width_changed_sgnl.connect(self.canvas.setStrokeWidth)
        self.canvas.dataUpdater = self.pages.updateCurPageData
        self.canvas.dataRetriever = self.pages.getPage
        self.pages.page_changed_sgnl.connect(self.canvas.retrieveUpdatedData)

    @pyqtSlot()
    def save(self):
        self.pages.save()

    @pyqtSlot()
    def saveAs(self):
        self.pages.saveAs()

    @pyqtSlot()
    def openPages(self):
        filepath = self.pages.filePath()
        newpages = Pages.openFile(filepath)
        if newpages:
            self.pages = newpages
            self.canvas.setPointsData(self.pages.getPage())
            self.connectCanvas()
            self.updateControls()
        else:
            print('could not open pages')

    @pyqtSlot()
    def updateControls(self):
        self.update_page_le()

    @pyqtSlot()
    def clearCanvas(self):
        self.canvas.clearCanvas()

    @pyqtSlot()
    def undo(self):
        self.pages.undo()

    @pyqtSlot()
    def redo(self):
        self.pages.redo()

    def genHideBar(self):
        hidelay = widgets.QHBoxLayout()
        hidelay.setContentsMargins(5, 5, 5, 5)
        hidelay.setSpacing(0)

        self.optionsHidden = True

        self.hideOptionsBtn = HoverButton()
        self.hideOptionsBtn.onMouseEntered = self.hideOptions
        self.setHideButtonIcon()

        hidelay.addWidget(self.hideOptionsBtn)
        self.mainLay.addLayout(hidelay)

    def setHideButtonIcon(self):
        icon_path = "pics/arrow_{0}.png".format(
            "up" if self.optionsHidden else "down")
        setIconToButton(self.hideOptionsBtn, icon_path, QSize(200, 30))

    @pyqtSlot()
    def hideOptions(self):
        self.optionsHidden = not self.optionsHidden
        self.setHideButtonIcon()
        self.setHideOptionsBtnIcon()

    def setHideOptionsBtnIcon(self):
        if self.optionsHidden:
            self.optionswidget.setVisible(False)
        else:
            self.optionswidget.setVisible(True)

    def genColorPickerGrid(self):
        colgrid = widgets.QGridLayout()
        colgrid.setSpacing(0)
        colgrid.setContentsMargins(0, 0, 0, 0)
        low = 40
        rc = genColors(low, 255, 0, 0, 0, 0)
        gc = genColors(0, 0, low, 255, 0, 0)
        bc = genColors(0, 0, 0, 0, low, 255)
        c1 = genColors(low, 255, 0, 150, 0, 0)
        c2 = genColors(low, 255, 0, 0, 0, 150)
        c3 = genColors(0, 0, 0, 150, low, 255)
        c4 = genColors(0, 255, 0, 255, 0, 255)

        for c, rs in enumerate(zip(rc, gc, bc, c1, c2, c3, c4)):
            for r, col in enumerate(rs):
                btn = self.genColorButton(col)
                colgrid.addWidget(btn, r, c)

        self.slider_grid.addLayout(colgrid, 0, 2, 5, 1)

    def setPenColor(self, col):
        q_col = QColor(col)
        r, g, b, a = q_col.red(), q_col.green(), q_col.blue(), q_col.alpha()
        self.red.setValue(
            int(r / 255 * (self.red.maximum() - self.red.minimum())))
        self.grn.setValue(
            int(g / 255 * (self.grn.maximum() - self.grn.minimum())))
        self.blu.setValue(
            int(b / 255 * (self.blu.maximum() - self.blu.minimum())))
        self.alp.setValue(
            int(a / 255 * (self.alp.maximum() - self.alp.minimum())))

    def genColorButton(self, col):
        btn = widgets.QPushButton()
        btn.setStyleSheet("QPushButton {" +
                          " background-color : {0}".format(col.name()) +
                          "; } ")
        setPenCol = lambda: self.setPenColor(col.name())
        btn.clicked.connect(setPenCol)
        btn.setMinimumWidth(10)
        return btn

    def generateStrokeWidthSlider(self):
        grid = self.slider_grid

        self.stroke_fctr = 10

        sw = self.genSlider()  #widgets.QSlider(Qt.Horizontal)
        sw.setValue(self.canvas.strokeWidth() * self.stroke_fctr)

        sw.setMinimum(int(1 * self.stroke_fctr))
        sw.setMaximum(int(20 * self.stroke_fctr))
        sw.valueChanged.connect(self.stroke_width_changed)

        swlgl = self.genLbl('stroke width: ')

        grid.addWidget(swlgl, 4, 0)
        grid.addWidget(sw, 4, 1)  #, Qt.AlignLeft)

    def generateColorPicker(self):
        grid = self.slider_grid

        red = self.genSlider()  #widgets.QSlider(Qt.Horizontal)
        grn = self.genSlider()  #widgets.QSlider(Qt.Horizontal)
        blu = self.genSlider()  #widgets.QSlider(Qt.Horizontal)
        alp = self.genSlider()  #widgets.QSlider(Qt.Horizontal)

        r, g, b, a = self.canvas.rgba()

        red.setValue(int(r * 100. / 255.))
        grn.setValue(int(g * 100. / 255.))
        blu.setValue(int(b * 100. / 255.))
        alp.setValue(int(a * 100. / 255.))

        self.red = red
        self.grn = grn
        self.blu = blu
        self.alp = alp

        red.valueChanged.connect(self.rgba_changed)
        grn.valueChanged.connect(self.rgba_changed)
        blu.valueChanged.connect(self.rgba_changed)
        alp.valueChanged.connect(self.rgba_changed)

        rlbl = self.genLbl('red:  ')
        glbl = self.genLbl('green:')
        blbl = self.genLbl('blue: ')
        albl = self.genLbl('alpha:')

        lay = grid  # self.mainLay

        lay.addWidget(rlbl, 0, 0)
        lay.addWidget(glbl, 1, 0)
        lay.addWidget(blbl, 2, 0)
        lay.addWidget(albl, 3, 0)

        lay.addWidget(red, 0, 1)  #, Qt.AlignLeft)
        lay.addWidget(grn, 1, 1)  #, Qt.AlignLeft)
        lay.addWidget(blu, 2, 1)  #, Qt.AlignLeft)
        lay.addWidget(alp, 3, 1)  #, Qt.AlignLeft)

    def genSlider(self):
        sldr = widgets.QSlider(Qt.Horizontal)
        sldr.setMaximumWidth(200)
        sldr.setMinimumWidth(200)
        return sldr

    def genLbl(self, txt):
        lbl = widgets.QLabel(txt)
        return lbl

    def genPageBar(self):
        prev = widgets.QPushButton('prev')
        nxt = widgets.QPushButton('next')
        page_lbl = widgets.QLabel('page:')
        page = widgets.QLineEdit(
        )  # auf 0 indexiert, linedit aber auf  indexiert
        self.page_le = page
        self.update_page_le()

        prev.clicked.connect(self.prevPage)
        nxt.clicked.connect(self.nextPage)
        page.textChanged.connect(self.setPage)

        pagelay = widgets.QHBoxLayout()
        pagelay.setSpacing(10)
        pagelay.setContentsMargins(5, 5, 5, 5)
        pagelay.addWidget(prev)
        pagelay.addWidget(nxt)
        pagelay.addWidget(page_lbl)
        pagelay.addWidget(page)

        self.optionslay.addLayout(pagelay)

    def update_page_le(self):
        self.page_le.setText('{0}'.format(self.pages.getPageId() + 1))

    @pyqtSlot(int)
    def pageChanged(self, page_id):
        self.update_page_le()

    @pyqtSlot()
    def nextPage(self):
        self.pages.nextPage()

    @pyqtSlot()
    def prevPage(self):
        self.pages.prevPage()

    @pyqtSlot('QString')
    def setPage(self, page_str):
        try:
            page_id = int(
                page_str) - 1  # auf 0 indexiert, linedit aber auf  indexiert
            if self.pages.getPageId() != page_id:
                self.setPage_hlpr(page_id)
        except:
            pass

    def setPage_hlpr(self, page_id):
        self.pages.setPage(page_id)

    @pyqtSlot(int)
    def stroke_width_changed(self, val):
        self.stroke_width_changed_sgnl.emit(val / self.stroke_fctr)

    @pyqtSlot()
    def rgba_changed(self):
        r, g, b, a = self.red.value(), self.grn.value(), self.blu.value(
        ), self.alp.value()
        r = int(r * 255. / 100.)
        g = int(g * 255. / 100.)
        b = int(b * 255. / 100.)
        a = int(a * 255. / 100.)
        self.rgba_changed_sgnl.emit(r, g, b, a)