-
BurntRice
from scene import * import math import random import sound import ui A = Action() width, height = ui.get_screen_size() #Setting up meteorites #Setting up background colour for the entire scene class Game(Scene): def setup(self): self.bg = SpriteNode('spc:BackgroundBlack') self.lasers = [] self.items = [] self.LaserTime = 0 self.frame_counter = 0 self.meteor = [] #Creating the player self.Player = SpriteNode('iow:arrow_up_b_32') self.Player.color = (1.0, 1.0, 1.0) self.Player.anchor_point = (0.5, 0.5) self.Player.x_scale = 2/1.0 self.Player.y_scale = 3/1.0 self.Player.position = (512, 400) self.Player.rotation = 0.0 self.add_child(self.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.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) #The score label self.score_label = LabelNode(text='Score: 0') self.score_label.anchor_point = (0, 0) self.score_label.position = (10, 790) self.score_label.font = ('Joystix', 30) self.add_child(self.score_label) #Movement code. def update(self): self.handle_button_presses() self.move_lasers() self.update_lasers() self.spawn_meteor() width, height = ui.get_screen_size() self.LaserTime = self.LaserTime + 1 def handle_button_presses(self): rotationAdd = math.pi/180 self.Player.location = self.Player.position for touch in self.touches.values(): if touch.location in self.LeftCrtl.bbox: actions = [A.rotate_by(math.pi/180)] self.Player.rotation = self.Player.rotation + rotationAdd self.Player.run_action(A.sequence(actions)) if touch.location in self.RightCrtl.bbox: actions = [A.rotate_by(math.pi/-180)] self.Player.rotation = self.Player.rotation - rotationAdd self.Player.run_action(A.sequence(actions)) if touch.location in self.UpCrtl.bbox: direction = Vector2(-math.sin(self.Player.rotation), math.cos(self.Player.rotation)) self.Player.position += direction*3 self.Player.position.x %= width self.Player.position.y %= height def move_lasers(self): for touch in self.touches.values(): if touch.location in self.laserButton.bbox and self.LaserTime >= 15: #reset the time self.LaserTime = 0 new_laser = SpriteNode('shp:Circle') new_laser.x_scale = 0.3/1.0 new_laser.y_scale = 0.3/1.0 new_laser.position = self.Player.position new_laser.rotation = self.Player.rotation self.add_child(new_laser) self.lasers.append(new_laser) sound.play_effect('arcade:Laser_6') def update_lasers(self): for l in self.lasers: l.direction = Vector2(-math.sin(l.rotation), math.cos(l.rotation))*10 l.position += l.direction if l.position.x < 0: l.remove_from_parent() self.lasers.remove(l) if l.position.x > 1112: l.remove_from_parent() self.lasers.remove(l) if l.position.y < 0: l.remove_from_parent() self.lasers.remove(l) if l.position.y > 834: l.remove_from_parent() self.lasers.remove(l) def spawn_meteor(self): self.frame_counter = self.frame_counter + 1 if self.frame_counter >= 60: self.frame_counter = 0 new_meteor = SpriteNode('spc:MeteorGraySmall1') new_meteor.position = (random.randint(0, 1024), 834) self.add_child(new_meteor) self.meteor.append(new_meteor) for meteor in self.meteor: meteor.position = (meteor.position.x, meteor.position.y - 5) if meteor.position.y < - 100: meteor.remove_from_parent() self.meteor.remove(meteor) if __name__ == '__main__': run(Game(), LANDSCAPE, show_fps=True)
-
BurntRice
I tried placing it self.update, but then it wouldn’t work.
-
BurntRice
width, height = ui.get_screen_size()
I think this is right. I did a seperate script where I print it out inside the console, and it worked. -
-
BurntRice
Here you go:
def handle_button_presses(self): rotationAdd = math.pi/180 self.Player.location = self.Player.position for touch in self.touches.values(): if touch.location in self.LeftCrtl.bbox: actions = [A.rotate_by(math.pi/180)] self.Player.rotation = self.Player.rotation + rotationAdd self.Player.run_action(A.sequence(actions)) if touch.location in self.RightCrtl.bbox: actions = [A.rotate_by(math.pi/-180)] self.Player.rotation = self.Player.rotation - rotationAdd self.Player.run_action(A.sequence(actions)) if touch.location in self.UpCrtl.bbox: direction = Vector2(-math.sin(self.Player.rotation), math.cos(self.Player.rotation)) self.Player.position += direction*3 if self.Player.position.x <= 0 or self.Player.position.x >= 1112: self.Player.position.x %= width if self.Player.position.y <= 0 or self.Player.position.y >= 834: self.Player.position.y %= height
I did change the movement code a little.
-
BurntRice
Whenever the Players position.x is equal to or smaller than 0 or whenever the position.x is equal to or greater than 1112, you do the remainder of the division of the screen height.
-
BurntRice
I have added this:
width, height = ui.get_screen_size()
And then added this later:
if self.Player.position.x <= 0 or self.Player.position.x >= 1112:
self.Player.position.x %= width
if self.Player.position.y <= 0 or self.Player.position.y >= 834:
self.Player.position.y %= height
This didn’t work. I checked if I had gotten the width and height variables in a seperate script, and I did. I am now stumped. -
-
BurntRice
Wait, how do find the screen width and height? And what about location?
-
-
BurntRice
Also, I would also like to know how to code screen wrapping.
-
BurntRice
I think I’ve finally done it! It works, as it shoots in the way I want it, and keeps on travelling even when I haven’t touched it, thanks.
All I have to do is add a action so I can’t spam or hold down on the button. What is the best way to add this? -
BurntRice
I’m still working on it, but I added this:
def update_lasers(self): for l in self.lasers: l.direction = Vector2(0,1) l.rotation = self.Player.rotation l.position = l.position + l.direction
I just want to check if I’m getting side tracked or not.
-
BurntRice
The code runs whenever the laser button detects that someone has touched it.
-
BurntRice
def update(self):
self.handle_button_presses()
self.move_lasers()
if random.random() < 0.01:
self.spawn_item()def handle_button_presses(self): for touch in self.touches.values(): if touch.location in self.LeftCrtl.bbox: new_x = self.Player.position.x - 5 self.Player.rotation = math.pi/2 if new_x >= 0 and new_x <= 1100: self.Player.position = (new_x, self.Player.position.y) if touch.location in self.RightCrtl.bbox: new_x = self.Player.position.x + 5 self.Player.rotation = math.pi*3/2 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 self.Player.rotation = 0.0 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 self.Player.rotation = math.pi if new_y >= 0 and new_y <= 800: self.Player.position = (self.Player.position.x, new_y) def move_lasers(self): for touch in self.touches.values(): if touch.location in self.laserButton.bbox: new_laser = SpriteNode('shp:Circle') new_laser.x_scale = 0.25/1.0 new_laser.y_scale = 0.25/1.0 new_laser.position = self.Player.position new_laser.rotation = self.Player.rotation direction = Vector2(-math.sin(new_laser.rotation), math.cos(new_laser.rotation)) self.add_child(new_laser) self.lasers.append(new_laser) for l in self.lasers: l.position += direction*75
-
BurntRice
I want my existing lasers to keep on moving. I have also implemented your idea of rewriting my update function.
I really don’t know how to though. If I use a touch.began method, it runs once, but stays in it’s place.
Am I forgetting to add something? -
BurntRice
I have been looking for quite a bit, and I’m really blind, but I can’t find any indentation errors.
from scene import * import math 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('spc:Fire1') self.Player.color = ("cyan") self.Player.anchor_point = (0.5, 0.5) self.Player.x_scale = 2/1.0 self.Player.y_scale = 1.5/1.0 self.Player.position = (512, 400) self.Player.rotation = 0.0 self.add_child(self.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) #The score label self.score_label = LabelNode(text='Score: 0') self.score_label.anchor_point = (0, 0) self.score_label.position = (10, 790) self.score_label.font = ('Arial Rounded MT Bold', 30) self.add_child(self.score_label) #Movement code. def update(self): for touch in self.touches.values(): if touch.location in self.LeftCrtl.bbox: new_x = self.Player.position.x - 5 self.Player.rotation = math.pi/2 if new_x >= 0 and new_x <= 1100: self.Player.position = (new_x, self.Player.position.y) if touch.location in self.RightCrtl.bbox: new_x = self.Player.position.x + 5 self.Player.rotation = math.pi*3/2 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 self.Player.rotation = 0.0 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 self.Player.rotation = math.pi 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('shp:Circle') new_laser.x_scale = 0.25/1.0 new_laser.y_scale = 0.25/1.0 new_laser.position = self.Player.position new_laser.rotation = self.Player.rotation direction = Vector2(-math.sin(new_laser.rotation), math.cos(new_laser.rotation)) self.add_child(new_laser) self.lasers.append(new_laser) for l in self.lasers: l.position += direction*75 if __name__ == '__main__': run(Game(), LANDSCAPE, show_fps=True)
-
BurntRice
I’ve made it so my laser shoots in the direction that I want it to, but, they are not moving. Once one laser pops up, the next one goes 75 pixels ahead, for ad infinitum. I don’t know if this is because of where I indented my direction value, but I don’t think so as that is mostly for rotation; nothing about moving forward etc.
-
BurntRice
from scene import * import math 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.5) self.Player.position = (512, 400) self.Player.rotation = 0.0 self.add_child(self.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) #The score label self.score_label = LabelNode(text='Score: 0') self.score_label.anchor_point = (0, 0) self.score_label.position = (10, 790) self.score_label.font = ('Arial Rounded MT Bold', 30) self.add_child(self.score_label) #Movement code. def update(self): for touch in self.touches.values(): if touch.location in self.LeftCrtl.bbox: new_x = self.Player.position.x - 5 self.Player.rotation = math.pi*3/2 if new_x >= 0 and new_x <= 1100: self.Player.position = (new_x, self.Player.position.y) if touch.location in self.RightCrtl.bbox: new_x = self.Player.position.x + 5 self.Player.rotation = math.pi/2 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 self.Player.rotation = 0.0 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 self.Player.rotation = math.pi 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.rotation = self.Player.rotation direction = Vector2(-math.sin(new_laser.rotation), math.cos(new_laser.rotation)) self.add_child(new_laser) self.lasers.append(new_laser) for l in self.lasers: l.position += direction*75 if __name__ == '__main__': run(Game(), LANDSCAPE, show_fps=True)
-
BurntRice
Um, sorry? I don’t really understand. I have properly indented the for l in self.lasers part, I don’t know how to add l.rotation to set motion direction.