I wanna make a small game, but I need some help...
I'm pretty newbie both in python and in kivy. I'm using python 3.4 and kivy 1.8.0.
The game will have some drawn elements which will be draggable and/or disappering:
-if you click on a point you could drag it
- if you click anywhere one point will disappear
I've tried to make the disappearing part of it, but I got stuck.
I made a dummy code with some points on the canvas, where you could see how I wanted to approach the problem:
--> draw some points
--> remove / reposition one of point
--> clear canvas
--> redraw
Somehow I cannot redraw it. But I managed to clear the canvas...
Could you help me?
Also I would like to get help/ideas how to make it draggable...
Thank you, here's my code:
from kivy.app import App
from kivy.graphics import Ellipse
from kivy.uix.boxlayout import BoxLayout
import random
class CustomLayout(BoxLayout):
def __init__(self, **kwargs):
super(CustomLayout, self).__init__(**kwargs)
self.a = []
self.points_pos()
with self.canvas.after:
self.draw_points()
def points_pos(self):
i=0
x = random.sample(range(800), 10)
y = random.sample(range(640), 10)
for i in range(10):
pos = [0,0]
pos[0]=x[i]
pos[1]=y[i]
self.a.append(pos)
print(self.a)
def draw_points(self):
i = 0
for i in range(len(self.a)):
self.circle = Ellipse(
size = (25,25),
pos = (self.a[i][0],self.a[i][1])
)
def random_remove(self):
point = self.a[random.randint(0,len(self.a)-1)]
self.a.remove(point)
def update(self):
self.parent.canvas.clear()
with self.canvas:
self.draw_points()
def on_touch_down(self, touch):
self.random_remove()
self.update()
class MainApp(App):
def build(self):
root = CustomLayout()
return root
if __name__ == '__main__':
MainApp().run()
def update(self):
self.parent.canvas.clear()
with self.canvas:
self.draw_points()
You clear the parent's canvas, which includes the canvas of your BoxLayout. After that it doesn't matter how many things you draw on the BoxLayout, they won't ever be displayed.
To fix this you probably want to simply do self.canvas.clear() instead, but actually this is very inefficient and isn't a good way to work with kivy's canvases (though it will work fine for small numbers of instructions). It is much better to keep references to all your canvas instructions and only remove the specific one(s) you no longer want, with self.canvas.remove(...).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With