Exemple #1
0
    def draw_circle(self, x, y, r, c, fill=False, gradient=None):

        if not isinstance(c, graphics.Color):
            c = graphics.Color(c[0], c[1], c[2])

        if fill:
            for radius in range(r):
                if gradient is not None:
                    red, green, blue = self.__gradient(gradient, x, r, c)
                    c.red, c.green, c.blue = red, green, blue

                graphics.DrawCircle(self.matrix, x, y, radius, c)
        else:
            graphics.DrawCircle(self.matrix, x, y, r, c)
Exemple #2
0
def render_everything(can):
    can.Clear()
    len = graphics.DrawText(can, font, can.width - 12, 11, white_color,
                            str(timer_number))
    render_pick_ups(can)
    graphics.DrawCircle(can, player_x, player_y, 0, blue_color)
    can = matrix.SwapOnVSync(can)
Exemple #3
0
 def drawDayNight(self, centerX, centerY, isDay):
     dayNightColour = Colours.Black
     squareSize=6
     
     if isDay == 1:
         dayNightColour = Colours.Yellow
     else:
         dayNightColour = Colours.Gray
     
     self.drawDataBoarder(centerX,  centerY,  squareSize,  dispColour=dayNightColour, drawRight=True)
     
     graphics.DrawLine(self.offscreen_canvas, centerX - 1,  centerY - 1,  centerX + 1, centerY + 1, dayNightColour)
     graphics.DrawLine(self.offscreen_canvas, centerX - 1,  centerY + 1,  centerX + 1, centerY - 1, dayNightColour)
     
     for r in range(0, 3):
         graphics.DrawCircle(self.offscreen_canvas, centerX, centerY, r,  dayNightColour)
     
     if isDay == 1:
         # Sun twinkles
         twinkleSize=4
         
         self.__animSunStep += 5
         start = 0 - self.__animSunStep
         end = 359 - self.__animSunStep
         
         if start < -359:
             self.__animSunStep = 0
             start = 0
             end  = 359
         
         for twinkle in range(start,  end, int(360/8)):
             sinMultR = math.sin(math.radians(twinkle))
             cosMultR = math.cos(math.radians(twinkle))
             
             graphics.DrawLine(self.offscreen_canvas, centerX + (twinkleSize * sinMultR),  centerY + (twinkleSize * cosMultR), centerX + (twinkleSize * sinMultR),  centerY + (twinkleSize * cosMultR), Colours.Yellow)
Exemple #4
0
    def loop(self):
        
        previous_head = Point()
        previous_head.x = self.head.x
        previous_head.y = self.head.y
        previous_head_temp = Point()

        ## head logic
        if self.update_time:
            self.follow(self.ball)
        else:
            self.follow(self.fake_ball)

        for t in self.tail:
            previous_head_temp.x = t.x
            previous_head_temp.y = t.y

            t.x = previous_head.x
            t.y = previous_head.y
            previous_head.x = previous_head_temp.x
            previous_head.y = previous_head_temp.y

        # Drawing

        self.canvas.Clear()
        #graphics.DrawLine(canvas, 5, 5, 22, 13, red)
        graphics.DrawCircle(self.canvas, self.head.x, self.head.y, 0, self.red)

        for c, t in enumerate(self.tail):
            color = max(50, 255-30*c)
            self.matrix.SetPixel(t.x,t.y, color, 0, 0)
        
        graphics.DrawCircle(self.canvas, self.ball.x, self.ball.y, 0, self.white)
        #graphics.DrawCircle(self.canvas, self.fake_ball.x, self.fake_ball.y, 0, self.green)

        str_time = "{} {}".format( str(self.hour).zfill(2), str(self.minute).zfill(2))
        graphics.DrawText(self.canvas, self.font, 7, 5, self.blue, str_time)

        #  Change time
        if self.minute != datetime.now().minute or self.hour != datetime.now().hour:
            self.update_time = True
        
        time.sleep(0.1)   # show display for 10 seconds before exit

        self.canvas = self.matrix.SwapOnVSync(self.canvas)
Exemple #5
0
    def loop(self):
        self.image.thumbnail((self.matrix.width, self.matrix.height),
                             Image.ANTIALIAS)
        self.matrix.SetImage(self.image.convert('RGB'))

        graphics.DrawText(self.matrix, self.font, 20, 8, self.white,
                          self.temperature)
        graphics.DrawCircle(self.matrix, 29, 4, 1, self.white)

        time.sleep(0.1)
Exemple #6
0
                def drawHand(angle, length, color, line=True):
                    #Offset by 90.0 degrees so that we get 0 as the top of the clock,
                    #not 3 o'clock, like algebra normally does
                    angle -= 90.0

                    rads = math.radians(angle)
                    center = self.centerPoint()
                    endPoint = center.offsetByVector(rads, length)
                    if line:
                        graphics.DrawLine(offscreen_canvas, center.x, center.y,
                                          endPoint.x, endPoint.y, color)
                    else:
                        graphics.DrawCircle(offscreen_canvas, endPoint.x,
                                            endPoint.y, 1, color)
Exemple #7
0
    def run(self):
        canvas = self.matrix
        font = graphics.Font()
        font.LoadFont("../../../fonts/nanum.bdf")

        red = graphics.Color(255, 0, 0)
        graphics.DrawLine(canvas, 5, 5, 22, 13, red)

        green = graphics.Color(0, 255, 0)
        graphics.DrawCircle(canvas, 15, 15, 10, green)

        blue = graphics.Color(0, 0, 255)
        graphics.DrawText(canvas, font, 2, 10, blue, "Text")

        time.sleep(10)  # show display for 10 seconds before exit
Exemple #8
0
    def draw_ok(self, name: str):
        canvas = self.canvas
        start_headline = self.calc_horizontal_center(len(name), 4, 0, self.display.matrix.width)
        graphics.DrawText(self.canvas, self.font_ok, start_headline, 8, self.font_ok_color, name)
        graphics.DrawCircle(self.canvas, 32, 20, 10, self.circle_ok_color)

        x_pos = 27
        y_pos = 21
        for i in range(0, 4):
            canvas.SetPixel(x_pos + i, y_pos + i, 255, 255, 255)
        
        x_pos = 31
        y_pos = 25
        for i in range(0, 9):
            canvas.SetPixel(x_pos +i, y_pos - i, 255, 255, 255)
Exemple #9
0
def timer(can):
    global timer_number
    global win
    while True:
        can.Clear()
        len = graphics.DrawText(can, font, can.width - 12, 11, white_color,
                                str(timer_number))
        render_pick_ups(can)
        graphics.DrawCircle(can, player_x, player_y, 0, blue_color)
        can = matrix.SwapOnVSync(can)
        time.sleep(1)
        timer_number -= 1
        if (timer_number <= 0):
            break
        if (win == True):
            break
Exemple #10
0
    def draw_error(self, name: str):
        canvas = self.canvas
        graphics.DrawCircle(self.canvas, 12, 16, 10, self.circle_error_color)

        x_pos = 8
        y_pos = 20
        for i in range(0, 9):
            canvas.SetPixel(x_pos + i, y_pos - i, 255, 255, 255)
        
        x_pos = 8
        y_pos = 12
        for i in range(0, 9):
            canvas.SetPixel(x_pos +i, y_pos + i, 255, 255, 255)

        graphics.DrawText(self.canvas, self.font_error, 26, 14, self.font_error_color, 'FAILED ON')
        graphics.DrawText(self.canvas, self.font_info, 26, 24, self.font_info_color, name)
Exemple #11
0
    def show_home_weather(self):
        time.sleep(0.01)
        self.canvas = self.matrix.SwapOnVSync(self.canvas)
        self.canvas.Clear()
        
        self.home_image.thumbnail((self.matrix.width, self.matrix.height), Image.ANTIALIAS)
        self.matrix.SetImage(self.home_image.convert('RGB'), 0, 7)

        home_temperature = r.get('temperature').decode("utf-8").split('.')[0]
        home_humidity = r.get('humidity').decode("utf-8").split('.')[0]

        graphics.DrawText(self.matrix, self.font, 9, 13, self.yellow, home_temperature)
        graphics.DrawCircle(self.matrix, 18, 9, 1, self.yellow)
        
        graphics.DrawText(self.matrix, self.font, 21, 13, self.blue, "{}%".format(home_humidity))
        
        str_time = "{}:{}".format( str(self.hour).zfill(2), str(self.minute).zfill(2))
        graphics.DrawText(self.matrix, self.font, 7, 5, self.blue, str_time)

        time.sleep(5)
Exemple #12
0
 def windDirection(self, centerX, centerY, windPointer,  windSpeed,  handLength,  handColour):
     circleRadius = 5
     arrowHeadSize = 10
     
     windPointer = float(windPointer)
     windPointerR = math.radians(windPointer)
     
     sinMult = math.sin(windPointerR)
     cosMult = math.cos(windPointerR)
     
     graphics.DrawCircle(self.offscreen_canvas, centerX, centerY, circleRadius,  handColour)
     graphics.DrawLine(self.offscreen_canvas, centerX + ((sinMult * circleRadius) * 1.1), centerY - ((cosMult * circleRadius) * 1.1), centerX + (sinMult * handLength),  centerY - (cosMult * handLength), handColour)
     
     # Left
     headAngle = windPointer - arrowHeadSize
     if headAngle > 360:
         headAngle += 360
     
     sinMultL = math.sin(math.radians(headAngle))
     cosMultL = math.cos(math.radians(headAngle))
     
     graphics.DrawLine(self.offscreen_canvas, centerX + (sinMultL * (handLength - 1)),  centerY - (cosMultL * (handLength - 1)), centerX + (sinMult * handLength),  centerY - (cosMult * handLength), handColour)
     
     # Right
     headAngle = windPointer + arrowHeadSize
     if headAngle > 360:
         headAngle -= 360
     
     sinMultR = math.sin(math.radians(headAngle))
     cosMultR = math.cos(math.radians(headAngle))
     
     graphics.DrawLine(self.offscreen_canvas, centerX + (sinMultR * (handLength - 1)),  centerY - (cosMultR * (handLength - 1)), centerX + (sinMult * handLength),  centerY - (cosMult * handLength), handColour)
     
     dispString = "%2.0f" % (float(windSpeed))
     
     if float(windSpeed) < 10:
         charWidth = 2
     else:
         charWidth  = 0
     
     graphics.DrawText(self.offscreen_canvas, self.__font, (centerX - 3) - charWidth, centerY + 3, handColour, dispString)
Exemple #13
0
    def show_weather(self):
        time.sleep(0.01)
        self.canvas = self.matrix.SwapOnVSync(self.canvas)
        self.canvas.Clear()
        
        # Request every 10 mins
        if self.last_weather_request + 600 < time.time():
            
            url = "https://api.darksky.net/forecast/{}/36.65794,-4.5422482?units=si&exclude=hourly,daily".format(config.weather_key)
            
            response = requests.get(url).json()
            self.temperature = str(int(response['currently']['temperature']))
            self.humidity = str(response['currently']['humidity'] * 100).split('.')[0]
            icon = response['currently']['icon']
            try:        
                self.image = Image.open("./weather_icons/{}8.png".format(icon))
            except: 
                print("ERROR Weather icon does not exist: {}".format(icon))
                self.image = Image.open("./weather_icons/unknown8.png")

            self.last_weather_request = time.time()

        self.image.thumbnail((self.matrix.width, self.matrix.height), Image.ANTIALIAS)
        self.matrix.SetImage(self.image.convert('RGB'), 0 ,7 )

        #graphics.DrawText(self.matrix, self.font, 20, 6, self.yellow, self.temperature)
        #graphics.DrawCircle(self.matrix, 29, 2, 1, self.yellow)
        
        #graphics.DrawText(self.matrix, self.font, 20, 14, self.blue, "{}%".format(self.humidity))
        
        graphics.DrawText(self.matrix, self.font, 9, 13, self.yellow, "{}".format(self.temperature))
        graphics.DrawCircle(self.matrix, 18, 9, 1, self.yellow)
        
        graphics.DrawText(self.matrix, self.font, 21, 13, self.blue, "{}%".format(self.humidity))
        
        str_time = "{}:{}".format( str(self.hour).zfill(2), str(self.minute).zfill(2))
        graphics.DrawText(self.matrix, self.font, 7, 5, self.blue, str_time)

        time.sleep(5)
Exemple #14
0
    print " my = ", mag['y'], ": ", lowestMY, " - ", highestMY
    #print " mz = " , ( mag['z'] )
    print('')


skip = 0

while not done:

    if skip > 2:
        skip = 0
        mag = mpu9250.readMagnet()
        x = ((mag['x'] + 45) / 100) * -1
        y = ((mag['y'] - 20) / 100) * -1
        verlet.gravity = Vector2D(x, y)
        # print x, y
    else:
        skip += 1

    verlet.runTimeStep()

    matrix.Clear()

    for particle in verlet.particles:
        graphics.DrawCircle(matrix, int(particle.vector.x),
                            int(particle.vector.y), particle.radius,
                            particle.data['color'])

    # graphics.DrawLine(matrix, 5, 5, 22, 13, red)

    # time.sleep(10)   # show display for 10 seconds before exit
Exemple #15
0
def check_radius():
    while True:
        for circle in list_circles:
            if (circle.radius == 9):
                list_circles.remove(circle)


try:
    print('Press CTRL-C to stop.')
    canvas = matrix.CreateFrameCanvas()
    thread1 = Thread(target=add_circle)
    thread2 = Thread(target=check_radius)
    thread1.daemon = True
    thread2.daemon = True
    thread1.start()
    thread2.start()
    while True:
        canvas.Clear()
        for circle in list_circles:
            index = random.randint(0, 6)
            current_color = circle.color
            graphics.DrawCircle(canvas, circle.x, circle.y, circle.radius,
                                current_color)
            circle.radius = circle.radius + 1
        canvas = matrix.SwapOnVSync(canvas)
        time.sleep(0.125)

except KeyboardInterrupt:
    sys.exit(0)
Exemple #16
0
 def draw_circle(self, x: int, y: int, radius: int, color: int):
     color = graphics.Color(*hex2rgb(color))
     graphics.DrawCircle(self.canvas, x, y, radius, color)
Exemple #17
0
    def run(self):
        self.logger.info("Starting display thread")
        options = RGBMatrixOptions()
        options.rows = 32
        options.cols = 64
        options.chain_length = 1
        options.parallel = 1
        options.hardware_mapping = 'regular'  # If you have an Adafruit HAT: 'adafruit-hat'                                                                       
        matrix = RGBMatrix(options=options)

        offscreen_canvas = matrix.CreateFrameCanvas()

        font = graphics.Font()
        font.LoadFont("rpi-rgb-led-matrix/fonts/tom-thumb.bdf")
        font2 = graphics.Font()
        font2.LoadFont("rpi-rgb-led-matrix/fonts/6x12.bdf")

        font3 = graphics.Font()
        font3.LoadFont("rpi-rgb-led-matrix/fonts/4x6.bdf")

        font4 = graphics.Font()
        font4.LoadFont("rpi-rgb-led-matrix/fonts/6x9.bdf")

        home_image = Image.open("blue_house1.bmp").convert('RGB')

        black = graphics.Color(0, 0, 0)
        red = graphics.Color(128, 0, 0)
        purple = graphics.Color(28, 65, 84)
        lightBlue = graphics.Color(0, 0, 128)
        dark_blue = graphics.Color(0, 0, 32)
        green = graphics.Color(0, 128, 0)
        orange = graphics.Color(128, 75, 0)

        while True:
            try:
                offscreen_canvas.Clear()
                offscreen_canvas.brightness = \
                    self.collection.brightness * min(self.collection.ambient_light * 2 + 10, 100)
                if self.collection.screen == SCREEN_TRAINS:
                    graphics.DrawLine(offscreen_canvas, 0, 1, 63, 1, dark_blue)

                    for index, dep in enumerate(self.collection.departure_list, start=0):
                        if dep.delay < 1:
                            dep_color = green
                        elif dep.delay < 8:
                            dep_color = orange
                        else:
                            dep_color = red

                        y0 = index * 6 + 2
                        y1 = y0 + 5
                        text_length = graphics.DrawText(offscreen_canvas, font, dep.display.pos, y1, dep_color,
                                                        dep.text1())
                        dep.display.scroll(text_length)
                        for y in range(y0, y1):
                            graphics.DrawLine(offscreen_canvas, 45, y, 63, y, black)
                        graphics.DrawLine(offscreen_canvas, 45 - 1, y0, 45 - 1, y1, dark_blue)
                        graphics.DrawLine(offscreen_canvas, 0, y1, 63, y1, dark_blue)
                        graphics.DrawText(offscreen_canvas, font, 45, y1, dep_color, dep.text2())
                    offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas)
                    time.sleep(0.03)
                elif self.collection.screen == SCREEN_INDOOR:
                    graphics.DrawLine(offscreen_canvas, 0, 5, 63, 5, dark_blue)
                    graphics.DrawText(offscreen_canvas, font, 2, 5, red, f'{self.collection.datetime}')
                    indoor = self.collection.indoor_environment_data
                    temp_text_length = graphics.DrawText(offscreen_canvas, font2, 0, 14, green,
                                                         f'{indoor.temperature:.2f} C')
                    graphics.DrawText(offscreen_canvas, font2, 0, 23, green, f'{indoor.humidity:.2f} %')
                    graphics.DrawCircle(offscreen_canvas, temp_text_length - 8, 8, 1, green)
                    graphics.DrawText(offscreen_canvas, font2, 0, 32, green, f'{indoor.pressure:.1f} hPa')
                    offscreen_canvas.SetImage(home_image, 45, 7)
                    offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas)
                    time.sleep(0.1)

                elif self.collection.screen == SCREEN_OUTDOOR:
                    outdoor = self.collection.current_weather_data
                    graphics.DrawLine(offscreen_canvas, 0, 5, 63, 5, dark_blue)
                    header_text_length = graphics.DrawText(offscreen_canvas, font, outdoor.header_text.pos, 5, red,
                                                           outdoor.header_text.text)
                    outdoor.header_text.scroll(header_text_length)
                    temp_text_length = graphics.DrawText(offscreen_canvas, font2, 0, 14, green,
                                                         f'{outdoor.temperature} C')
                    graphics.DrawCircle(offscreen_canvas, temp_text_length - 8, 8, 1, green)
                    graphics.DrawText(offscreen_canvas, font2, 0, 23, green, f'{outdoor.humidity} %')
                    offscreen_canvas.SetImage(outdoor.weather_icon, 45, 7)

                    #                detail1_length = graphics.DrawText(offscreen_canvas, font, outdoor.detail_text1.pos, 25, green, outdoor.detail_text1.text)
                    #                outdoor.detail_text1.scroll(detail1_length)
                    detail2_length = graphics.DrawText(offscreen_canvas, font2, outdoor.detail_text2.pos, 31, green,
                                                       outdoor.detail_text2.text)
                    outdoor.detail_text2.scroll(detail2_length)
                    offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas)
                    time.sleep(0.03)

                #                elif (self.collection.screen in [SCREEN_FORECAST_1, SCREEN_FORECAST_2, SCREEN_FORECAST_3]):
                elif (
                        self.collection.screen == SCREEN_FORECAST_1 or self.collection.screen == SCREEN_FORECAST_2 or self.collection.screen == SCREEN_FORECAST_3):
                    offset = 0
                    if self.collection.screen == SCREEN_FORECAST_2:
                        offset = 4
                    elif self.collection.screen == SCREEN_FORECAST_3:
                        offset = 8
                    graphics.DrawLine(offscreen_canvas, 0, 5, 63, 5, dark_blue)
                    graphics.DrawText(offscreen_canvas, font, 0, 5, purple,
                                      f'{self.collection.current_weather_data.city}  {self.collection.forecast_list[offset].weekday}')

                    graphics.DrawText(offscreen_canvas, font, 0, 11, green,
                                      f'{self.collection.forecast_list[offset].timestr} {self.collection.forecast_list[offset].temp}C')
                    #                graphics.DrawText(offscreen_canvas, font, 0, 17, green, f'{self.collection.forecast_list[offset].weather_desc}')
                    detail_length1 = graphics.DrawText(offscreen_canvas, font,
                                                       self.collection.forecast_list[offset].detail_text.pos, 17, green,
                                                       f'{self.collection.forecast_list[offset].detail_text.text}')
                    self.collection.forecast_list[offset].detail_text.scroll(detail_length1)
                    graphics.DrawLine(offscreen_canvas, 0, 18, 63, 18, dark_blue)
                    graphics.DrawText(offscreen_canvas, font, 0, 25, green,
                                      f'{self.collection.forecast_list[offset + 2].timestr} {self.collection.forecast_list[offset + 2].temp}C')
                    #                graphics.DrawText(offscreen_canvas, font, 0, 31, green, f'{self.collection.forecast_list[offset+2].weather_desc}')
                    detail_length2 = graphics.DrawText(offscreen_canvas, font,
                                                       self.collection.forecast_list[offset + 2].detail_text.pos, 31,
                                                       green,
                                                       f'{self.collection.forecast_list[offset + 2].detail_text.text}')
                    self.collection.forecast_list[offset + 2].detail_text.scroll(detail_length2)
                    offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas)
                    time.sleep(0.03)
                elif self.collection.screen == SCREEN_NEWS:
#                    graphics.DrawLine(offscreen_canvas, 0, 1, 63, 1, dark_blue)

                    for index, dep in enumerate(self.collection.news_list, start=0):
#                        if dep.delay < 1:
                        dep_color = green

                        y0 = index * 6 + 2
                        y1 = y0 + 5
                        text_length = graphics.DrawText(offscreen_canvas, font, dep.text.pos, y1, dep_color,
                                                        dep.text.text)
                        dep.text.scroll(text_length)
                    offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas)
                    time.sleep(0.03)
            except Exception as e:
                self.logger.error('Failed to show display: %s', str(e))
                time.sleep(3)
Exemple #18
0
def render_pick_ups(canvas):
    for pick_up in pick_ups:
        graphics.DrawCircle(canvas, pick_up.x, pick_up.y, pick_up.radius,
                            pick_up.color)
    canvas = matrix.SwapOnVSync(canvas)
Exemple #19
0
        win = True


# Function that quits keyboard input and then exits the whole program
def quit_program():
    curses.endwin()
    sys.exit(1)


# Main loop that starts the timer daemon thread and waits for user keyboard input
player_x = 1
player_y = 1
try:
    create_pick_ups()
    render_everything(canvas)
    graphics.DrawCircle(canvas, player_x, player_y, 0, blue_color)
    canvas = matrix.SwapOnVSync(canvas)
    timer_thread = Thread(target=timer, args=(canvas, ))
    timer_thread.daemon = True
    timer_thread.start()
    key = ''
    while timer_thread.is_alive() == True:
        key = stdscr.getch()
        stdscr.addch(20, 25, key)
        stdscr.refresh()
        check_boundry = 0
        canvas.Clear()
        if key == ord('w'):
            check_boundry = player_y
            check_boundry -= 1
            if (check_boundry >= 0):
def main(win):

    # 32 Rows, 2 panel, 1 chain
    MyMatrix = RGBMatrix(32, 2, 1)

    # Bits used for PWM. Something between 1..11. Default: 11
    MyMatrix.pwmBits = 11

    # Sets brightness level. Default: 100. Range: 1..100"
    MyMatrix.brightness = 100

    # Buffer canvas.
    MyOffsetCanvas = MyMatrix.CreateFrameCanvas()

    # setup colours for border, paddles and ball
    red = graphics.Color(255, 0, 0)
    green = graphics.Color(0, 255, 0)
    blue = graphics.Color(0, 0, 255)
    white = graphics.Color(255, 255, 255)
    black = graphics.Color(0, 0, 0)
    grey = graphics.Color(128, 128, 128)

    # set player1 paddle start
    p1x = 2
    p1y = 12

    # set player2 paddle start
    p2x = 61
    p2y = 12

    # set the ball start
    ballx = 32
    bally = 16

    # Set the ball movement
    xballmovement = +1
    yballmovement = -1

    # start the main loop
    going = True

    win.nodelay(True)  # make getkey() not wait

    while going:

        try:
            key = win.getkey()
        except:  # in no delay mode getkey raise and exeption if no key is press
            key = None
        #endtry

        if key == "x":
            going = False
            break
        #endif

        # player1 paddle down
        if key == "q":
            p1y = p1y - 1
        #endif

        # player1 paddle down
        if key == "a":
            p1y = p1y + 1
        #endif

        # player2 paddle up
        if key == "p":
            p2y = p2y - 1
        #endif

        # player2 paddle down
        if key == "l":
            p2y = p2y + 1
        #endif

        ballx = ballx + xballmovement
        bally = bally + yballmovement

        p1y = bally - 4
        p2y = bally - 4

        MyMatrix.Clear()

        # draw the border
        graphics.DrawLine(MyOffsetCanvas, 0, 0, 63, 0, white)
        graphics.DrawLine(MyOffsetCanvas, 0, 0, 0, 31, white)

        graphics.DrawLine(MyOffsetCanvas, 63, 0, 63, 31, white)
        graphics.DrawLine(MyOffsetCanvas, 0, 31, 63, 31, white)

        # draw player1 paddle
        graphics.DrawLine(MyOffsetCanvas, p1x, p1y, p1x, p1y + 8, red)

        # draw player2 paddle
        graphics.DrawLine(MyOffsetCanvas, p2x, p2y, p2x, p2y + 8, green)

        # draw the ball
        graphics.DrawCircle(MyOffsetCanvas, ballx, bally, 1, blue)

        # Collision Detect
        collision_type = fnDetectCollision(p1x, p1y, p2x, p2y, ballx, bally)

        if collision_type == PADDLE1:
            xballmovement = +1
        #endif

        if collision_type == PADDLE2:
            xballmovement = -1
        #endif

        if collision_type == TOP:
            yballmovement = +1
        #endif

        if collision_type == BOTTOM:
            yballmovement = -1
        #endif

        if collision_type == LEFT_EDGE:
            xballmovement = +1
        #endif

        if collision_type == RIGHT_EDGE:
            xballmovement = -1
        #endif

        # flip on vsync
        MyOffsetCanvas = MyMatrix.SwapOnVSync(MyOffsetCanvas)

        # Wait a bit
        time.sleep(0.005)

    #endwhile

    return
Exemple #21
0
    def pong_loop(self):

        ## Stick logic
        if self.ball_x < self.canvas.width/2:
            if self.left_needs_to_lose:
                pass
            elif self.ball_y < self.stick_left:
                self.stick_left -= 1
            else:
                self.stick_left += 1
        else:
            if self.right_needs_to_lose:
                pass
            elif self.ball_y < self.stick_right:
                self.stick_right -= 1
            else:
                self.stick_right += 1

        ## Ball logic
        if self.ball_y < 1 or self.ball_y > self.canvas.height-1:
            self.is_up = not self.is_up
        if self.ball_x < 2:
            if self.left_needs_to_lose:
                self.minute =  datetime.now().minute
                self.ball_x = self.canvas.width/2
                self.ball_y = self.canvas.height/2
                self.left_needs_to_lose = False
            else:
                self.is_left = not self.is_left
        if self.ball_x > self.canvas.width-3:
            if self.right_needs_to_lose:
                self.hour = datetime.now().hour
                self.ball_x = self.canvas.width/2
                self.ball_y = self.canvas.height/2
                self.right_needs_to_lose = False
            else:
                self.is_left = not self.is_left

        if self.is_left:
            self.ball_x = self.ball_x - 1
        else:
            self.ball_x = self.ball_x + 1

        if self.is_up:
            self.ball_y = self.ball_y -1
        else:
            self.ball_y = self.ball_y +1

        # Drawing


        #graphics.DrawLine(canvas, 5, 5, 22, 13, red)
        graphics.DrawLine(self.canvas, self.canvas.width/2, 0, self.canvas.width/2, self.canvas.height, self.white)
        
        graphics.DrawLine(self.canvas, 0, self.stick_left -2 , 0, self.stick_left +2, self.green)
        graphics.DrawLine(self.canvas, self.canvas.width-1, self.stick_right - 2, self.canvas.width-1, self.stick_right + 2, self.red)

        graphics.DrawCircle(self.canvas, self.ball_x, self.ball_y, 0, self.white)

        str_time = "{} {}".format( str(self.hour).zfill(2), str(self.minute).zfill(2))
        graphics.DrawText(self.canvas, self.font, 7, 5, self.blue, str_time)

        #  Change time
        if self.minute != datetime.now().minute:
            self.left_needs_to_lose = True
        if self.hour != datetime.now().hour:
            self.right_needs_to_lose = True
     
        time.sleep(0.1)   # show display for 10 seconds before exit
Exemple #22
0
 def draw(self, canvas):
     graphics.DrawCircle(canvas, self.x, self.y, self.radius, white)