I need the type of code so that depending on which direction button was pressed last, the lasers fires in that direction. My code so far only shoots in a positive x axis. I need it to shoot in negative x axis, and positive and negative y axis as well.
Here is my code so far:
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.bg = SpriteNode('spc:BackgroundBlack')
self.lasers = []
#Creating the player
self.Player = SpriteNode('shp:RoundRect')
self.Player.color = ("cyan")
self.Player.anchor_point = (0.5, 0)
self.Player.position = (512, 400)
self.add_child(self.Player)
#Controlling the player
self.UpCrtl = SpriteNode('iow:arrow_up_b_32')
self.UpCrtl.x_scale = 3.5/1.0
self.UpCrtl.y_scale = 3.5/1.0
self.UpCrtl.alpha = 0.5
self.UpCrtl.position = (175, 295)
self.add_child(self.UpCrtl)
self.DownCrtl = SpriteNode('iow:arrow_down_b_32')
self.DownCrtl.x_scale = 3.5/1.0
self.DownCrtl.y_scale = 3.5/1.0
self.DownCrtl.alpha = 0.5
self.DownCrtl.position = (175, 130)
self.add_child(self.DownCrtl)
self.RightCrtl = SpriteNode('iow:arrow_right_b_32')
self.RightCrtl.x_scale = 3.5/1.0
self.RightCrtl.y_scale = 3.5/1.0
self.RightCrtl.alpha = 0.5
self.RightCrtl.position = (250, 212.5)
self.add_child(self.RightCrtl)
self.LeftCrtl = SpriteNode('iow:arrow_left_b_32')
self.LeftCrtl.x_scale = 3.5/1.0
self.LeftCrtl.y_scale = 3.5/1.0
self.LeftCrtl.alpha = 0.5
self.LeftCrtl.position = (100, 212.5)
self.add_child(self.LeftCrtl)
#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):
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 <= 1100:
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 <= 1100:
self.Player.position = (new_x, self.Player.position.y)
if touch.location in self.UpCrtl.bbox:
new_y = self.Player.position.y + 5
if new_y >= 0 and new_y <= 800:
self.Player.position = (self.Player.position.x, new_y)
if touch.location in self.DownCrtl.bbox:
new_y = self.Player.position.y - 5
if new_y >= 0 and new_y <= 800:
self.Player.position = (self.Player.position.x, new_y)
if touch.location in self.laserButton.bbox:
new_laser = SpriteNode('spc:LaserBlue6')
new_laser.position = (self.Player.position.x, self.Player.position.y + 60)
self.add_child(new_laser)
self.lasers.append(new_laser)
for l in self.lasers:
l.position = (l.position.x, l.position.y + 7.5)
if l.position.y > 800:
l.remove_from_parent()
self.lasers.remove(l)
if __name__ == '__main__':
run(Game(), LANDSCAPE, show_fps=True)