i have no idea why getting error...
i have class hungerbar displays rectangle on canvas. want show @ start of game. put in update function call once program starts. here's piece of code.
class hungerbar(rectangle): def hunger_dec(self): if (self.size[0] > 0): self.size = vector(-1, 0) + self.size def hunger_inc(self, increase): if (self.size[0] + increase < 100): self.size = vector(increase, 0) + self.size class shootinggame(widget): hungerbar = hungerbar() def drawbar(self): self.hungerbar.size = vector(1000, 20) self.add_widget(self.hungerbar) def update(self, dt): self.drawbar() class gamescreen(screen): def __init__(self, **kwargs): super(gamescreen, self).__init__(**kwargs) self.game = shootinggame() self.add_widget(self.game) clock.schedule_interval(self.game.update, 1.0 / 60.0) last, gamescreen gets added app.
and how hungerbar defined in .kv file
<hungerbar>: color: rgb: (0, 1, 1) canvas: rectangle: pos: self.width * 7, 30 size: self.size when run code, error get:
traceback (most recent call last): file "main.py", line 323, in <module> shootingapp().run() file "/applications/kivy.app/contents/resources/kivy/kivy/app.py", line 824, in run runtouchapp() file "/applications/kivy.app/contents/resources/kivy/kivy/base.py", line 487, in runtouchapp eventloop.window.mainloop() file "/applications/kivy.app/contents/resources/kivy/kivy/core/window/window_sdl2.py", line 539, in mainloop self._mainloop() file "/applications/kivy.app/contents/resources/kivy/kivy/core/window/window_sdl2.py", line 300, in _mainloop eventloop.idle() file "/applications/kivy.app/contents/resources/kivy/kivy/base.py", line 330, in idle self.dispatch_input() file "/applications/kivy.app/contents/resources/kivy/kivy/base.py", line 315, in dispatch_input post_dispatch_input(*pop(0)) file "/applications/kivy.app/contents/resources/kivy/kivy/base.py", line 221, in post_dispatch_input listener.dispatch('on_motion', etype, me) file "kivy/_event.pyx", line 699, in kivy._event.eventdispatcher.dispatch (kivy/_event.c:7011) file "/applications/kivy.app/contents/resources/kivy/kivy/core/window/__init__.py", line 904, in on_motion self.dispatch('on_touch_down', me) file "kivy/_event.pyx", line 699, in kivy._event.eventdispatcher.dispatch (kivy/_event.c:7011) file "/applications/kivy.app/contents/resources/kivy/kivy/core/window/__init__.py", line 920, in on_touch_down if w.dispatch('on_touch_down', touch): file "kivy/_event.pyx", line 699, in kivy._event.eventdispatcher.dispatch (kivy/_event.c:7011) file "/applications/kivy.app/contents/resources/kivy/kivy/uix/screenmanager.py", line 1069, in on_touch_down return super(screenmanager, self).on_touch_down(touch) file "/applications/kivy.app/contents/resources/kivy/kivy/uix/widget.py", line 382, in on_touch_down if child.dispatch('on_touch_down', touch): file "kivy/_event.pyx", line 699, in kivy._event.eventdispatcher.dispatch (kivy/_event.c:7011) file "/applications/kivy.app/contents/resources/kivy/kivy/uix/relativelayout.py", line 276, in on_touch_down ret = super(relativelayout, self).on_touch_down(touch) file "/applications/kivy.app/contents/resources/kivy/kivy/uix/widget.py", line 382, in on_touch_down if child.dispatch('on_touch_down', touch): file "kivy/_event.pyx", line 699, in kivy._event.eventdispatcher.dispatch (kivy/_event.c:7011) file "main.py", line 94, in on_touch_down self.drawbar() file "main.py", line 173, in drawbar self.add_widget(self.hungerbar) file "/applications/kivy.app/contents/resources/kivy/kivy/uix/widget.py", line 442, in add_widget 'add_widget() can used instances' kivy.uix.widget.widgetexception: add_widget() can used instances of widget class. thank in advance helping me! :)
here full code. i'm sorry it's long , messy.
import kivy __version__ = "1.9.0" kivy.app import app kivy.uix.widget import widget kivy.properties import numericproperty, referencelistproperty, objectproperty, stringproperty kivy.uix.relativelayout import relativelayout kivy.animation import animation kivy.uix.label import label kivy.vector import vector kivy.clock import clock kivy.uix.button import button kivy.uix.screenmanager import screenmanager, screen kivy.graphics import line, color, rectangle functools import partial import socket, time, math, random, math random import randint kivy.lang import builder builder.load_file('shooting.kv') #setup graphics kivy.config import config config.set('graphics','resizable',0) #graphics fix kivy.core.window import window; window.clearcolor = (1,1,1,1) udp_ip = "10.0.1.5" udp_port = 5005 sock = socket.socket(socket.af_inet, socket.sock_dgram) class missile(widget): angle = numericproperty(0) source = stringproperty("") def expend_size(self): if (self.size[0] < 200): self.size = vector(3, 3) + self.size def cont_travel(self, velocity): self.pos = vector(*velocity) + self.pos def rotate(self, a): self.angle = class enemy(widget): velocity_x = numericproperty(0) velocity_y = numericproperty(0) spawnt = numericproperty(0) angle = numericproperty(0) source = stringproperty("") def move(self): self.x = self.x + self.velocity_x self.y = self.y + self.velocity_y def drawwalking(self, *large): self.canvas: self.source = './images/walk1.png' def changewalk(rect, newsource, *largs): rect.source = newsource clock.schedule_once(partial(changewalk, self, './images/walk2.png'),0.5) class hungerbar(widget): def hunger_dec(self): if (self.size[0] > 0): self.size = vector(-1, 0) + self.size def hunger_inc(self, increase): if (self.size[0] + increase < 100): self.size = vector(increase, 0) + self.size class score(label): def show_score(self, s): self.text = str(s) class testcircle(widget): def move(self, dt): self.x = self.x + self.velocity_x class shootinggame(widget): missile = missile() hungerbar = objectproperty(none) enemy = enemy() enemy_list = [] enemy_count = 0 enemy_amount = 1 # indicates if frog traveling or not travel = false pos_down = vector(0, 0) pos_up = vector(0, 0) time_down = 0 time_up = 0 enemy_on_left = true missile_onscreen = false vel = vector(0, 0) points = numericproperty(0) # timer controls if bug stops or moves movement_timer = 5 # if bug traveling or not movement = true def __init__(self, **kwargs): super(shootinggame, self).__init__(**kwargs) randx = random.choice([0.1, 0.2, 0.8, 0.9]) self.enemy = self.drawenemy(randx) clock.schedule_interval(self.enemy.drawwalking, 1) # self.hungerbar = self.drawbar() # following functions let users shoot missiles touching screen def on_touch_down(self, touch): # draws new object if there none on screen. checks side of bug if ((not self.missile_onscreen) , ((self.enemy_on_left , touch.x > (self.parent.width/2)) or ((not self.enemy_on_left) , touch.x < self.parent.width/2))): # generate new line self.canvas: color(1, 0, 0) self.line = line(points=(touch.x, touch.y, touch.x, touch.y), width = 2) # generate new missile self.missile_onscreen = true self.missile = missile() self.add_widget(self.missile) self.missile.pos = (touch.x - self.missile.size[0]/2 , touch.y - self.missile.size[0]/2) self.pos_down = vector(touch.x, touch.y) self.time_down = time.time() self.missile.source = 'atlas://images/sprite.atlas/frog' # rotate sprite # left if (touch.x < self.parent.width/2): self.missile.angle = -45 send_server(40002, 0.29, 400, 200) # right else: self.missile.angle = 45 send_server(50002, 0.29, 400, 200) def on_touch_move(self, touch): if (self.missile_onscreen): self.time_down = time.time() self.canvas: self.line.points = [touch.x, touch.y, self.pos_down.x, self.pos_down.y] if (touch.x - self.pos_down[0] != 0): = math.degrees(math.atan((-(touch.y - self.pos_down[1]))/(-(touch.x - self.pos_down[0])))) # left if (self.pos_down[0] < self.parent.width/2) , (touch.x - self.pos_down[0] < 0): self.missile.angle = a/2 - 45 # right elif (self.pos_down[0] > self.parent.width/2) , (touch.x - self.pos_down[0] > 0): self.missile.angle = a/2 + 45 def on_touch_up(self, touch): # gives speed , orientation if there no object traveling if (self.missile_onscreen , (self.vel == vector(0, 0))): self.time_up = time.time() self.pos_up = vector(touch.x, touch.y) self.missile.source = 'atlas://images/sprite.atlas/frog_jump' # remove line self.canvas.remove(self.line) self.vel = vector(-(self.pos_up[0] - self.pos_down[0])/10, -(self.pos_up[1] - self.pos_down[1])/10) # frog traveling (with speed threshold) if (abs(self.vel[0]) > 5 ) , (self.vel[1] != 0): print("x vel: "+str(self.vel[0])) print("y vel: "+str(self.vel[1])) # left if (self.vel[0] > 0 , self.pos_down[0] < (self.parent.width / 3)): self.travel = true (intensity, duration) = get_haptic_par(self.missile.size[0], self.vel, self.parent.width - self.pos_up[0]) # right send_server(40001, intensity, duration, 200) elif (self.vel[0] < 0 , self.pos_down[0] > (self.parent.width * 2 / 3)): self.travel = true (intensity, duration) = get_haptic_par(self.missile.size[0], self.vel, self.pos_up[0]) send_server(50001, intensity, duration, 200) else: self.remove_widget(self.missile) send_server(40003, 0, 0, 200) send_server(50003, 0, 0, 200) else: self.remove_widget(self.missile) self.missile_onscreen = false self.vel = vector(0, 0) send_server(40003, 0, 0, 200) send_server(50003, 0, 0, 200) def drawbar(self): bar = hungerbar() # self.hungerbar.size = vector(1000, 20) self.add_widget(bar) return bar # randomly generating enemy def drawenemy(self, x_pos): tmpenemy = enemy() tmpenemy.x = self.width * x_pos # 1280 width of screen if (tmpenemy.x < 1280/2): self.enemy_on_left = true else: self.enemy_on_left = false randpos = randint(10, 90) # 1200 width of screen tmpenemy.y = float(randpos) /100 * 800 tmpenemy.velocity_x = 0 tmpenemy.velocity_y = randint(3, 5) tmpenemy.spawnt = time.time() self.add_widget(tmpenemy) return tmpenemy def update(self, dt): # self.drawbar() # missile travels users flicks missile if (self.travel): self.missile.cont_travel(self.vel) if ((self.missile.right < 0) or (self.missile.x > self.parent.width) or (self.missile.top < 0) or (self.missile.y > self.parent.height)): self.travel = false self.missile_onscreen = false self.vel = vector(0, 0) # controls timing of enemy movoing # print(time.time() % self.movement_timer) if ((time.time() % self.movement_timer) < 0.015): self.movement = not (self.movement) self.movement_timer = random.randint(3, 5) if (self.movement): self.enemy.move() # animate bug turning around if (self.enemy.top > self.parent.height): angle = -90 animation(center=self.enemy.center, angle=angle, duration = 0.5).start(self.enemy) self.enemy.velocity_y = -(self.enemy.velocity_y) elif (self.enemy.y < 0): angle = 90 animation(center=self.enemy.center, angle=angle, duration = 0.5).start(self.enemy) self.enemy.velocity_y = -(self.enemy.velocity_y) # when frog catches bug if (self.missile , self.enemy.collide_widget(self.missile) , self.missile_onscreen): #calculate score depending on how long takes player hit enemy round_score = int(1.0/(time.time() - self.enemy.spawnt)/self.missile.size[0] * 1000) if round_score > 30: round_score = 30 elif round_score < 1: round_score = 1 if (self.enemy.x < self.parent.width / 2): send_server(60001, 0, 0, 200) if (self.enemy.x > self.parent.width / 2): send_server(60002, 0, 0, 200) # self.points = self.points + round_score score = score() score.pos = vector(self.enemy.x, self.enemy.y + 5) score.show_score("[color=ff3333]" + str(round_score) + "[/color]") self.add_widget(score) clock.schedule_once(lambda dt: self.remove_widget(score), 1) #removing missile - removed bug: frog gets stuck on edge of screen self.remove_widget(self.missile) self.missile_onscreen = false self.vel = vector(0, 0) # self.enemy_list.remove(e) def get_haptic_par(size, vel, canvaswidth): intensity = float(size)/300 * 0.29 vis_dur_tot = canvaswidth/(abs(vel[0]) * 60) * 1000 hap_dur_tot = 0.69 * vis_dur_tot + 137.02 #hap_dur_tot = dur + soa = dur + 0.28 * dur + 60.7 => dur = (hap_dur_tot - 60.7)/1.28 return (intensity, (hap_dur_tot - 60.7) /1.28) def send_server(*args): port = args[0] intensity = args[1] duration = args[2] frequency = args[3] message = "%d;%f;%f;%f" % (port, intensity, duration, frequency) sock.sendto(message, (udp_ip, udp_port)) class welcomescreen(screen): pass class basicscreen1(screen): pass class basicscreen2(screen): pass class basicscreen3(screen): pass class gamescreen(screen): def __init__(self, **kwargs): super(gamescreen, self).__init__(**kwargs) self.game = shootinggame() self.add_widget(self.game) clock.schedule_interval(self.game.update, 1.0 / 60.0) sm = screenmanager() sm.add_widget(welcomescreen(name='welcome')) sm.add_widget(basicscreen1(name='basic1')) sm.add_widget(basicscreen2(name='basic2')) basicscreen3 = basicscreen3(name='basic3') sm.add_widget(basicscreen3) game_screen = gamescreen(name='game') sm.add_widget(game_screen) class shootingapp(app): def printthis(self, x): print(x) def play_haptic(self, intensity, duration): send_server(40001, intensity, duration, 70) def play_ball(self, intensity, duration): ball = testcircle() # = 0.0106 * math.pow(10, (intensity - 38.892)/9.7721) # ball.size = vector(50, 50) ball.pos = vector(0,405) basicscreen3.add_widget(ball) lengtht = duration + 0.4 * duration + 0.28 * duration + 60.7 ball.velocity_x = ball.parent.width/lengtht * 20 clock.schedule_interval(ball.move, 1.0/60.0) if (ball.x > ball.parent.width): basicscreen3.remove_widget(ball) def build(self): return sm if __name__ == '__main__': shootingapp().run() the kivy file:
#:kivy 1.9.0 #:import atlas kivy.atlas.atlas #:import notransition kivy.uix.screenmanager.notransition #:import slidetransition kivy.uix.screenmanager.slidetransition <welcomescreen>: button: text: "learn tactile illusions" size_hint: none, none size: 500, 70 pos: 100, 200 font_size: 30 on_release: app.root.transition = slidetransition() app.root.current = "basic1" button: text: "play our game" size_hint: none, none size: 500, 70 pos: 100, 100 font_size: 30 on_release: app.root.transition = slidetransition() app.root.current = "game" <basicscreen1>: name: "basic1" label: text: "when 2 hands vibrated time lapse in middle, \nyou feel continuous motion across hands." font_size: 30 color: .8,.9,0,1 button: text: "play" size_hint: none, none size: 140, 70 pos: 1000, 200 font_size: 30 on_release: app.play_haptic(0.5, 400) button: text: "next" size_hint: none, none size: 140, 70 pos: 1000, 120 font_size: 30 on_release: app.root.transition = slidetransition() app.root.current = "basic2" <basicscreen2>: name: "basic2" label: text: "you can change speed of motion across hands. \nbut regardless of speed, motion feels continuous." font_size: 30 color: .8,.9,0,1 pos: 0, 150 slider: id: s1 value: 100 range: (100, 800) step: 1 pos: 0, 0 padding: 250 button: text: "play" size_hint: none, none size: 140, 70 pos: 1000, 200 font_size: 30 on_release: app.play_haptic(0.5, s1.value) button: text: "next" size_hint: none, none size: 140, 70 pos: 1000, 120 font_size: 30 on_release: app.root.transition = slidetransition() app.root.current = "basic3" <basicscreen3>: name: "basic3" label: text: "we can generate visual content optimally matches tactile illusion. \nthe 2 motions work generate coherent multimodal experience." font_size: 30 pos: 0, 200 color: .8,.9,0.1,1 label: text: "speed (fast <-> slow)" font_size: 30 pos: 0, -25 color: 0, 0, 0, 1 slider: id: s2 min: 100 max: 800 pos: 0, -75 padding: 250 button: text: "play" size_hint: none, none size: 250, 70 pos: 1000, 200 font_size: 30 on_release: app.play_ball(0.5, s2.value) app.play_haptic(0.5, s2.value) button: text: "play game" size_hint: none, none size: 250, 70 pos: 1000, 120 font_size: 30 on_release: app.root.transition = slidetransition() app.root.current = "game" <testcircle>: canvas: color: rgb: (0, 1, 1) ellipse: size: 100, 100 pos: self.pos <gamescreen>: name: "game" button: text: "back" size_hint: none, none size: 70, 70 pos: 0, 0 font_size: 30 on_release: app.root.transition = slidetransition(direction="right") root.game.points = 0 app.root.current = "welcome" <missile>: size: 50, 50 canvas.before: pushmatrix rotate: angle: self.angle origin: self.center canvas.after: popmatrix canvas: color: rgb: (1, 1, 1) rectangle: source: self.source pos: self.pos size: self.size <enemy>: size: 70, 70 canvas.before: pushmatrix rotate: angle: self.angle origin: self.center canvas.after: popmatrix canvas: color: rgb: (1, 1, 1) rectangle: source: self.source pos: self.pos size: self.size <hungerbar>: size: 100, 5 color: rgb: (0, 1, 1) canvas: rectangle: pos: self.width * 7, 30 <score>: font_size: 30 pos: self.pos text: self.text markup: true
<hungerbar>: size: 100, 5 color: rgb: (0, 1, 1) canvas: rectangle: pos: self.width * 7, 30 this rule adds color (which vertexinstruction) outside canvas block, kivy tries add widget.
Comments
Post a Comment