# coding: utf-8
from scene import *
A = Action
class TouchBall(Scene):
def setup(self):
ball_size = 1
bend = 10 # higher value puts less bend on the ball
self.background_color = 'green'
self.ball = SpriteNode('emj:White_Circle',parent=self,color='yellow')
self.ball.position = (self.size.x/2,200)
self.ball.scale = ball_size
self.x_motion = 0
self.y_motion = 0
self.bend = bend
self.can_hit = True
def update(self):
self.ball_move_logic()
self.wall_collision_logic()
self.reset()
def ball_move_logic(self):
x, y = self.ball.position
x += self.x_motion
y += self.y_motion
self.ball.position = (x,y)
def wall_collision_logic(self):
ball_x, ball_y = self.ball.position
half_ball_x_size = (self.ball.frame[2]/2)
half_ball_y_size = (self.ball.frame[3]/2)
x, y = self.size
if ball_x < (0+half_ball_x_size):
self.x_motion -= (self.x_motion*2)
if ball_x > (x-half_ball_x_size):
self.x_motion -= (self.x_motion*2)
if ball_y > (y-half_ball_y_size):
self.y_motion -= (self.y_motion*2)
self.can_hit = True
def reset(self):
x, y = self.size
ball_pos_x, ball_pos_y = self.ball.position
if ball_pos_y < (0-self.ball.frame[3]):
self.can_hit = True
self.ball.position = (self.size.x/2,200)
self.x_motion = 0
self.y_motion = 0
def touch_began(self, touch):
x, y = touch.location
ball_pos_x, ball_pos_y, ball_size_x, ball_size_y = self.ball.frame
if ball_pos_x < x < (ball_pos_x + ball_size_x) and ball_pos_y < y < (ball_pos_y + ball_size_y) and self.can_hit == True:
self.can_hit = False
side = (ball_pos_x + (ball_size_x/2)) - x
self.x_motion += side/self.bend
if self.y_motion == 0:
self.y_motion += 10
else:
self.y_motion -= (self.y_motion*2)
#touch graffic
cross = SpriteNode('shp:x4',parent=self)
cross.position = (x, y)
fade_out = A.fade_to(0)
cross.run_action(fade_out)
run(TouchBall(),PORTRAIT)
Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Best posts made by DanielDeiana
-
Keep the ball up
Latest posts made by DanielDeiana
-
Keep the ball up
# coding: utf-8 from scene import * A = Action class TouchBall(Scene): def setup(self): ball_size = 1 bend = 10 # higher value puts less bend on the ball self.background_color = 'green' self.ball = SpriteNode('emj:White_Circle',parent=self,color='yellow') self.ball.position = (self.size.x/2,200) self.ball.scale = ball_size self.x_motion = 0 self.y_motion = 0 self.bend = bend self.can_hit = True def update(self): self.ball_move_logic() self.wall_collision_logic() self.reset() def ball_move_logic(self): x, y = self.ball.position x += self.x_motion y += self.y_motion self.ball.position = (x,y) def wall_collision_logic(self): ball_x, ball_y = self.ball.position half_ball_x_size = (self.ball.frame[2]/2) half_ball_y_size = (self.ball.frame[3]/2) x, y = self.size if ball_x < (0+half_ball_x_size): self.x_motion -= (self.x_motion*2) if ball_x > (x-half_ball_x_size): self.x_motion -= (self.x_motion*2) if ball_y > (y-half_ball_y_size): self.y_motion -= (self.y_motion*2) self.can_hit = True def reset(self): x, y = self.size ball_pos_x, ball_pos_y = self.ball.position if ball_pos_y < (0-self.ball.frame[3]): self.can_hit = True self.ball.position = (self.size.x/2,200) self.x_motion = 0 self.y_motion = 0 def touch_began(self, touch): x, y = touch.location ball_pos_x, ball_pos_y, ball_size_x, ball_size_y = self.ball.frame if ball_pos_x < x < (ball_pos_x + ball_size_x) and ball_pos_y < y < (ball_pos_y + ball_size_y) and self.can_hit == True: self.can_hit = False side = (ball_pos_x + (ball_size_x/2)) - x self.x_motion += side/self.bend if self.y_motion == 0: self.y_motion += 10 else: self.y_motion -= (self.y_motion*2) #touch graffic cross = SpriteNode('shp:x4',parent=self) cross.position = (x, y) fade_out = A.fade_to(0) cross.run_action(fade_out) run(TouchBall(),PORTRAIT)
-
Number bonds, good for kids
# coding: utf-8 from scene import * import sound import random import math import console import speech A = Action happy_faces = (Texture('emj:Grinning'),Texture('emj:Kissing_2'),Texture('emj:Monkey_Face'),Texture('emj:Smiling_1'),Texture('emj:Smiling_2'),Texture('emj:Smiling_4'),Texture('emj:Smiling_6'),Texture('emj:Stuck-Out_Tongue_2'),Texture('emj:Winking')) speech.say('welcome to number bonds... what is your name please?') name = console.input_alert('WELCOME TO NUMBER BONDS🤔','Name',hide_cancel_button=True) thank_you = 'thank you %s' % name speech.say(thank_you) class rand_color(object): def get(self): return float(random.randrange(0,10)) / 10 class MyScene (Scene): def setup(self): #setup background and number key-pad# rect = ui.Path.rounded_rect(0,0,self.size.x-10,160,15) panel = ShapeNode(rect,parent=self) panel.position = (self.size.x/2,150) panel.color = '#ffffc9' panel.line_width = 10 panel.stroke_color = '#ffff28' col = rand_color() self.background_color = '#86aeff' self.number_list = [] self.total = 0 x = 30 y = 200 num = 0 while num < 11: a = col.get() b = col.get() c = col.get() number = LabelNode(str(num),parent=self) number.position = (x,y) number.scale = 3 number.color = (a,b,c) self.number_list.append(number) x += 50 num += 1 if x > 300: x = 30 y -= 100 #set up number bonds# x2, y2 = self.size / 2 circ_shape = ui.Path.oval(0,0,80,80) circle1 = ShapeNode(circ_shape,parent=self) circle1.position = (x2,y2+230) circle1.line_width = 10 circle1.stroke_color = '#ff6b6b' circle2 = ShapeNode(circ_shape,parent=self) circle2.position = (circle1.position.x/2,y2+160) circle2.line_width = 10 circle2.stroke_color = '#6b6bff' circle3 = ShapeNode(circ_shape,parent=self) circle3.position = (circle1.position.x/2*3,y2+160) circle3.line_width = 10 circle3.stroke_color = '#6b6bff' self.check = ShapeNode(rect,parent=self) self.check.position = (x2,y2+50) self.check.line_width = 10 self.check.stroke_color = '#000000' self.check.scale = 0.5 check_label = LabelNode('CHECK',parent=self) check_label.position = self.check.position check_label.color = 'black' check_label.scale = 2 self.master = random.randrange(30,100) self.num1 = random.randrange(1,self.master-10) self.answer = self.master - self.num1 self.guess = 0 self.master_label = LabelNode(str(self.master),parent=self) self.master_label.position = circle1.position self.master_label.color = 'black' self.master_label.scale = 2 self.num1_label = LabelNode(str(self.num1),parent=self) self.num1_label.position = circle2.position self.num1_label.color = 'black' self.num1_label.scale = 2 self.guess_label = LabelNode(str(self.guess),parent=self) self.guess_label.position = circle3.position self.guess_label.color = 'black' self.guess_label.scale = 2 self.tries = 0 self.correct = 0 self.correct_messages = ['well done','excellent','your the best','fandabidozee','boom ting','bang on','bet you do this in your sleep'] def reset(self): self.master = random.randrange(30,100) self.num1 = random.randrange(1,self.master-10) self.answer = self.master - self.num1 self.guess = 0 self.master_label.text = str(self.master) self.num1_label.text = str(self.num1) self.guess_label.text = str(self.guess) def update(self): pass def touch_began(self, touch): global name x, y = touch.location for number in self.number_list: x1, y1, x2, y2 = number.frame if x1 < x < (x1 + x2) and y1 < y < (y1 + y2): num = int(number.text) self.guess += num self.guess_label.text = str(self.guess) if num == 0: self.guess = 0 self.guess_label.text = str(self.guess) x1, y1, x2, y2 = self.check.frame if x1 < x < x1+x2 and y1 < y < y1+y2: if self.answer == self.guess: # IF ANSWER'S CORRECT speech.say(name) speech.say(self.correct_messages[random.randrange(0,len(self.correct_messages))]) self.tries += 1 self.correct += 1 sound.play_effect('game:Ding_3') animation = A.group(A.move_by(self.size.x+30,0,3),A.rotate_by(-5,3)) happy = SpriteNode(happy_faces[random.randrange(0,len(happy_faces))],parent=self) happy.position = (0,260) happy.run_action(animation) else: #IF ANSWER'S WRONG self.tries += 1 sound.play_effect('game:Error') self.reset() appraisal = self.tries % 5 if appraisal == 0 and self.tries > 0: message = '%d out of %d' % (self.correct, self.tries) console.hud_alert(message) if __name__ == '__main__': run(MyScene(),PORTRAIT, show_fps=False)```
-
RE: Toy Scene Script: Drag a ball around with elastic string
Hardcore maths!
I was looking to get a moveable line. Did you squeeze a rect and rotate it?