game_template.py

Created by 1epauletteshark

Created on August 12, 2024

1.25 KB

A simple template for a 2d game with sprites


from kandinsky import *
from random import *
from math import *
from time import *
from ion import *

SCREEN_W=320
SCREEN_H=222
FONT_W=10
FONT_H=16
COLORS={}

#Centered text function
def text(text,y=SCREEN_H/2,x=SCREEN_W/2):
  text=str(text)
  draw_string(text,int(x-len(text)*FONT_W/2),int(y-FONT_H/2),"white",color(75,0,160))
  
#Directional navigation
def move(axis,steps,dir):
  dir=radians(dir)
  if axis==x:
    return round(steps*cos(dir))
  else:
    return round(steps*sin(dir))

#Render sprite
def render(x,y,sprite,dir=0):
  x=int(x)
  y=int(y)
  sprites={#"name":"pixels" - https://scratch.mit.edu/projects/921920186/ to get pixels
    }
  draw=sprites[sprite]
  for i in range(0,len(draw[0]),5):
    pixel=draw[0][i:i+5]
    pixel_x=x-draw[1]+int(pixel[:2])
    pixel_y=y-draw[2]+int(pixel[2:4])
    pixel_color=COLORS[pixel[4]]
    rotated_x=round((pixel_x-x)*cos(radians(dir))-(pixel_y-y)*sin(radians(dir))+x)
    rotated_y=round((pixel_x-x)*sin(radians(dir))+(pixel_y-y)*cos(radians(dir))+y)
    set_pixel(rotated_x,rotated_y,pixel_color)

#Detect collisions
def collisions(x1,y1,size1,x2,y2,size2):
    return (
        x1<x2+size2 and
        x1+size1>x2 and
        y1<y2+size2 and
        y1+size1>y2)
        
#Full game loop
while True:

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.