Here's pygame you can argue about
print("Python has begun running . . .")
#Imports
import time
import math
import pygame
import threading
import ctypes
from sys import exit
#pygame startup
pygame.init()
info = pygame.display.Info()
print(info)
screen = pygame.display.set_mode((info.current_w, info.current_h), pygame.RESIZABLE | pygame.DOUBLEBUF | pygame.HWSURFACE)
pygame.display.set_caption("Survivion")
pygame.display.set_icon(pygame.image.load("fg/assets/player.svg"))
clock = pygame.time.Clock()
#mouse setup
pygame.mouse.set_visible(False)
cursorImg = pygame.image.load("fg/assets/cursor.png")
#Classes and Functions
class Tmo:
def __init__(self):
self.timers = {}
self.timer_id = 0
def setTimeout(self, fn, delay, *args, **kwargs):
def timer_callback():
self.timers.pop(timer_id, None)
fn(*args, **kwargs)
timer_id = self.timer_id
self.timer_id += 1
t = threading.Timer(delay / 1000, timer_callback)
self.timers[timer_id] = t
t.start()
return timer_id
def clearTimeout(self, timer_id):
t = self.timers.pop(timer_id, None)
if t is not None:
t.cancel()
class Ints:
def __init__(self):
self.timers = {}
self.timer_id = 0
def setInterval(self, fn, time, *args):
def interval_callback():
fn(*args)
self.timers[timer_id] = threading.Timer(time, interval_callback)
self.timers[timer_id].start()
timer_id = self.timer_id
self.timer_id += 1
self.timers[timer_id] = threading.Timer(time, interval_callback)
self.timers[timer_id].start()
def clearInterval(self, fn):
for timer_id in self.timers:
if self.timers[timer_id].function == fn:
self.timers[timer_id].cancel()
del self.timers[timer_id]
break
tmo = Tmo()
ints = Ints()
class Stats:
def __init__(self):
self.clock = pygame.time.Clock()
self.font = pygame.font.Font("fg/fonts/LilitaOne.ttf", 20)
self.fps = 0
self.ms = self.font.render(f"{ms}ms", True, (255, 255, 255))
self.visible = True
def render(self, display):
self.fps = self.font.render(f"{round(self.clock.get_fps())}fps", True, (255, 255, 255))
if self.visible:
screen.blit(self.fps, (30, 15))
screen.blit(self.ms, (30, 45))
class Player(object):
def __init__(self):
self.width = 34
self.height = 30
self.x = (info.current_w/2) - self.width/2
self.y = (info.current_h/2) - self.height/2
self.keys = [False, False, False, False]
self.sprite = pygame.image.load('fg/assets/player.svg').convert_alpha()
self.sprite = pygame.transform.scale(self.sprite, (self.width, self.height))
self.movementSpeed = 1
self.rotation = 0
def update(self):
if (self.keys[0] and not self.keys[1]):
self.y -= self.movementSpeed
if (self.keys[1] and not self.keys[0]):
self.y += self.movementSpeed
if (self.keys[2] and not self.keys[3]):
self.x -= self.movementSpeed
if (self.keys[3] and not self.keys[2]):
self.x += self.movementSpeed
#Rotation towards mouse
angle = math.atan2(mouse["y"] - 16 - self.y + (self.height/2), mouse["x"] - self.x - 16 + (self.width/2))
self.rotation = angle;
degrees = math.degrees(self.rotation)
self.spriteR = pygame.transform.rotate(self.sprite, -degrees)
#THIS STEP IS VERY IMPORTANT. IT ANCHORS TO THE CENTER OF THE SPRITE, AND NOT THE TOP LEFT
sprite_rect = self.spriteR.get_rect(center=(self.x + self.width / 2, self.y + self.height / 2))
screen.blit(self.spriteR, sprite_rect)
def perform(self):
self.update()
class Background(object):
def __init__(self):
self.screen_width = info.current_w
self.screen_height = info.current_h
self.sprite = pygame.image.load('fg/assets/background.jpg').convert_alpha()
self.backgrounds = []
bg_width, bg_height = self.sprite.get_size()
for x in range(0, self.screen_width, bg_width):
for y in range(0, self.screen_height, bg_height):
self.backgrounds.append((x, y))
def perform(self):
for x, y in self.backgrounds:
screen.blit(self.sprite, (x, y))
def cursor(x, y):
screen.blit(cursorImg, (x, y))
#Variables
mouse = {
"x": 0,
"y": 0,
"holding": False
}
ms = 0
ds, de = 0, 0
stats = Stats()
#Sprites
background = Background()
player = Player()
#Loop
while True:
ds = pygame.time.get_ticks()
mouse["x"], mouse["y"] = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.keys[0] = True
if event.key == pygame.K_s:
player.keys[1] = True
if event.key == pygame.K_a:
player.keys[2] = True
if event.key == pygame.K_d:
player.keys[3] = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
player.keys[0] = False
if event.key == pygame.K_s:
player.keys[1] = False
if event.key == pygame.K_a:
player.keys[2] = False
if event.key == pygame.K_d:
player.keys[3] = False
background.perform()
player.perform()
cursor(mouse["x"], mouse["y"])
de = pygame.time.get_ticks()
ms = abs(round(ds - de))
stats.render(screen)
pygame.display.update()
stats.clock.tick(65)
def calcMS():
self.ms = self.font.render(f"{ms}ms", True, (255, 255, 255))
ints.setInterval(calcMS, 0.5)