Last active
January 26, 2024 02:29
-
-
Save tigercoding56/fbe87d5cee028e5ca7f0b2140b00f08d to your computer and use it in GitHub Desktop.
pyglet rectangle with rounded corners
This file contains hidden or 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
import pyglet | |
if __name__=='__main__': | |
window=pyglet.window.Window() | |
class rcrect: | |
def __init__(self,x,y,h,w,r,color=[255,255,255,255]): | |
self.pos=[x,y] | |
self.x=x | |
self.y=y | |
self.h=h | |
self.w=w | |
self.r=r | |
self.color=color | |
self.batch=pyglet.graphics.Batch() | |
self.rc1=[[x-r,y-h],[w+(2*r),h]] | |
self.rc2=[[x,y-r-h],[w,h+(2*r)]] | |
self.cp=[[x,y],[x+w,y],[x+w,y-h],[x,y-h]] | |
self.r1 = pyglet.shapes.Rectangle(self.rc1[0][0],self.rc1[0][1],self.rc1[1][0],self.rc1[1][1],batch=self.batch,color=color) | |
self.r2 = pyglet.shapes.Rectangle(self.rc2[0][0],self.rc2[0][1],self.rc2[1][0],self.rc2[1][1],batch=self.batch,color=color) | |
self.cls=[] | |
for i in self.cp: | |
self.cls.append(pyglet.shapes.Circle(i[0],i[1],r,10,batch=self.batch,color=color)) | |
def draw(self): | |
self.batch.draw() | |
def collide(self,point): | |
if point[0]>self.x and point[0]<(self.x+self.w): | |
if point[1]>(self.y-self.h) and point[1]<(self.y): | |
return 1 | |
return 0 | |
def upd(self,x=None,y=None,h=None,w=None,r=None,color=None): | |
if x==None: | |
x=self.x | |
else: | |
self.x=x | |
if y==None: | |
y=self.y | |
else: | |
self.y=y | |
if h==None: | |
h=self.h | |
else: | |
self.h=h | |
if w==None: | |
w=self.w | |
else: | |
self.w=w | |
if r==None: | |
r=self.r | |
else: | |
self.r=r | |
if color==None: | |
color=self.color | |
else: | |
self.color=color | |
self.pos=[x,y] | |
self.rc1=[[x-r,y-h],[w+(2*r),h]] | |
self.rc2=[[x,y-r-h],[w,h+(2*r)]] | |
self.cp=[[x,y],[x+w,y],[x+w,y-h],[x,y-h]] | |
self.r1.position = self.rc1[0] | |
self.r1.width=self.rc1[1][0] | |
self.r1.height=self.rc1[1][1] | |
self.r1.color=color | |
self.r2.position = self.rc2[0] | |
self.r2.width=self.rc2[1][0] | |
self.r2.height=self.rc2[1][1] | |
self.r2.color=color | |
self.cls=[] | |
for i in self.cp: | |
self.cls.append(pyglet.shapes.Circle(i[0],i[1],r,10,batch=self.batch,color=color)) | |
if __name__=='__main__': | |
example=rcrect(20,220,200,200,10) | |
@window.event() | |
def on_mouse_motion(x, y, dx, dy): | |
print(example.collide([x,y])) | |
example.upd(color=[x%255,y%255,255,255]) | |
@window.event() | |
def on_draw(): | |
example.draw() | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment