Skip to content

Commit

Permalink
feat(other): ✨ Added a snake.py and a basicwrapdemo
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsunami014 (Max) authored and Tsunami014 (Max) committed Nov 27, 2024
1 parent 600497c commit c9a9f66
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 3 deletions.
3 changes: 1 addition & 2 deletions BlazeSudio/utils/wrap/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from BlazeSudio.collisions import rotate
import BlazeSudio.utils.wrap.constraints as constraints
from BlazeSudio.utils.wrap import constraints, snake
import os
import pygame
import math
Expand Down
104 changes: 104 additions & 0 deletions BlazeSudio/utils/wrap/snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import math
import BlazeSudio.collisions as colls

__all__ = [
'Snake'
]

class Snake:
def __init__(self, width):
self.joints = [[0, 0], [width, 0]]
self.jointDists = [width]

@property
def segments(self):
return [(self.joints[i], self.joints[i+1]) for i in range(len(self.joints)-1)]

@property
def width(self):
return sum(
math.sqrt((self.joints[i][0]-self.joints[i+1][0])**2+(self.joints[i][1]-self.joints[i+1][1])**2) for i in range(len(self.joints)-1)
)

@width.setter
def width(self, newWidth):
d = 0
for i in range(len(self.joints)-1):
oldD = d
x, y = self.joints[i+1][0]-self.joints[i][0], self.joints[i+1][1]-self.joints[i][1]
d += math.sqrt(x**2+y**2)
if d > newWidth:
ang = math.degrees(math.atan2(y, x))-90
newdist = newWidth-d-oldD
self.joints = [*self.joints[:i], colls.rotate(self.joints[i], (self.joints[i][0], self.joints[i][1]+newdist), ang)]
self.jointDists = [*self.jointDists[:i], newdist]
break
else:
x, y = self.joints[i+1][0]-self.joints[i][0], self.joints[i+1][1]-self.joints[i][1]
ang = math.degrees(math.atan2(y, x))-90
newdist = newWidth-d
self.joints.append(colls.rotate(self.joints[-1], (self.joints[-1][0], self.joints[-1][1]+newdist), ang))
self.jointDists.append(newdist)

def insert_straight(self, x):
self.straighten()
if self.joints[0][-1] < x < self.joints[0][0]:
for idx in range(len(self.joints)-1):
if self.joints[idx+1][0] < x:
self.joints.insert(idx+1, (x, self.joints[idx][1]))
self.recalculate_dists()
return True
return False

def recalculate_dists(self):
prevj = None
self.jointDists = []
for j in self.joints:
if prevj is None:
prevj = j
continue
self.jointDists.append(math.sqrt((prevj[0]-j[0])**2+(prevj[1]-j[1])**2))
prevj = j

def update(self):
for i in range(len(self.joints)-1):
x, y = self.joints[i+1][0]-self.joints[i][0], self.joints[i+1][1]-self.joints[i][1]
ang = math.degrees(math.atan2(y, x))-90
self.joints[i+1] = colls.rotate(self.joints[i], (self.joints[i][0], self.joints[i][1]+self.jointDists[i]), ang)

def straighten(self):
for i in range(len(self.joints)-1):
self.joints[i+1] = colls.rotate(self.joints[i], (self.joints[i][0], self.joints[i][1]+self.jointDists[i]), 90)

def __iter__(self):
return ((self.joints[i], self.joints[i+1]) for i in range(len(self.joints)-1))

def __len__(self):
return len(self.joints)-1

def copy(self):
s = Snake(self.width, self.joints.copy())
s.offsets = self.offsets
return s

"""class Snake:
def __init__(self, width, joints=[]):
self.width = width
self.offsets = [[0, 0], [0, 0], [0, 0]]
self.joints = joints
@property
def segments(self):
l = []
l2 = [0, *self.joints, self.width]
for i in range(len(l2)-1):
l.append((l2[i], l2[i+1]))
return l
def __iter__(self):
return iter(self.segments)
def copy(self):
s = Snake(self.width, self.joints.copy())
s.offsets = self.offsets
return s"""
75 changes: 74 additions & 1 deletion demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,78 @@ def CollisionsDemo(debug=False):
os.environ['debug'] = 'True'
from demoFiles import collisionsDemo # noqa: F401

def WrapBasicDemo():
from BlazeSudio.utils.wrap import snake
import pygame
pygame.init()

win = pygame.display.set_mode()

main = snake.Snake(100)

r = True
heldSegment = None
selectedSegment = None
while r:
movingMode = pygame.key.get_mods() & pygame.KMOD_ALT

selectedSegment = (None, None)
if not movingMode:
mp = pygame.mouse.get_pos()
for idx in range(len(main.joints)):
i = main.joints[idx]
if (i[0]-mp[0])**2+(i[1]-mp[1])**2 <= 5**2:
selectedSegment = (idx, i)
break

for event in pygame.event.get():
if event.type == pygame.QUIT:
r = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
r = False
elif event.key == pygame.K_SPACE:
if (not movingMode) and (selectedSegment[0] is None):
main.insert_straight(pygame.mouse.get_pos()[0])
elif event.type == pygame.MOUSEBUTTONDOWN:
if not movingMode:
heldSegment = selectedSegment[0]

if heldSegment is not None and (not pygame.mouse.get_pressed()[0]):
heldSegment = None

win.fill((10, 10, 10))

y = win.get_height()/2
x = (win.get_width()+main.width)/2

if movingMode:
heldSegment = None
main.joints[0] = pygame.mouse.get_pos()
main.update()
else:
if heldSegment is not None:
newx = pygame.mouse.get_pos()[0]
if heldSegment > 0:
newx = min(newx, main.joints[heldSegment-1][0])
if heldSegment < len(main.joints)-1:
newx = max(newx, main.joints[heldSegment+1][0])
main.joints[heldSegment] = (newx, main.joints[heldSegment][1])
main.recalculate_dists()
else:
main.joints[0] = (x, y)
main.straighten()

for i in main:
pygame.draw.line(win, (255, 255, 255), i[0], i[1], 10)
for j in main.joints:
if j == selectedSegment[1]:
pygame.draw.circle(win, (255, 100, 100), j, 5)
else:
pygame.draw.circle(win, (10, 50, 255), j, 5)

pygame.display.update()

def WrapDemo():
from BlazeSudio.graphics import Graphic, GO, GUI
from BlazeSudio.utils.wrap import wrapSurface
Expand Down Expand Up @@ -393,7 +465,8 @@ def cmd(cmdd):


label('Misc stuff:')
button('Wrap Demo [game]', WrapDemo, )
button('Wrap Demo [game]', WrapDemo, )
button('Wrap Basic Demo [game]', WrapBasicDemo, )

if has_tk:
root.after(1, lambda: root.attributes('-topmost', True))
Expand Down

0 comments on commit c9a9f66

Please sign in to comment.