Forum Archive

Why is my movement code wrong?

BurntRice

I don’t know my code isn’t working, (I’m pretty new to this), and I need to fix this soon as this is a school project. Please help.
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')

        #Creating the player
        self.Player = SpriteNode('shp:RoundRect')
        self.Player.color = ("cyan")
        self.Player.anchor_point = (0.5, 0)
        self.Player.position = (600, 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 <= 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 __name__ == '__main__':
    run(Game(), LANDSCAPE, show_fps=True)
cvp

@BurntRice indentation problem of def update, must be at same level as def setup

        self.laserButton.position = (1000, 212.5)


    def update(self):
BurntRice

Thank you so much.

ccc

Off topic but some repetitive code could be eliminated...

    def make_arrow(image_name: str, position) -> SpriteNode:
        sprite_node = SpriteNode(image_name)
        sprite_node.x_scale = 3.5/1.0
        sprite_node.y_scale = 3.5/1.0
        sprite_node.alpha = 0.5
        sprite_node.position = position
        self.add_child(sprite_node)
        return sprite_node

and then...

        self.UpCrtl = self.make_arrow('iow:arrow_up_b_32', (175, 295))
        self.DownCrtl = self.make_arrow('iow:arrow_down_b_32', (175, 130))
        self.RightCrtl = self.make_arrow('iow:arrow_right_b_32', (250, 212.5))
        self.LeftCrtl = self.make_arrow('iow:arrow_left_b_32', (100, 212.5))
BurntRice

Thank you. I’m a beginner so learning to simplify code will be really good.

BurntRice

I have another problem, with the up and down buttons (I haven’t implemented your code yet but I will soon.)
Here is my code.

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')

        #Creating the player
        self.Player = SpriteNode('shp:RoundRect')
        self.Player.color = ("cyan")
        self.Player.anchor_point = (0.5, 0)
        self.Player.position = (600, 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 <= 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:
                    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:
                    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=True)
cvp

@BurntRice you did inverse x and y

            if touch.location in self.UpCrtl.bbox:
                    new_y = self.Player.position.y + 5
                    if new_y >= 0 and new_y <= 1024:
                        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 <= 1024:
                        self.Player.position = (self.Player.position.x, new_y)