Last active
May 12, 2021 17:30
-
-
Save TheBonnec/c146b98a99120b575d266848d92f8f6f to your computer and use it in GitHub Desktop.
Games Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Version 1.1 | |
from math 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.limitToBorders = 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.limitToBorders: | |
self.checkBorders() | |
self.hasMoved = True | |
self.motion.setSpeeds(x,y) | |
def checkBorders(self): | |
self.x = min(max(self.x , 0), 320 - self.width) | |
self.y = min(max(self.y, 0), 222 - self.height) | |
if self.y == 222 - self.height: | |
self.motion.on = False | |
else: | |
self.motion.on = True | |
def changeFrame(self,w,h): | |
self.width = w | |
self.height = h | |
def changeColor(self,col): | |
self.color = col | |
def setLimitToBorders(self,ltb): | |
self.limitToBorders = ltb | |
def setMotion(self,motion): | |
self.motion = motion | |
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.previousMvt = [0,0] | |
def motion(self): | |
self.ySpeed += self.gravity / 10 | |
self.previousMvt = [self.xSpeed, self.ySpeed] | |
return [self.xSpeed, self.ySpeed] | |
def setSpeeds(self,xSpeed,ySpeed): | |
self.xSpeed = xSpeed | |
self.ySpeed = 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.firstShow = False | |
self.hardReload() | |
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) | |
if b.motion != None and b.motion.on == True and b.hasMoved == False: | |
m = b.motion.motion() | |
b.move(m[0],m[1]) | |
b.hasMoved = False | |
def changeBackground(self,bg): | |
self.background = bg | |
def searchBlockByID(self,i): | |
for j in range(self.blocks): | |
if self.blocks[j].ID == i: | |
return j | |
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 | |
def setOldPos(self): | |
self.OLDPOS = (self.blocks, self.background) | |
# Other functions | |
def rInt(i): | |
return int(round(i,0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from games_engine_beta import * | |
from ion import * | |
from time import * | |
bg = (46,46,40) | |
white = (255,255,255) | |
motion = GEMotion(True,0,-3,0,1,0) | |
mous = GEBlock("mous",160,120,10,10,white) | |
mous.setMotion(motion) | |
mous.setLimitToBorders(True) | |
view = GEView(bg) | |
view.addBlock(mous) | |
while True: | |
dx, dy = 0, 0 | |
if keydown(KEY_LEFT): | |
dx = -3 | |
if keydown(KEY_RIGHT): | |
dx = 3 | |
if keydown(KEY_UP): | |
dy = -3 | |
if keydown(KEY_DOWN): | |
dy = 3 | |
if keydown(KEY_OK): | |
view.blocks[0].motion.ySpeed = -1.5 | |
view.blocks[0].move(dx,dy) | |
view.reload() | |
#sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment