Skip to content

Instantly share code, notes, and snippets.

@TheBonnec
Last active August 27, 2022 09:55
Show Gist options
  • Save TheBonnec/44c5aa577e51548e49a46f5fa02d5a30 to your computer and use it in GitHub Desktop.
Save TheBonnec/44c5aa577e51548e49a46f5fa02d5a30 to your computer and use it in GitHub Desktop.
Real engine
from real_engine import *
from random import *
from time import *
from ion import *
# Colors
white = (255,255,255)
dark = (40, 40, 48)
black = (0,0,0)
yellow = (255,170,20)
orange = (220,110,10)
blue = (0,196,247)
blue = (0,192,196)
green = (0,137,86)
lightGreen = (72,192,136)
darkGreen = (47,72,88)
otherGreen = (68,142,121)
def runSession():
gameSession = GameSession((0,0,320,222))
idealTicks = 40
# Background style
grid = GridObject(0, 168,
((1,1,0,0,0,0,1,1,0,0,1,1,0,0,1,1,1,0,0,1),
(1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1)),
16, {1: otherGreen})
# Player
floor = Object(0,200, 320,22, darkGreen)
yobj = Object(35,30, 30,25, yellow)
wobj = Object(30,37, 15,10, white)
oobj = Object(60,40, 15,10, orange)
bobj = Object(50,35, 6,6, black)
group = Group([yobj, wobj, oobj, bobj])
motion = Motion(0,-5)
physics = Physics(0.5,15,0.8)
motion.linkTo(group, gameSession)
physics.linkTo(group, gameSession)
gameSession.addObjects([grid, floor, yobj, wobj, oobj, bobj])
tuyoGroup = Group([])
tuyoMotion = Motion(-4,0)
tuyoMotion.linkTo(tuyoGroup, gameSession)
def gameLost():
nonlocal lostTick, tuyoMotion
lostTick = ticks + idealTicks
tuyoMotion.unlink()
del tuyoMotion
motion.yVelocity = -8
yobj.color = green
def appendTuyo():
space = 70
r = randint(10, 200-10-space)
# pos = ((x, y, width, height, yOffset))
pos = ((320, 0, 40, r, 0), (320, r + space, 40, 200 - r - space, 3))
for p in pos:
mainTuyo = Object(p[0], p[1], p[2], p[3], darkGreen)
greenTuyo = Object(p[0] + 3, p[1] + p[4], p[2] - 6, p[3] - 3, green)
lightGreenTuyo = Object(p[0] + 8, p[1] + p[4], p[2] - 32, p[3] - 3, lightGreen)
mainTuyo.cAttrs["tuyo"] = True
tuyoGroup.objects.append(mainTuyo)
tuyoGroup.objects.append(greenTuyo)
tuyoGroup.objects.append(lightGreenTuyo)
gameSession.addObjects([mainTuyo, greenTuyo, lightGreenTuyo], 2)
appendTuyo()
run = True
delay = monotonic()
ticks = 1
score = 0
lostTick = 0
while run:
if keyDownSingle(KEY_OK) and lostTick == 0:
motion.yVelocity = -8
if ticks % (0.85*idealTicks) == 0:
appendTuyo()
gameSession.refresh(blue, idealTicks, delay)
draw_string(str(int(score)), 10,203, white, darkGreen)
for obj in gameSession.objects:
if "tuyo" in obj.cAttrs and obj.x < -40:
score += 0.5
gameSession.objects.remove(obj)
if "tuyo" in obj.cAttrs and gameSession.hitTest(yobj, obj) and lostTick == 0:
gameLost()
if yobj.y + yobj.height > 200 and lostTick == 0:
gameLost()
elif yobj.y < 0:
group.setY(0)
physics.applyBounce("v")
if lostTick != 0 and ticks >= lostTick:
run = False
delay = monotonic()
ticks += 1
def flappy_bird():
fill_rect(0,0,320,222,dark)
run = True
while run:
fill_rect(65,90, 190,38, green)
draw_string("Press OK to start", 75,100, white, green)
if keyDownSingle(KEY_OK):
runSession()
elif keydown(KEY_ONOFF):
run = False
flappy_bird()
from kandinsky import *
from time import *
from math import *
from ion import *
class CoreObject:
def __init__(self, x, y):
self.x = x
self.y = y
self.cAttrs = {} # Complementary Attributes
def update(self):
pass
class Object(CoreObject):
def __init__(self, x, y, width, height, color):
CoreObject.__init__(self, x, y)
self.width = width
self.height = height
self.color = color
class GridObject(CoreObject):
def __init__(self, x, y, grid, itemSize, colors):
CoreObject.__init__(self, x, y)
self.grid = grid
self.itemSize = itemSize
self.colors = colors
class CoreComplementary:
def __init__(self, className):
self.__className = className
self.parent = None
self.session = None
def linkTo(self, parent, session):
if self.__className not in parent.cAttrs:
parent.cAttrs[self.__className] = self
session.complementaries.append(self)
self.parent = parent
self.session = session
def unlink(self):
del self.parent.cAttrs[self.__className]
self.parent = None
self.session.complementaries.remove(self)
def update(self):
pass
class Motion(CoreComplementary):
def __init__(self, xVelocity, yVelocity):
CoreComplementary.__init__(self, "motion")
self.xVelocity = xVelocity
self.yVelocity = yVelocity
def update(self):
if isinstance(self.parent, CoreObject):
self.parent.x += self.xVelocity
self.parent.y += self.yVelocity
elif isinstance(self.parent, Group):
self.parent.setX(self.parent.getX() + self.xVelocity)
self.parent.setY(self.parent.getY() + self.yVelocity)
def accelerate(self, xv, yv):
self.xVelocity += xv
self.yVelocity += yv
class Physics(CoreComplementary):
def __init__(self, bounce, gravity, frictions):
CoreComplementary.__init__(self, "physics")
self.bounce = bounce
self.gravity = gravity
self.frictions = frictions
def update(self):
if self.frictions != 1:
self.parent.cAttrs["motion"].xVelocity *= sin(self.frictions * pi / 2)
self.parent.cAttrs["motion"].yVelocity *= sin(self.frictions * pi / 2)
self.parent.cAttrs["motion"].yVelocity += self.gravity / 10
def applyBounce(self, side):
if side == "h":
self.parent.cAttrs["motion"].xVelocity *= -self.bounce
elif side == "v":
self.parent.cAttrs["motion"].yVelocity *= -self.bounce
class Animation(CoreComplementary):
def __init__(self, startPoint, endPoint, duration):
CoreComplementary.__init__(self, "animation")
self.startPoint = startPoint
self.endPoint = endPoint
self.duration = duration # In ticks
self.__progression = 0
dx = endPoint[0] - startPoint[0]
dy = endPoint[1] - startPoint[1]
self.__delta = (dx, dy)
def update(self):
progress = (cos((self.__progression / self.duration) * pi + pi) + 1) / 2
if isinstance(self.parent, CoreObject):
self.parent.x = self.startPoint[0] + self.__delta[0] * progress
self.parent.y = self.startPoint[1] + self.__delta[1] * progress
if isinstance(self.parent, Group):
self.parent.setX(self.startPoint[0] + self.__delta[0] * progress)
self.parent.setY(self.startPoint[1] + self.__delta[1] * progress)
self.__progression += 1
if self.__progression > self.duration:
self.unlink()
class Group:
def __init__(self, objects):
self.objects = objects
self.cAttrs = {} # Complementary Attributes
self.__x = 0
self.__y = 0
def getX(self):
min_ = float("inf")
for obj in self.objects:
min_ = min(min_, obj.x)
return min_
def setX(self, value):
_x = self.getX()
for obj in self.objects:
dx = obj.x - _x
obj.x = value + dx
def getY(self):
min_ = float("inf")
for obj in self.objects:
min_ = min(min_, obj.y)
return min_
def setY(self, value):
_y = self.getY()
for obj in self.objects:
dy = obj.y - _y
obj.y = value + dy
class GameSession:
def __init__(self, windowRect):
self.windowRect = windowRect
self.objects = []
self.complementaries = []
#displayLogo()
def addObjects(self, objects, pos=float("inf")):
for obj in objects:
self.objects.insert(pos, obj)
pos += 1
def refresh(self, bgColor=(255,255,255), tick=30, delay=monotonic()): # delay is used to follow tick speed
for com in self.complementaries:
com.update()
'''
for obj in self.objects:
obj.update()
'''
displayList = []
for obj in self.objects:
obj.update()
# Display Object
if isinstance(obj, Object):
#fill_rect(round(obj.x), round(obj.y), obj.width, obj.height, obj.color)
displayList.append([round(obj.x), round(obj.y), obj.width, obj.height, obj.color])
# Display GridObject
elif isinstance(obj, GridObject):
for i in range(len(obj.grid)):
for j in range(len(obj.grid[i])):
if obj.grid[i][j] != 0:
#fill_rect(round(obj.x + j * obj.itemSize), round(obj.y + i * obj.itemSize),
# obj.itemSize, obj.itemSize, obj.colors[obj.grid[i][j]])
displayList.append([round(obj.x + j * obj.itemSize), round(obj.y + i * obj.itemSize),
obj.itemSize, obj.itemSize, obj.colors[obj.grid[i][j]]])
wait_vblank()
# Filling Background
fill_rect(self.windowRect[0], self.windowRect[1], self.windowRect[2], self.windowRect[3], bgColor)
i = 0
for d in displayList:
if i < 30: fill_rect(d[0], d[1], d[2], d[3], d[4])
#i += 1
# Custom function
#function()
# Sleep to follow tick speed
sleep(max((1 / tick) - (monotonic() - delay), 0))
def hitTest(self, obj1, obj2):
center1 = (obj1.x + obj1.width / 2, obj1.y + obj1.height / 2)
center2 = (obj2.x + obj2.width / 2, obj2.y + obj2.height / 2)
if abs(center2[0] - center1[0]) < obj1.width / 2 + obj2.width / 2:
if abs(center2[1] - center1[1]) < obj1.height / 2 + obj2.height / 2:
return True
return False
def reset(self):
self.objects = []
self.complementaries = []
keysDown = {}
def keyDownSingle(key):
global keysDown
if key not in keysDown:
keysDown[key] = False
if keydown(KEY_OK):
if not keysDown[key]:
keysDown[key] = True
return True
return False
else:
keysDown[key] = False
return False
def displayLogo():
colors = {1: (29, 92, 222), 2: (29, 148, 189), 3: (29, 209, 146)}
bg = (40, 40, 48)
re = ((1, 1, 1, 1, 0),
(1, 0, 0, 1, 0),
(1, 1, 2, 2, 3),
(1, 0, 2, 0, 0),
(1, 0, 2, 3, 0),
(0, 0, 3, 0, 0),
(0, 0, 3, 3, 3))
fill_rect(0, 0, 320, 240, bg)
for i in range(len(re)):
for j in range(len(re[i])):
if re[i][j] != 0:
fill_rect(115 + j * 18, 24 + i * 18, 18, 18, colors[re[i][j]])
draw_string("REAL ENGINE 1.0", 85, 166, (255, 255, 255), bg)
draw_string("U Games", 125, 188, (180, 180, 180), bg)
sleep(0)
displayLogo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment