Skip to content

Instantly share code, notes, and snippets.

@TheBonnec
Last active August 3, 2022 12:53
Show Gist options
  • Save TheBonnec/dc8d4ea7971a64fc722ff24b0285881e to your computer and use it in GitHub Desktop.
Save TheBonnec/dc8d4ea7971a64fc722ff24b0285881e to your computer and use it in GitHub Desktop.
Dynausor
from games_engine_beta import *
from kandinsky import *
from random import *
from time import *
from ion import *
from os import *
global state, pts, theme, bg1, bg2, green, darkGreen, black
state = "menu"
pts = 0
theme = "clear"
''' ----- Menus ----- '''
def menu():
fill_rect(0,0,320,50,bg1)
fill_rect(0,50,320,172,bg2)
draw_string("SQUARE DINOSAUR",85,55,green,bg2)
fill_rect(57,5,12,30,green)
fill_rect(40,26,8,24,darkGreen)
fill_rect(150,26,8,24,darkGreen)
fill_rect(162,32,8,18,darkGreen)
fill_rect(272,23,8,27,darkGreen)
draw_string("U Games v1.0",195,203,darkGreen,bg2)
global index
index = 0
while True:
s = selectionView([["Play",80,100],
["Scores", 80, 120],
["Switch theme",80,140],
["Exit",80,160]],
220)
if s == 0: return "play"
elif s == 1: return "scores"
elif s == 2: return setColors(True)
elif s == 3: return "exit"
def selectionView(menus, okX):
# menus = [[titre, x, y]]
global index
if keydown(KEY_UP):
index = max(index-1, 0)
sec(KEY_UP)
elif keydown(KEY_DOWN):
index = min(index+1, len(menus)-1)
sec(KEY_DOWN)
elif keydown(KEY_OK):
sec(KEY_OK)
return index
def getColor(i):
return green if index == i else black
for m in range(len(menus)):
n = menus[m]
draw_string(n[0], n[1], n[2], getColor(m), bg2)
if index == m:
draw_string("OK", okX, n[2], green, bg2)
else:
draw_string(" ", okX, n[2], black, bg2)
''' ----- Views ----- '''
def scoresView():
fill_rect(0,0,320,222,bg1)
draw_string("Press home button to get back",15,15,black,bg1)
scores = readScores()
for s in range(len(scores)):
draw_string(scores[s][0], 50, 50+15*s, darkGreen, bg1)
draw_string(scores[s][1] + " pts", 190, 50+15*s, darkGreen, bg1)
while True:
if keydown(KEY_HOME):
sec(KEY_HOME)
return "menu"
def game():
view = GEView(bg1)
dino = GEBlock("dino",50,192,14,30,green)
dino.motion = GEMotion(True,0,0,0,0.9,1)
dino.bordersLimited = True
view.addBlock(dino)
view.reload()
global pts
pts = 0
tour = 1
mono = monotonic()
end = False
while True:
if tour % 70*(1+tour/15000) == 0:
if randint(0,4) == 1:
s = -1.5-(tour/3000)
h = uniform(min(20-s,30), 37)
cactus = GEBlock(str(tour),320,222-h,10,h,darkGreen)
cactus.motion = GEMotion(True,s,0,0,0,1)
view.addBlock(cactus)
else:
tour = max(tour-10, 1)
if (keydown(KEY_OK) or keydown(KEY_ONOFF)) and view.getBlock("dino").motion.ySpeed == 0:
view.getBlock("dino").motion.ySpeed = -3
draw_string(str(pts)+" pts",10,10,black,bg1)
view.reload()
tour += 1
for b in view.blocks:
if b.x < -15:
view.blocks.remove(b)
pts += 1
elif b.ID != "dino" and b.x < 62 and b.x > 40 and view.getBlock("dino").y > b.y-30:
end = True
if end == True:
break
while monotonic() - mono < 0.008:
abc = 1
mono = monotonic()
return "endMessage"
def endMessage():
global index
fill_rect(15,15,290,140,bg2)
draw_string("You have reached " + str(pts) + " pts", 20, 20, darkGreen, bg2)
index = 0
while True:
s = selectionView([["Restart",20,95],
["Save Score",20,115],
["Main menu",20,135]],
280)
if s == 0: return "play"
elif s == 2: return "menu"
elif s == 1:
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSAVE YOUR SCORE\n")
name = input("Name : ")
saveScore(name)
sec(KEY_EXE)
return "menu"
''' ----- Other ----- '''
def setColors(toggle=False):
global theme, bg1, bg2, green, darkGreen, black
theme = "clear" if toggle and theme == "dark" else "dark"
if theme == "clear":
bg1, bg2, green, darkGreen, black = (242,242,247), (209,209,214), (52,169,89), (72,124,115), (0,0,0)
else:
bg1, bg2, green, darkGreen, black = (28,28,30), (58,58,60), (48,159,81), (72,124,115), (255,255,255)
return "menu"
def readScores(order=True):
def second(e): return int(e[1])
try:
file = open("eisen.sav","r")
s = file.readlines()
file.close()
for i in range(len(s)):
s[i] = s[i].replace("\x01","")
s[i] = s[i].replace("\x00","")
if order:
s[i] = s[i].replace("\n","")
s[i] = s[i].split(":")
if order == False:
return "".join(s)
s.sort(key=second,reverse=True)
return s
print(">scores red !")
except:
file.close()
print(">failed to read the scores...")
def saveScore(name):
global pts
if name == "": name = "unamed"
try :
oldScores = readScores(False)
file = open("eisen.sav","w")
file.truncate(0)
file.write(oldScores + name + ":" + str(pts) + "\n")
file.close()
print(">score saved !")
except:
file.close()
print(">failed to save the score...")
def sec(key):
while keydown(key):
abc = 1
setColors()
while True:
if state == "menu":
state = menu()
elif state == "scores":
state = scoresView()
elif state == "play":
state = game()
elif state == "endMessage":
state = endMessage()
else:
break
# Version 1.1
# Beta 4
from math import *
from time import *
from kandinsky import *
class GEBlock:
def __init__(self, ID, x,y,w,h, col):
self.ID = ID
self.x = x
self.y = y
self.width = w
self.height = h
self.color = col
self.bordersLimited = False
self.motion = None
self.oldPos = (0,0,0,0)
self.hasMoved = False
def move(self,x,y):
self.x += x
self.y += y
if self.bordersLimited:
self.checkBorders()
self.hasMoved = True
def checkBorders(self):
self.x = min(max(self.x , 0), 320 - self.width)
self.y = min(max(self.y, 0), 222 - self.height)
def performMotion(self):
if self.motion != None and self.motion.on == True and self.hasMoved == False:
if self.motion.bounce != 0:
if self.x >= 320-self.width and self.motion.xSpeed > 0 or self.x <= 0 and self.motion.xSpeed < 0:
self.motion.bounceMotion("h")
if self.y >= 222-self.height and self.motion.ySpeed > 0 or self.y <= 0 and self.motion.ySpeed < 0:
self.motion.bounceMotion("v")
self.motion.motion()
if self.bordersLimited and self.y >= 222 - self.height and self.motion.ySpeed > 0 and self.motion.gravity != 0:
self.motion.ySpeed = 0
m = self.motion.getSpeeds()
self.move(m[0],m[1])
def changeFrame(self,w,h):
self.width = w
self.height = h
def updateOldPos(self):
s = self
self.oldPos = (s.x,s.y,s.width,s.height)
class GEMotion:
def __init__(self,on,xSpeed,ySpeed,bounce,gravity,frictions):
self.on = on
self.xSpeed = xSpeed
self.ySpeed = ySpeed
self.bounce = bounce
self.gravity = gravity
self.frictions = frictions
self.acceleration = 0
self.previousMvt = [0,0]
def motion(self):
if self.frictions != 1:
self.xSpeed *= sin(self.frictions * pi/2)
self.ySpeed *= sin(self.frictions * pi/2)
self.ySpeed += self.gravity / 10
def bounceMotion(self,side):
if side == "h":
self.xSpeed *= -self.bounce
elif side == "v":
self.ySpeed *= -self.bounce
def setSpeeds(self,xSpeed,ySpeed):
self.xSpeed = xSpeed
self.ySpeed = ySpeed
def getSpeeds(self):
return [self.xSpeed, self.ySpeed]
class GEView:
def __init__(self, bg):
self.blocks = []
self.background = bg
self.firstShow = True
def addBlock(self, block, pos = 0):
self.blocks.insert(pos,block)
def addBlocks(self, blocks, pos = 0):
for b in range(blocks):
self.blocks.insert(pos, blocks[-b])
def getBlock(self, ID):
for b in self.blocks:
if b.ID == ID:
return b
return False
def reload(self):
if self.firstShow:
self.hardReload()
self.firstShow = False
else:
self.smallReload()
def hardReload(self):
fill_rect(0,0,320,222,self.background)
for b in self.blocks:
fill_rect(rInt(b.x), rInt(b.y), rInt(b.width), rInt(b.height), b.color)
def smallReload(self):
for b in self.blocks:
fill_rect(rInt(b.x), rInt(b.y), rInt(b.width), rInt(b.height), b.color)
for z in self.zte(b):
b.updateOldPos()
fill_rect(rInt(z[0]), rInt(z[1]), rInt(z[2]), rInt(z[3]), self.background)
b.performMotion()
b.hasMoved = False
def zte(self, b):
o = b.oldPos
dx, dy = rInt(b.x) - rInt(o[0]), rInt(b.y) - rInt(o[1])
if dx >= b.width or dy >= b.height:
return [(o[0], o[1], o[2], o[3])]
r = []
if dx != 0:
r.append((o[0]+o[2]/2-(dx/abs(dx) * (o[2]/2)), o[1], dx, o[3]))
if dy != 0:
r.append((o[0], o[1]+o[3]/2-(dy/abs(dy) * (o[3]/2)), o[2], dy))
return r
# Other functions
def rInt(i):
return int(round(i,0))
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(2)
displayLogo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment