diff --git a/examples/pong.py b/examples/pong.py index 9d56240..32ab882 100755 --- a/examples/pong.py +++ b/examples/pong.py @@ -92,18 +92,18 @@ def intersects(self, rect): dist_x = abs(self.x - rx) dist_y = abs(self.y - ry) - if dist_x > rw/2.0+self.radius or dist_y > rh/2.0+self.radius: + if dist_x > rw / 2.0 + self.radius or dist_y > rh / 2.0 + self.radius: return False - if dist_x <= rw/2.0 or dist_y <= rh/2.0: + if dist_x <= rw / 2.0 or dist_y <= rh / 2.0: return True - cx = dist_x-rw/2.0 - cy = dist_y-rh/2.0 + cx = dist_x - rw / 2.0 + cy = dist_y - rh / 2.0 - c_sq = cx**2.0 + cy**2.0 + c_sq = cx ** 2.0 + cy ** 2.0 - return c_sq <= self.radius**2.0 + return c_sq <= self.radius ** 2.0 def update(self, delta, left_player, right_player): global width @@ -119,11 +119,11 @@ def update(self, delta, left_player, right_player): w, h = item.size relative_y = (cy - self.y) / (h / 2) - speed = self.speed + (abs(relative_y)/4) + speed = self.speed + (abs(relative_y) / 4) angle = relative_y * 5 * (math.pi / 12) - if self.x > width/2: + if self.x > width / 2: self.x = item.position.x - self.radius self.velocity = Vec2D( speed * -math.cos(angle), @@ -193,8 +193,8 @@ def center(self): @property def position(self): return Position( - x=self.x - (self.width/2), - y=self.y - (self.height/2)) + x=self.x - (self.width / 2), + y=self.y - (self.height / 2)) @property def size(self): @@ -218,8 +218,8 @@ def render(self, draw): time_last = millis() -player_one_pos = height/2 -player_two_pos = height/2 +player_one_pos = height / 2 +player_two_pos = height / 2 displayhatmini.set_led(0, 0, 0) diff --git a/examples/pygame-basic.py b/examples/pygame-basic.py new file mode 100755 index 0000000..f9d5156 --- /dev/null +++ b/examples/pygame-basic.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import os +import sys +import signal +import pygame +import time +import math + +from displayhatmini import DisplayHATMini + +print("""Display HAT Mini: Basic Pygame Demo""") + + +def _exit(sig, frame): + global running + running = False + print("\nExiting!...\n") + + +def update_display(): + display_hat.st7789.set_window() + # Grab the pygame screen as a bytes object + pixelbytes = pygame.transform.rotate(screen, 180).convert(16, 0).get_buffer() + # Lazy (slow) byteswap: + pixelbytes = bytearray(pixelbytes) + pixelbytes[0::2], pixelbytes[1::2] = pixelbytes[1::2], pixelbytes[0::2] + # Bypass the ST7789 PIL image RGB888->RGB565 conversion + for i in range(0, len(pixelbytes), 4096): + display_hat.st7789.data(pixelbytes[i:i + 4096]) + + +display_hat = DisplayHATMini(None) + +os.putenv('SDL_VIDEODRIVER', 'dummy') +pygame.display.init() # Need to init for .convert() to work +screen = pygame.Surface((display_hat.WIDTH, display_hat.HEIGHT)) + +signal.signal(signal.SIGINT, _exit) + +running = True + +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + break + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + running = False + break + + # Clear the screen + screen.fill((0, 0, 0)) + + box_w = display_hat.WIDTH // 3 + box_h = display_hat.HEIGHT // 2 + + pygame.draw.rect(screen, (255, 0, 0), (0, 0, box_w, box_h)) + pygame.draw.rect(screen, (0, 255, 0), (box_w, 0, box_w, box_h)) + pygame.draw.rect(screen, (0, 0, 255), (box_w * 2, 0, box_w, box_h)) + + pygame.draw.rect(screen, (255, 255, 0), (0, box_h, box_w, box_h)) + pygame.draw.rect(screen, (255, 0, 255), (box_w, box_h, box_w, box_h)) + pygame.draw.rect(screen, (0, 255, 255), (box_w * 2, box_h, box_w, box_h)) + + r = 50 + x = math.sin(time.time() * 2) * (display_hat.WIDTH - r) / 2 + y = math.cos(time.time()) * (display_hat.HEIGHT - r) / 2 + x += display_hat.WIDTH // 2 + y += display_hat.HEIGHT // 2 + pygame.draw.circle(screen, (0, 0, 0), (x, y), r) + + update_display() + + +pygame.quit() +sys.exit(0) diff --git a/examples/pygame-demo.py b/examples/pygame-demo.py new file mode 100755 index 0000000..608e540 --- /dev/null +++ b/examples/pygame-demo.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +import os +import sys +import signal +import pygame +import time +import colorsys +import math + +from displayhatmini import DisplayHATMini + + +print("""Display HAT Mini: Pygame Demo""") + + +hue_to_rgb = [] + + +for i in range(0, 255): + hue_to_rgb.append(colorsys.hsv_to_rgb(i / 255.0, 1, 1)) + + +# zoom tunnel +def tunnel(x, y, step): + u_width = 32 + u_height = 32 + speed = step / 100.0 + x -= (u_width / 2) + y -= (u_height / 2) + xo = math.sin(step / 27.0) * 2 + yo = math.cos(step / 18.0) * 2 + x += xo + y += yo + if y == 0: + if x < 0: + angle = -(math.pi / 2) + else: + angle = (math.pi / 2) + else: + angle = math.atan(x / y) + if y > 0: + angle += math.pi + angle /= 2 * math.pi # convert angle to 0...1 range + hyp = math.sqrt(math.pow(x, 2) + math.pow(y, 2)) + shade = hyp / 2.1 + shade = 1 if shade > 1 else shade + angle += speed + depth = speed + (hyp / 10) + col1 = hue_to_rgb[step % 255] + col1 = (col1[0] * 0.8, col1[1] * 0.8, col1[2] * 0.8) + col2 = hue_to_rgb[step % 255] + col2 = (col2[0] * 0.3, col2[1] * 0.3, col2[2] * 0.3) + col = col1 if int(abs(angle * 6.0)) % 2 == 0 else col2 + td = .3 if int(abs(depth * 3.0)) % 2 == 0 else 0 + col = (col[0] + td, col[1] + td, col[2] + td) + col = (col[0] * shade, col[1] * shade, col[2] * shade) + return (col[0] * 255, col[1] * 255, col[2] * 255) + + +class PygameDHM: + screen = None + + def __init__(self): + self.dhm = DisplayHATMini(None) + self._init_display() + + self.screen.fill((0, 0, 0)) + self._updatefb() + + self._running = False + + def _exit(self, sig, frame): + self._running = False + print("\nExiting!...\n") + + def _init_display(self): + os.putenv('SDL_VIDEODRIVER', 'dummy') + pygame.display.init() # Need to init for .convert() to work + self.screen = pygame.Surface((320, 240)) + + def __del__(self): + "Destructor to make sure pygame shuts down, etc." + + def _updatefb(self): + self.dhm.st7789.set_window() + # Grab the pygame screen as a bytes object + pixelbytes = pygame.transform.rotate(self.screen, 180).convert(16, 0).get_buffer() + # Lazy (slow) byteswap: + pixelbytes = bytearray(pixelbytes) + pixelbytes[0::2], pixelbytes[1::2] = pixelbytes[1::2], pixelbytes[0::2] + # Bypass the ST7789 PIL image RGB888->RGB565 conversion + for i in range(0, len(pixelbytes), 4096): + self.dhm.st7789.data(pixelbytes[i:i + 4096]) + + def run(self): + self._running = True + signal.signal(signal.SIGINT, self._exit) + while self._running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self._running = False + break + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + self._running = False + break + + # Clear the screen + self.screen.fill((0, 0, 0)) + + # Draw the demo effect + t = int(time.time() * 40) + for x in range(21): + for y in range(15): + r, g, b = tunnel(x, y, t) + r = min(255, int(r)) + g = min(255, int(g)) + b = min(255, int(b)) + pygame.draw.circle(self.screen, (r, g, b), ((x * 15) + 7, (y * 15) + 6 + 7), 7) + + self._updatefb() + + pygame.quit() + sys.exit(0) + + +display = PygameDHM() +display.run()