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.
Help me find what’s wrong with my code.
-
I don’t know what is wrong with my code, the touch mechanism.
from scene import * import turtle import ui import math import random A = Action() #Setting up background colour for the entire scene class Game(Scene): def setup(self): self.background_color = 0.1, 0.1, 0.1 self.frame_counter = 0 #Creating the player self.Player = SpriteNode('shp:RoundRect') self.Player.color = ("cyan") self.Player.anchor_point = (0.5, 0) self.Player.position = (self.size.w/2, 400) self.add_child(self.Player) #Controlling the player self.UpCrtl = SpriteNode('shp:RoundRect') self.UpCrtl.x_scale = 2/1 self.UpCrtl.y_scale = 2/1 self.UpCrtl.alpha = 0.5 self.add_child(self.UpCrtl) self.UpCrtl.position = (175, 295) self.DownCrtl = SpriteNode('shp:RoundRect') self.DownCrtl.x_scale = 2/1 self.DownCrtl.y_scale = 2/1 self.DownCrtl.alpha = 0.5 self.add_child(self.DownCrtl) self.DownCrtl.position = (175, 130) self.RightCrtl = SpriteNode('shp:RoundRect') self.RightCrtl.x_scale = 2/1 self.RightCrtl.y_scale = 2/1 self.RightCrtl.alpha = 0.5 self.add_child(self.RightCrtl) self.RightCrtl.position = (250, 212.5) self.LeftCrtl = SpriteNode('shp:RoundRect') self.LeftCrtl.x_scale = 2/1 self.LeftCrtl.y_scale = 2/1 self.LeftCrtl.alpha = 0.5 self.add_child(self.LeftCrtl) self.LeftCrtl.position = (100, 212.5) #The button for shooting self.laserButton = SpriteNode('shp:Circle') self.laserButton.color = ('gray') self.laserButton.x_scale = 3/1 self.laserButton.y_scale = 3/1 self.add_child(self.laserButton) self.laserButton.position = (1000, 212.5) def update(self): pos.x = max(0, min(self.size.w, pos.x)) pos.y = max(0, min(self.size.h, pos.y)) self.frame_counter = self.frame_counter + 1 if self.frame_counter >= 120: self.frame_counter = 0 for touch in self.touches.values(): if touch.location in self.LeftCrtl.bbox: #left button pressed new_x = self.player.position.x - 5 if new_x >= 0 and new_x <= 1024: self.player.position = (new_x, self.player.position.y) if touch.location in self.RightCrtl.bbox: #right button pressed new_x = self.player.position.x + 5 if new_x >= 0 and new_x <= 1024: self.player.position = (new_x, self.player.position.y) if touch.location in self.UpCrtl.bbox: #Up button pressed new_y = self.player.position.y + 5 if new_y >= 0 and new_y <= 1024: self.player.position = (new_y, self.player.position.x) if touch.location in self.DownCrtl.bbox: #Down button pressed new_y = self.player.position.y - 5 if new_y >= 0 and new_y <= 1024: self.player.position = (new_y, self.player.position.x) if __name__ == '__main__': run(Game(), LANDSCAPE, show_fps=False)
-
@BurntRice Touch mechanism
-
Not sure if this is just a problem with pasting code, but your for loop is at the wrong indent level. Check that is properly indented.
I find it is helpful to add printouts so you can see what is happening. Is the for touch loop even running (are you receiving touches? Add a print inside the for loop). Is the bbox checking code working ? (Add a print inside each if condition). Is your newx limit checking code failing? Add an else and add a print.
It may also be useful to have debug code that adds LabelNodea with persistent status info that gets updated -- like touch position, player position, button bbox position, etc. That way you find find obvious logic errors, coordinate system assumption errors, etc. -
@BurntRice same remarks as @jonb: indentation seems invalid. For instnce:
- def update seems to be internal to def setup
- loop on touch seems to be internal to setup, thus done only once
Other remark, in up and down checking, you did swap x and y
self.player.position = (new_y, self.player.position.x)