Last active
November 16, 2021 00:02
-
-
Save jjfiv/83f35a1da87381cefcf1b44e8dddf908 to your computer and use it in GitHub Desktop.
Working on a Pygame Wrapper for CS145
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
import pygame | |
import random | |
__version__ = "2021.11.15.0" | |
class SimplePygame: | |
def __init__(self, screen): | |
self.screen = screen | |
self.has_quit = False | |
self.fonts = {} | |
self.keyboard = {} | |
self.mouse_pressed = False | |
self.mouse_position = (0, 0) | |
def quit(self): | |
self.has_quit = True | |
def get_size(self): | |
return (self.screen.get_width(), self.screen.get_height()) | |
def get_font(self, name: str, size: int): | |
key = (name, size) | |
if key not in self.fonts: | |
self.fonts[key] = pygame.font.SysFont(name, size) | |
return self.fonts[key] | |
def draw_text(self, font, text, color, x, y): | |
rendered = font.render(text, True, color) | |
self.screen.blit(rendered, (x, y)) | |
def fill_rect(self, x, y, w, h, color): | |
rect = pygame.Rect(x, y, w, h) | |
pygame.draw.rect(self.screen, color, rect) | |
def outline_rect(self, x, y, w, h, color, width: int = 1): | |
rect = pygame.Rect(x, y, w, h) | |
pygame.draw.rect(self.screen, color, rect, width=width) | |
def fill_ellipse(self, x, y, w, h, color): | |
rect = pygame.Rect(x, y, w, h) | |
pygame.draw.ellipse(self.screen, color, rect) | |
def outline_ellipse(self, x, y, w, h, color, width: int = 1): | |
rect = pygame.Rect(x, y, w, h) | |
pygame.draw.ellipse(self.screen, color, rect, width=width) | |
def draw_line(self, x1, y1, x2, y2, color, width=1): | |
pygame.draw.line(self.screen, color, (x1, y1), (x2, y2), width=width) | |
def random_hue(saturation, lightness): | |
c = pygame.Color(0, 0, 0) | |
c.hsla = (random.randint(0, 360), saturation, lightness, 100) | |
return c | |
def random_pastel(): | |
return random_hue(50, 50) | |
def run_animation( | |
draw, | |
width=600, | |
height=600, | |
fps=60, | |
bgcolor=(0, 0, 0), | |
): | |
# initialize pygame | |
pygame.init() | |
pygame.font.init() | |
screen = pygame.display.set_mode((width, height)) | |
time = 0.0 | |
delta_t = 1 / fps | |
# setup the font and clock | |
clock = pygame.time.Clock() | |
system = SimplePygame(screen) | |
while not system.has_quit: | |
# get the event corresponding to user input | |
event = pygame.event.poll() | |
if event.type == pygame.QUIT: | |
break # we're done! | |
system.mouse_position = pygame.mouse.get_pos() | |
system.mouse_pressed = pygame.mouse.get_pressed()[0] | |
if event.type == pygame.KEYDOWN: | |
letter = pygame.key.name(event.key) | |
system.keyboard[letter] = True | |
elif event.type == pygame.KEYUP: | |
letter = pygame.key.name(event.key) | |
if letter in system.keyboard: | |
del system.keyboard[letter] | |
screen.fill(bgcolor) # first fill the scene with white | |
draw(system) | |
# update the screen | |
pygame.display.update() | |
clock.tick(fps) # control the frame rate | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment