Пример #1
0
class GameEngine:
    def __init__(self, screensize):
        spi = SPI(1, baudrate=20000000, polarity=0, phase=0,
                  sck=Pin(SPI_SCK), mosi=Pin(SPI_SDO), miso=None)
        # def __init__( self, spi, aDC, aReset, aCS) :
        # Pin details for SPI interface for screen
        self.tft = TFT(spi, SPI_DC, SPI_RS, SPI_CS)
        self.tft.initg()   # Change this to make screen dimensions match board i.e if you get a line around the edge driver needs rewwwok for this
        self.tft.rgb(True)
        self.tft.set_size(screensize)
        self.set_caption("Tetris")
        self.level = 2
        self.screenSize = screensize
        # Board offsets
        self.x = 4
        self.y = 10
        
        # Pin details for buttons.
        self.right_button = button(19)
        self.left_button = button(14)

    def set_caption(self, caption):
        self.tft.fill(TFT.BLACK)
        v = 30
        v += sysfont["Height"] * 2
        self.tft.text((20, v), caption, TFT.GREEN, sysfont, 2, nowrap=True)
        time.sleep_ms(1000)

    def render_text(self, aText, aColour, vPos, fSize):
        self.tft.text((8, vPos), aText, aColour, sysfont, fSize, nowrap=True)

    # Draw a grid by drawing hollow rectangles starting a smallest coordinate'''
    # Then fill the hollow retangles with colours of fallen frozen blocks '''
    # For the filled rectangle.  aStart is the smallest coordinate corner
    # and aSize is a tuple indicating width, heightalready rededuced for  the ineer block
    def draw_board(self, myTetris):
        gridLineColour = TFT.RED

        # print (' blockOuter: {0} blockInner: {1}  TWidth: {2} THeight: {3}'.format( blockOuter, blockInner, myTetris.width, myTetris.height))
      
        for i in range(0, myTetris.width):
            for j in range(0, myTetris.height):

                # Render grid
                self.tft.rect((self.x + 12 * i  , self.y + 7*j ),
                             (120//10+1, 155//20+1), gridLineColour)
             
    def render_all(self,myTetris):
        for i in range(0, myTetris.width):
            for j in range(0, myTetris.height):
                self.tft.fillrect((self.x + 12 * i +1 , self.y + 7 * j + 1),
                                    (11,5)  , (myTetris.field[j][i]))
                
        #print('\n'.join(' '.join(map(str,sl)) for sl in myTetris.field))
        
    def render_frozen(self,myTetris):
        for i in range(0, myTetris.width):
            for j in range(0, myTetris.height):
                if myTetris.field[j][i] > 0:
                    #(120//10-2, 155//20-2)
                    self.tft.fillrect((self.x + 12 * i , self.y + 7 * j),
                                  (12,7)    , (myTetris.field[j][i]))
       
       
    def hide_figure(self,figure):
        for i in range(4):
            for j in range(4):
                p = i * 4 + j        
                if p in figure.orientation():
                    blockColor = TFT.BLACK
                    fx = figure.x
                    fy = figure.y
                    self.tft.fillrect(
                                (self.x + 12 * (j + fx) + 1, self.y + 7 * (fy + i) + 1),  (120//10-2, 155//20-1),  blockColor)
   # Render the floating blocks 
    def render_figure(self, figure):
        if figure is not None:
           # print("fig or last:" + str(figure.orientation_last()))
            for i in range(4):
                for j in range(4):
                    p = i * 4 + j
                    if p in figure.orientation_last():
                        blockColor = TFT.BLACK
                        fx = figure.x_org
                        fy = figure.y_org

                        self.tft.fillrect(
                            (self.x + 12 * (j + fx) +1 , self.y + 7 * (fy + i) +1 ), (11,5) ,  blockColor)
                for i in range(4):
                    for j in range(4):
                        p = i * 4 + j        
                        if p in figure.orientation():
                            blockColor = figure.color
                            fx = figure.x 
                            fy = figure.y

                            self.tft.fillrect(
                                (self.x + 12 * (j + fx) +1, self.y + 7 * (fy + i)+1), (11,5) ,  blockColor)


    def game_end(self):
        self.tft.fill(TFT.BLACK)
        self.render_text("Game Over", TFT.ORANGE, 50, 2)
        self.render_text("PRESS ANY KEY", TFT.ORANGE, 90, 1)

    def game_score(self, ascore):
        self.render_text("Tetris Score: " + str(ascore), TFT.ORANGE, 1, 1)

    def game_quit(self):
        self.tft.fill(TFT.BLACK)
        self.render_text("Game END", TFT.ORANGE, 50, 2)
        return
Пример #2
0
from ST7735 import TFT
from sysfont import sysfont
from machine import SPI, Pin
import network
import time
import math
spi = SPI(2,
          baudrate=20000000,
          polarity=0,
          phase=0,
          sck=Pin(05),
          mosi=Pin(23),
          miso=Pin(19))
tft = TFT(spi, 17, 9, 16)
tft.rgb(True)
tft.initg()
sta = network.WLAN(network.STA_IF)
sta.active(True)
sta.scan()
sta.isconnected()
sta.connect("SSID", "PASSWD")
sta.ifconfig()


def testlines(color):
    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((0, 0), (x, tft.size()[1] - 1), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((0, 0), (tft.size()[0] - 1, y), color)