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)