import sdl2 import sdl2.ext from sdl2.ext.image import load_image import numpy as np WHITE = sdl2.ext.Color(255, 255, 255) class Player(sdl2.ext.Entity): def __init__(self, world, renderer, radius, path_to_image, posx=0, posy=0): image = load_image(str(path_to_image)) new_surf = sdl2.surface.SDL_CreateRGBSurface(0,radius*2,radius*2,32,0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000) sdl2.surface.SDL_BlitScaled(image, sdl2.rect.SDL_Rect(0,0,image.h,image.w), new_surf, sdl2.rect.SDL_Rect(0,0,radius*2,radius*2)) self.sprite = renderer.factory.from_surface(new_surf, False) self.sprite.position = int(posx), int(posy) def update_pos(self, pos): self.sprite.position = int(pos[0]), int(pos[1]) class SoftwareRenderer(sdl2.ext.SoftwareSpriteRenderSystem): def __init__(self, window): super(SoftwareRenderer, self).__init__(window) def render(self, components): sdl2.ext.fill(self.surface, sdl2.ext.Color(0, 0, 0)) super(SoftwareRenderer, self).render(components) class SdlRender(): def __init__(self): sdl2.ext.init() self.window = sdl2.ext.Window("Gravity Sim", size=(800, 800)) self.window.show() self.sdl_world = sdl2.ext.World() self.spriterenderer = SoftwareRenderer(self.window) self.sdl_world.add_system(self.spriterenderer) self.factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)