fixed collision system
This commit is contained in:
parent
72d4f28cea
commit
fdd3775da6
8
HEMD.py
8
HEMD.py
|
@ -1,4 +1,5 @@
|
||||||
import threading
|
import threading
|
||||||
|
from decimal import localcontext, Decimal, ROUND_HALF_UP
|
||||||
|
|
||||||
class color3:
|
class color3:
|
||||||
def __init__(self, r=0, g=0, b=0):
|
def __init__(self, r=0, g=0, b=0):
|
||||||
|
@ -62,8 +63,13 @@ class vector2:
|
||||||
def __copy__(self):
|
def __copy__(self):
|
||||||
return vector2(self.x, self.y)
|
return vector2(self.x, self.y)
|
||||||
|
|
||||||
|
def __tround__(self, target):
|
||||||
|
with localcontext() as ctx:
|
||||||
|
ctx.rounding = ROUND_HALF_UP
|
||||||
|
return Decimal(target).to_integral_value()
|
||||||
|
|
||||||
def __round__(self):
|
def __round__(self):
|
||||||
return vector2(round(self.x), round(self.y))
|
return vector2(self.__tround__(self.x), self.__tround__(self.y))
|
||||||
|
|
||||||
def __add__(self, v):
|
def __add__(self, v):
|
||||||
return vector2(self.x+v.x, self.y+v.y)
|
return vector2(self.x+v.x, self.y+v.y)
|
||||||
|
|
7
audiosys.py
Normal file
7
audiosys.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import simpleaudio
|
||||||
|
class sound:
|
||||||
|
def __init__(self):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import random
|
||||||
import string
|
import string
|
||||||
import math
|
import math
|
||||||
import time
|
import time
|
||||||
|
import threading
|
||||||
#import itertools
|
#import itertools
|
||||||
#import multiprocessing
|
#import multiprocessing
|
||||||
|
|
||||||
|
@ -32,12 +33,12 @@ class game:
|
||||||
self._width = int(canvas.winfo_reqwidth()/pixelsize)
|
self._width = int(canvas.winfo_reqwidth()/pixelsize)
|
||||||
self._pixelsize = pixelsize
|
self._pixelsize = pixelsize
|
||||||
self._OAP = {}
|
self._OAP = {}
|
||||||
self._PAO = {}
|
|
||||||
self._objects = {}
|
self._objects = {}
|
||||||
self._visobjects = {}
|
self._visobjects = {}
|
||||||
self._camera = obj()
|
self._camera = obj()
|
||||||
self._camera.position = vector2()
|
self._camera.position = vector2()
|
||||||
self._camera.zoom = pixelsize
|
self._camera.zoom = pixelsize
|
||||||
|
self._OAPUPDATING = False
|
||||||
"""self._workers = []
|
"""self._workers = []
|
||||||
self._processes = 2 # total number of processes.
|
self._processes = 2 # total number of processes.
|
||||||
for i in range(self._processes):
|
for i in range(self._processes):
|
||||||
|
@ -85,8 +86,9 @@ class game:
|
||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def addobj(self, target: obj, physics=True):
|
def addobj(self, target: obj, physics=True, id=None):
|
||||||
temp = self.genid()
|
if id == None: temp = self.genid()
|
||||||
|
else: temp = id
|
||||||
if physics: self._objects[temp] = target
|
if physics: self._objects[temp] = target
|
||||||
self._visobjects[temp] = target
|
self._visobjects[temp] = target
|
||||||
target.id = temp
|
target.id = temp
|
||||||
|
@ -117,8 +119,8 @@ class game:
|
||||||
if obj2.anchored == True:
|
if obj2.anchored == True:
|
||||||
obj1.velocity = vector2()
|
obj1.velocity = vector2()
|
||||||
else:
|
else:
|
||||||
if xblock: obj1.velocity.x = obj1.velocity.x/2;obj2.velocity.x = obj2.velocity.x/2
|
if xblock: obj1.velocity.x = obj1.velocity.x/2;obj2.velocity.x = obj2.velocity.x/2;obj2.position.x += obj2.velocity.x
|
||||||
if yblock: obj1.velocity.y = obj1.velocity.y/2;obj2.velocity.y = obj2.velocity.y/2
|
if yblock: obj1.velocity.y = obj1.velocity.y/2;obj2.velocity.y = obj2.velocity.y/2;obj2.position.y += obj2.velocity.y
|
||||||
return (xblock, yblock)
|
return (xblock, yblock)
|
||||||
|
|
||||||
def calcphysobj(self, target: obj):
|
def calcphysobj(self, target: obj):
|
||||||
|
@ -135,13 +137,23 @@ class game:
|
||||||
temp = self.handlecollision(target, i)
|
temp = self.handlecollision(target, i)
|
||||||
if temp[0] == False: target.position.x += oldvel.x
|
if temp[0] == False: target.position.x += oldvel.x
|
||||||
if temp[1] == False: target.position.y += oldvel.y
|
if temp[1] == False: target.position.y += oldvel.y
|
||||||
if len(colliding) == 0: target.position += target.velocity
|
if len(colliding) <= 1: target.position += target.velocity
|
||||||
else:
|
else:
|
||||||
target.position += target.velocity
|
target.position += target.velocity
|
||||||
target.velocity += target.acceleration
|
target.velocity += target.acceleration
|
||||||
self.updateobjinoap(target)
|
self.updateobjinoap(target)
|
||||||
|
|
||||||
|
def updallobjpositions(self):
|
||||||
|
self._OAPUPDATING = True
|
||||||
|
for i in self._visobjects.values():
|
||||||
|
try:
|
||||||
|
self.updateobjinoap(i)
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
self._OAPUPDATING = False
|
||||||
|
|
||||||
def render(self):
|
def render(self):
|
||||||
|
if self._OAPUPDATING == False: threading.Thread(target=self.updallobjpositions).start()
|
||||||
self._canvas.delete(tk.ALL)
|
self._canvas.delete(tk.ALL)
|
||||||
self._height = int(canvas.winfo_reqheight()/self._camera.zoom)
|
self._height = int(canvas.winfo_reqheight()/self._camera.zoom)
|
||||||
self._width = int(canvas.winfo_reqwidth()/self._camera.zoom)
|
self._width = int(canvas.winfo_reqwidth()/self._camera.zoom)
|
||||||
|
@ -154,25 +166,25 @@ class game:
|
||||||
self._canvas.create_rectangle(x*self._pixelsize, y*self._pixelsize, x*self._pixelsize+self._pixelsize, y*self._pixelsize+self._pixelsize, fill=obj.color.__tohex__(), width=0)
|
self._canvas.create_rectangle(x*self._pixelsize, y*self._pixelsize, x*self._pixelsize+self._pixelsize, y*self._pixelsize+self._pixelsize, fill=obj.color.__tohex__(), width=0)
|
||||||
|
|
||||||
def updobjs(self):
|
def updobjs(self):
|
||||||
|
start = time.time()
|
||||||
for i in self._objects.values():
|
for i in self._objects.values():
|
||||||
self.calcphysobj(i)
|
self.calcphysobj(i)
|
||||||
|
return time.time()-start
|
||||||
|
|
||||||
if not "__CMD__" in globals():
|
if not "__CMD__" in globals():
|
||||||
root = tk.Tk()
|
root = tk.Tk()
|
||||||
canvas = tk.Canvas(root)
|
canvas = tk.Canvas(root)
|
||||||
canvas.grid()
|
canvas.grid()
|
||||||
temp = game(canvas)
|
temp = game(canvas)
|
||||||
for i in range(200):
|
for i in range(10):
|
||||||
tempobj = obj()
|
tempobj = obj()
|
||||||
tempobj.position = vector2(5, i)
|
tempobj.position = vector2(5, i)
|
||||||
tempobj.anchored = False
|
tempobj.anchored = False
|
||||||
|
tempobj.velocity.x = 1
|
||||||
tempobj.collide = True
|
tempobj.collide = True
|
||||||
temp.addobj(tempobj)
|
temp.addobj(tempobj)
|
||||||
while True:
|
while True:
|
||||||
temp.updobjs()
|
took = temp.updobjs()
|
||||||
for i in temp._objects.values():
|
|
||||||
i.color += color3(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
|
||||||
#i.position = vector2(random.randint(0, temp._width), random.randint(0, temp._height))
|
|
||||||
temp.render()
|
temp.render()
|
||||||
root.update()
|
root.update()
|
||||||
time.sleep(0.1)
|
time.sleep(0.1-took)
|
Loading…
Reference in a new issue