Help with bullet travel - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with bullet travel (/thread-18783.html) |
Help with bullet travel - GalaxyCoyote - May-31-2019 I am making a simple CTF game and the bullets aren't travelling, could someone help me? Blue_direction = 0 Red_direction = 0 game = True import turtle wn = turtle.Screen() wn.title("CTF 1v1") wn.bgcolor("black") wn.setup(width=800, height=600) wn.tracer(0) #Blue (right) Flag Blue_flag = turtle.Turtle() Blue_flag.speed(0) Blue_flag.shape("square") Blue_flag.color("blue") Blue_flag.penup() Blue_flag.goto(350, 0) #Blue fighter Blue_fighter = turtle.Turtle() Blue_fighter.speed(0) Blue_fighter.shape("circle") Blue_fighter.color("blue") Blue_fighter.penup() Blue_fighter.goto(350, 0) #Blue Bullet Blue_bullet = turtle.Turtle() Blue_bullet.speed(0) Blue_bullet.penup() Blue_bullet.shape("square") Blue_bullet.color("Yellow") Blue_bullet.penup() #Red (Left) Flag Red_flag = turtle.Turtle() Red_flag.speed(0) Red_flag.shape("square") Red_flag.color("red") Red_flag.penup() Red_flag.goto(-350, 0) #Red fighter Red_fighter = turtle.Turtle() Red_fighter.speed(0) Red_fighter.shape("circle") Red_fighter.color("red") Red_fighter.penup() Red_fighter.goto(-350, 0) #Red bullet Red_bullet = turtle.Turtle() Red_bullet.speed(0) Red_bullet.penup() Red_bullet.shape("square") Red_bullet.color("Yellow") Red_bullet.penup() #walls #midwall midwall = turtle.Turtle() midwall.speed(0) midwall.shape("square") midwall.color("white") midwall.shapesize(stretch_wid=2, stretch_len=1) midwall.penup() midwall.goto(0, 0) #blue spawn walls midwall = turtle.Turtle() midwall.speed(0) midwall.shape("square") midwall.color("white") midwall.shapesize(stretch_wid=5, stretch_len=1) midwall.penup() midwall.goto(250, 150) midwall = turtle.Turtle() midwall.speed(0) midwall.shape("square") midwall.color("white") midwall.shapesize(stretch_wid=5, stretch_len=1) midwall.penup() midwall.goto(250, -150) #red spawn walls midwall = turtle.Turtle() midwall.speed(0) midwall.shape("square") midwall.color("white") midwall.shapesize(stretch_wid=5, stretch_len=1) midwall.penup() midwall.goto(-250, -150) midwall = turtle.Turtle() midwall.speed(0) midwall.shape("square") midwall.color("white") midwall.shapesize(stretch_wid=5, stretch_len=1) midwall.penup() midwall.goto(-250, 150) #keybindings def Blue_move_up(): y = Blue_fighter.ycor() y += 30 Blue_fighter.sety(y) Blue_direction = "up" def Blue_move_left(): x = Blue_fighter.xcor() x -= 30 Blue_fighter.setx(x) Blue_direction = "left" def Blue_move_down(): y = Blue_fighter.ycor() y -= 30 Blue_fighter.sety(y) Blue_direction = "down" def Blue_move_right(): x = Blue_fighter.xcor() x += 30 Blue_fighter.setx(x) Blue_direction = "right" def Blue_shoot(): Blue_bullet.goto(Blue_pos) if Blue_direction == "up": ybb = Blue_bullet.ycor() ybb += 30 Blue_bullet.sety(y) if Blue_direction == "left": xbb = Blue_bullet.xcor() xbb -= 30 Blue_bullet.setx(x) if Blue_direction == "down": ybb = Blue_bullet.ycor() ybb -= 30 Blue_bullet.sety(y) if Blue_direction == "right": xbb = Blue_bullet.xcor() xbb += 30 Blue_bullet.setx(x) def Red_move_up(): y = Red_fighter.ycor() y += 30 Red_fighter.sety(y) Red_direction = "up" def Red_move_left(): x = Red_fighter.xcor() x -= 30 Red_fighter.setx(x) Red_direction = "left" def Red_move_down(): y = Red_fighter.ycor() y -= 30 Red_fighter.sety(y) Red_direction = "down" def Red_move_right(): x = Red_fighter.xcor() x += 30 Red_fighter.setx(x) Red_direction = "right" def Red_shoot(): Red_bullet.goto(Red_pos) if Red_direction == "up": yrb = Red_bullet.ycor() yrb += 30 Red_bullet.sety(y) if Red_direction == "left": xrb = Red_bullet.xcor() xrb -= 30 Red_bullet.setx(x) if Red_direction == "down": yrb = Red_bullet.ycor() yrb -= 30 Red_bullet.sety(y) if Red_direction == "right": xrb = Red_bullet.xcor() xrb += 30 Red_bullet.setx(x) wn.onkeypress(Blue_move_up, "w") wn.onkeypress(Blue_move_left, "a") wn.onkeypress(Blue_move_down, "s") wn.onkeypress(Blue_move_right, "d") wn.onkeypress(Blue_shoot, "e") wn.onkeypress(Red_move_up, "i") wn.onkeypress(Red_move_left, "j") wn.onkeypress(Red_move_down, "k") wn.onkeypress(Red_move_right, "l") wn.onkeypress(Red_shoot, "o") wn.listen() while True: wn.update() Blue_pos = Blue_fighter.pos() Red_pos = Red_fighter.pos() #collitionsNote: I am not really looking to optimise my code, only to solve the issue with bullet travel RE: Help with bullet travel - ichabod801 - May-31-2019 They move for me. Press the letter, they move. RE: Help with bullet travel - GalaxyCoyote - May-31-2019 (May-31-2019, 07:43 PM)ichabod801 Wrote: They move for me. Press the letter, they move.What version of python are you using? On my screen it just moves to the players position and does nothing... RE: Help with bullet travel - ichabod801 - May-31-2019 In 2.7 I get an AttributeError. In 3.6 the colored circles move, but the colored squares stay still. RE: Help with bullet travel - GalaxyCoyote - May-31-2019 (May-31-2019, 08:41 PM)ichabod801 Wrote: In 2.7 I get an AttributeError. In 3.6 the colored circles move, but the colored squares stay still. The coloured squares are the flags, they are meant to be still. The circles are the players, they move and the yellow squares are the bullets (They are supposed to shoot out according to the Blue and Red direction) RE: Help with bullet travel - ichabod801 - May-31-2019 Ah, I see. Well, they're not moving because you never move them. The only time you change the coordinates of the bullets is in Red_shoot and Blue_shoot. Those are only called on the right key presses. You would need another function to move them, or you would need to move them when the players move, in the Foo_move_bar functions. But that's only part of your problem. You "change" Red_direction in the Red_move functions. But you are changing a local value, not a global, because the assignment makes Python think it is a local variable. This is a problem with using globals and functions, you have to keep track of where you are using globals, and if you mess it up, it doesn't work. Now Red_shoot is checking the global because there is no assignment to indicate it is local (globals are supposed to be constants and not changed within functions). But since none of the move functions are changing the global, it is comparing to the 0 value the global is initialized with. I would recommend putting this into classes. You could have one class for player, and create instances for red and blue. The player class could keep track of the flag, the player circle, the bullet, and most importantly the direction. It would also remove the repeated code you have for red and blue. You could all global Red_direction to all of the red move functions, and likewise for blue. I would not recommend this though. It's going to make your code harder to maintain and expand.
RE: Help with bullet travel - GalaxyCoyote - May-31-2019 (May-31-2019, 11:14 PM)ichabod801 Wrote: Ah, I see. Well, they're not moving because you never move them. The only time you change the coordinates of the bullets is in Red_shoot and Blue_shoot. Those are only called on the right key presses. You would need another function to move them, or you would need to move them when the players move, in the Foo_move_bar functions. I have tried making blue and red_ direction a global variable and the bullet is still not moving. I am kind of having trouble understanding - sorry for causing you so much time. RE: Help with bullet travel - ichabod801 - May-31-2019 (May-31-2019, 11:29 PM)GalaxyCoyote Wrote: I have tried making blue and red_ direction a global variable and the bullet is still not moving. I am kind of having trouble understanding - sorry for causing you so much time. Don't be sorry for wasting my time, be sorry for choosing to use globals. Why don't you post your modified code, so I can see if you understood me correctly. RE: Help with bullet travel - GalaxyCoyote - Jun-01-2019 (May-31-2019, 11:34 PM)ichabod801 Wrote:(May-31-2019, 11:29 PM)GalaxyCoyote Wrote: I have tried making blue and red_ direction a global variable and the bullet is still not moving. I am kind of having trouble understanding - sorry for causing you so much time. Nah I didn't, I changed most variables to global lol. I have been trying to learn the differences and I now have a basic knowledge of global and local |