Simple Python Turtle Graphics Game (Part 3)

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ม.ค. 2025

ความคิดเห็น • 102

  • @Nicc-zq7fn
    @Nicc-zq7fn 5 ปีที่แล้ว +2

    I'm having an error in which after the borders are drawn the program closes.
    Here is the error message:
    File "C:\Users\Nick\Desktop\turtlegame.py", line 51, in
    if player.xcor() > 300 or player.xcor < -300:
    TypeError: '

    • @dearspocari2792
      @dearspocari2792 3 ปีที่แล้ว +1

      change it to this: if player.xcor() > 300 or player.xcor() < -300:

  • @Mikha758
    @Mikha758 5 ปีที่แล้ว

    I am making a game and I was going to do the same as exactly like yours, but I get confused about where to put that line of code. Because I had a loop inside the main loop. So how to solve that???? Can you please help me? Anyway, that programming tutorial is really great!!! Thanks!!! : )

  • @maddyschad6649
    @maddyschad6649 8 ปีที่แล้ว +1

    Why does my turtle graphics screen crash when I start it after I put the border check? I put the same border check thing and I don't know what the problem is. I think the loop is doing it.

  • @tota3683
    @tota3683 8 ปีที่แล้ว

    bare in mind that the boundery checking should be if and elif, otherwhise the player is just gone :-)), because turn(180) and turn(180) == 360!!

  • @jamesinman9262
    @jamesinman9262 9 ปีที่แล้ว +1

    @Christian Thompson
    I am on 3.5 and trying to make my code compatible but my borders arent working and speed is out of control + mypen is going into middle of the screen, even though I tried typing this:
    mypen.setpos(0, 0)
    but this doesnt work!!! uggggghhhhnnn!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

    • @sfm4life613
      @sfm4life613 8 ปีที่แล้ว

      James Inman I'm sure your well past this but I feel like answering.
      0,0 is the center of the screen... In the x,y plane, there are four quadrants with 0,0 at the center

    • @kscdisappointment2404
      @kscdisappointment2404 7 ปีที่แล้ว

      Christian Thompson my y boundary doesn't work

    • @kscdisappointment2404
      @kscdisappointment2404 7 ปีที่แล้ว

      nvm I fixed it

  • @opeoluwakusa4127
    @opeoluwakusa4127 4 ปีที่แล้ว

    when i use the while true loop the game hangs and doesn't rsspond

  • @duldularmar
    @duldularmar 3 ปีที่แล้ว +1

    Hey can u help me i cant get it to turn 180 degrees when it hits the border

    • @TokyoEdTech
      @TokyoEdTech  3 ปีที่แล้ว

      What have you tried so far?

    • @duldularmar
      @duldularmar 3 ปีที่แล้ว

      @@TokyoEdTech i got it thanks for replying 🥰

    • @TokyoEdTech
      @TokyoEdTech  3 ปีที่แล้ว

      You're welcome.

    • @duldularmar
      @duldularmar 3 ปีที่แล้ว

      @@TokyoEdTech i have a problem can u help me

    • @TokyoEdTech
      @TokyoEdTech  3 ปีที่แล้ว

      I can try...

  • @thefatkid12
    @thefatkid12 11 หลายเดือนก่อน +1

    hey mr.tokyo can u help me with this
    if player.xcor() > 300 or player.xcor < - 300:
    player.right(180)
    and i have this error
    Traceback (most recent call last):
    File "C:\Users\Yishin\AppData\Local\Programs\Python\Python38-32\pyhtion.py", line 61, in
    if player.xcor() > 300 or player.xcor < - 300:
    TypeError: '

    • @TokyoEdTech
      @TokyoEdTech  11 หลายเดือนก่อน

      Yep. Change player.xcor to player.xcor()

  • @madebyluque
    @madebyluque 7 ปีที่แล้ว +1

    my turtle stopped moving after the boundary checking. what can i do?

    • @madebyluque
      @madebyluque 7 ปีที่แล้ว +1

      solved it!

    • @gregorymcgraw1290
      @gregorymcgraw1290 5 ปีที่แล้ว +1

      How did you solve it? Im having the same problem

  • @mitsuo233
    @mitsuo233 4 ปีที่แล้ว

    Thanks for this tutorial. I don't know why I need to apply the onkey method to wn. Using Repl.it, I can't apply the onkey method to the turtle object.

    • @TokyoEdTech
      @TokyoEdTech  4 ปีที่แล้ว

      You're welcome - there were some changes to the turtle module between Python 2 and 3 - so the onkeymethod was added and is supposed to be applied to the window object, not the turtle object.

  • @josefrancisco471
    @josefrancisco471 7 ปีที่แล้ว

    Hello, when i set my position to draw the border there is a line from the center to what ever position i set the turtle, can you help me??

    • @daniellamackella
      @daniellamackella 5 ปีที่แล้ว

      pen.penup()
      pen.goto(###, ###)
      pen.pendown()
      pen.goto(###, ###)

  • @menheartastudillo3593
    @menheartastudillo3593 2 ปีที่แล้ว +1

    what's application is that?

    • @TokyoEdTech
      @TokyoEdTech  2 ปีที่แล้ว +1

      Probably at the time I used BBEdit, but nowadays I use Geany.

  • @alexhauser8981
    @alexhauser8981 4 ปีที่แล้ว +1

    while True:
    wn.update()
    wn.mainloop()
    #Border Checking
    if whalo.xcor()>300 or whalo.xcor()

    • @alexhauser8981
      @alexhauser8981 4 ปีที่แล้ว

      @@TokyoEdTech thanks:)

    • @alexhauser8981
      @alexhauser8981 4 ปีที่แล้ว

      @@TokyoEdTech pastebin.com/HSyEpxqT
      The window dosent close now, thanks. But the borders arent working:(

    • @duldularmar
      @duldularmar 3 ปีที่แล้ว

      @@alexhauser8981 what did he tell u im also having the same problem

  • @koolkd3724
    @koolkd3724 4 ปีที่แล้ว

    I thought, if you get to fast you some times cant control, so I made the back arrow to get slow!

    • @99vikram99
      @99vikram99 2 วันที่ผ่านมา +1

      bro you can even go in negative speed

    • @TokyoEdTech
      @TokyoEdTech  วันที่ผ่านมา

      Amazing!

  • @lirdib9596
    @lirdib9596 5 ปีที่แล้ว

    @Christian Thompson pls help i am getting this error:
    TypeError: penup() missing 1 required positional argument: 'self

    • @Beastpig41
      @Beastpig41 5 ปีที่แล้ว

      Are you doing "your turtle".penup()

    • @lirdib9596
      @lirdib9596 5 ปีที่แล้ว

      @@Beastpig41 i m not sure what you mean but i have done this
      mypen = turtle.Turtle
      mypen.penup()

    • @Beastpig41
      @Beastpig41 5 ปีที่แล้ว

      @@lirdib9596 it could be mypen =turtle.Turtle()

    • @lirdib9596
      @lirdib9596 5 ปีที่แล้ว

      @@TokyoEdTech thanks

    • @lirdib9596
      @lirdib9596 5 ปีที่แล้ว

      @@TokyoEdTech and can i ask you one more question why don t you make some tutortials on pygame

  • @arngren47
    @arngren47 7 ปีที่แล้ว +1

    my borders are not working i can go right trough them ): plz help me

    • @arngren47
      @arngren47 7 ปีที่แล้ว

      import turtle
      wn = turtle.Screen()
      wn.bgcolor("lightgreen")
      player = turtle.Turtle()
      player.color("blue")
      player.shape("triangle")
      player.penup()
      player.speed(0)
      goal = turtle.Turtle()
      goal.color("red")
      goal.shape("circle")
      goal.penup()
      goal.speed(0)
      goal.setpos
      mypen = turtle.Turtle()
      mypen.penup()
      mypen.setposition(-300,-300)
      mypen.pendown()
      mypen.pensize(3)
      for side in range(4):
      mypen.forward(600)
      mypen.left(90)
      mypen.hideturtle()
      speed = 1
      def turnleft():
      player.left(30)
      def turnright():
      player.right(30)
      def increasespeed():
      global speed
      speed += 1
      turtle.listen()
      turtle.onkey(turnleft, "Left")
      turtle.onkey(turnright, "Right")
      turtle.onkey(increasespeed, "Up")
      mypen = turtle.Turtle()
      mypen.penup()
      mypen.setposition(-300,-300)
      if player.xcor() > 300 or player.xcor() < -300:
      player.right(180)
      if player.ycor() > 300 or player.ycor() < -300:
      player.right(180)
      while True:
      player.forward(speed)
      Btw im using python 3.4.0

    • @arngren47
      @arngren47 7 ปีที่แล้ว

      Oh and i cant use pastebin for some reason

    • @arngren47
      @arngren47 7 ปีที่แล้ว

      Christian Thompson ok thx
      (:

    • @generalgrevious3589
      @generalgrevious3589 5 ปีที่แล้ว

      @@TokyoEdTech I have the exact code as him and it is still isn't working

    • @generalgrevious3589
      @generalgrevious3589 5 ปีที่แล้ว

      @@TokyoEdTech pastebin.com/HbL0NSYj

  • @vtvtify
    @vtvtify 8 ปีที่แล้ว

    amazing vid

  • @haversonferris1825
    @haversonferris1825 6 ปีที่แล้ว

    @Christian Thompson
    i am having trouble with the code the turtle won't move
    import turtle
    wn=turtle.Screen()
    wn.bgcolor("lightgreen")
    mp=turtle.Turtle()
    mp.pu()
    mp.setposition(-300,-300)
    mp.pd()
    mp.pensize(3)
    for side in range(4):
    mp.fd(600)
    mp.lt(90)
    mp.hideturtle()
    ply=turtle.Turtle()
    ply.color("grey")
    ply.shape("triangle")
    ply.pu()
    ply.speed(0)
    speed=1
    def turnleft():
    ply.lt(30)
    def turnright():
    ply.rt(30)
    def incrspeed():
    global speed
    speed+=1
    wn.listen()
    wn.onkey(turnleft,"left")
    wn.onkey(turnright,"right")
    wn.onkey(incrspeed,"up")
    while True:
    ply.fd(speed)
    if ply.ycord()>300 or ply.ycord()300 or ply.xcord()

  • @koolkd3724
    @koolkd3724 4 ปีที่แล้ว +1

    sure!

  • @anshik.k.t
    @anshik.k.t 3 ปีที่แล้ว

    if player hits the corner, its geting out of boundary? why is this?

    • @TokyoEdTech
      @TokyoEdTech  3 ปีที่แล้ว

      Long story short - it wasn't coded very well. I made this for 10 year old students so I had to keep it simple.

    • @anshik.k.t
      @anshik.k.t 3 ปีที่แล้ว +1

      @@TokyoEdTech ok sir, no problem. I jsut wanted to know the reason why this was happening so I had asked.

    • @TokyoEdTech
      @TokyoEdTech  3 ปีที่แล้ว

      No worries - it's a good question!

  • @sukruerdemgok9487
    @sukruerdemgok9487 6 ปีที่แล้ว

    I write this
    if player.xcor() > 300 or player.xcor() < -300:
    player.right(45)
    if player.ycor() > 300 or player.ycor() < -300:
    player.right(45)
    but the borders not working.I can go trough them plz help :(

    • @sukruerdemgok9487
      @sukruerdemgok9487 6 ปีที่แล้ว

      import turtle
      x = turtle.Screen
      border =turtle.Turtle()
      border.penup()
      border.setposition( -300,-300)
      border.pendown()
      border.pensize(3)
      for side in range(4) :
      border.forward(600)
      border.left(90)

      kalem = turtle.Turtle()
      kalem.color("Blue")
      kalem.penup()
      speed =1
      def turnleft():
      kalem.left(29)
      def turnright():
      kalem.right(29)
      def increasespeed():
      global speed
      speed+=1
      turtle.listen()
      turtle.onkey(turnleft, "Left")
      turtle.onkey(turnright, "Right")
      turtle.onkey(increasespeed,"Up")
      while True :
      kalem.forward(speed)
      if kalem.xcor() > 300 or kalem.xcor() < -300 :
      kalem.right(45)
      if kalem.ycor() > 300 or kalem.ycor() < -300:
      kalem.right(45)

    • @sukruerdemgok9487
      @sukruerdemgok9487 6 ปีที่แล้ว

      I can't speak english very well but ı now little

    • @sukruerdemgok9487
      @sukruerdemgok9487 6 ปีที่แล้ว

      I live in Turkey and when I want to use pastebin.com, google say this;
      "The protection measure has been taken for this website (pastebin.com) according to decision nr. 2012/27976 dated 08/03/2012 of "Ankara CBS" has been implemented by the Information Comunication Technologies Authority." so I can send you this only from g-mail.

  • @zitroxlpthegreenone9324
    @zitroxlpthegreenone9324 3 ปีที่แล้ว +3

    make it turtle.onkey --->>> to turtle.onkeypress.. makes it a littlebit better to play

    • @TokyoEdTech
      @TokyoEdTech  3 ปีที่แล้ว

      Thanks for the suggestion.

    • @BlazeTheCatMan
      @BlazeTheCatMan ปีที่แล้ว

      i figured that out on my own for once! and also be aware it has a turning issue to where your player will keep turning for about 2 seconds or glitch out if anyone has a fix for that!

  • @cbtube6372
    @cbtube6372 7 ปีที่แล้ว

    guys can someone help with a border if player.xcor() >= 300 or player.xcor()

    • @yesveryprofesionalnameyes6055
      @yesveryprofesionalnameyes6055 6 ปีที่แล้ว

      if you want a answer make another comment like this:
      @Christian Thompson
      and paste your entire code

  • @jamesinman9262
    @jamesinman9262 9 ปีที่แล้ว

    never mind got it now its mypen.setpos(275, -275)
    and the square is mypen.forward(525)

    • @jasonchu8710
      @jasonchu8710 5 ปีที่แล้ว

      @@TokyoEdTech This uses classes too right

  • @sorymillimono5931
    @sorymillimono5931 5 ปีที่แล้ว

    you are the best

    • @sorymillimono5931
      @sorymillimono5931 5 ปีที่แล้ว

      @@TokyoEdTech okey not problem , i am french ,that is what I make mistakes in comment sorry,but I already signed

  • @stigidystacks3747
    @stigidystacks3747 3 ปีที่แล้ว

    My code is pretty much identical to yours but it runs way less smooth, anyone know why? (It’s like my FPS is way lower)

    • @TokyoEdTech
      @TokyoEdTech  3 ปีที่แล้ว

      You'll have to share your code so I can take a look.

    • @stigidystacks3747
      @stigidystacks3747 3 ปีที่แล้ว

      @@TokyoEdTech here’s my code (it’s a turtle you can control and a ball that bounces around in a squares):
      import turtle
      import math
      wn = turtle.Screen()
      wn.bgcolor("lightgreen")
      wn.title("Bouncing Balls")
      #Ritar banan
      mypen = turtle.Turtle()
      mypen.penup()
      mypen.setposition(-300,-300)
      mypen.pendown()
      mypen.pensize(3)
      mypen.speed(10)
      for side in range(4):
      mypen.forward(600)
      mypen.left(90)
      mypen.hideturtle()
      #Skapar spelaren
      player = turtle.Turtle()
      player.color("blue")
      player.shape("turtle")
      player.penup()
      player.speed(0)
      speed = 0
      #Skapar boll
      boll = turtle.Turtle()
      boll.color("yellow")
      boll.shape("circle")
      boll.penup()
      boll.speed(0)
      boll.shapesize(1,1,1)
      boll.goto(250,200)
      boll.left(30)
      def turnleft():
      player.left(30)
      def turnright():
      player.right(30)
      def increasespeed():
      global speed
      speed += 5
      def decreasespeed():
      global speed
      speed -= 5
      turtle.listen()
      turtle.onkey(turnleft, "a")
      turtle.onkey(increasespeed, "w")
      turtle.onkey(turnright, "d")
      turtle.onkey(decreasespeed, "s")
      while True:
      boll.forward(7)
      player.forward(speed)
      if speed > 5:
      speed = 5
      if speed < -5:
      speed = -5
      if player.xcor() > int(300) or player.xcor() < int(-300):
      player.right(180)
      if player.ycor() > int(300) or player.ycor() < int(-300):
      player.right(180)
      d = math.sqrt(math.pow(player.xcor()-boll.xcor(),2) + math.pow(player.ycor()-boll.ycor(),2))
      if d < 17:
      break
      if boll.xcor() > int(300) or boll.xcor() < int(-300):
      boll.left(80)
      if boll.ycor() > int(300) or boll.ycor() < int(-300):
      boll.left(80)

    • @TokyoEdTech
      @TokyoEdTech  3 ปีที่แล้ว

      @@stigidystacks3747 Try this - I added two lines which should make it much faster:
      import turtle
      import math
      wn = turtle.Screen()
      wn.bgcolor("lightgreen")
      wn.title("Bouncing Balls")
      wn.tracer(0)
      #Ritar banan
      mypen = turtle.Turtle()
      mypen.penup()
      mypen.setposition(-300,-300)
      mypen.pendown()
      mypen.pensize(3)
      mypen.speed(10)
      for side in range(4):
      mypen.forward(600)
      mypen.left(90)
      mypen.hideturtle()
      #Skapar spelaren
      player = turtle.Turtle()
      player.color("blue")
      player.shape("turtle")
      player.penup()
      player.speed(0)
      speed = 0
      #Skapar boll
      boll = turtle.Turtle()
      boll.color("yellow")
      boll.shape("circle")
      boll.penup()
      boll.speed(0)
      boll.shapesize(1,1,1)
      boll.goto(250,200)
      boll.left(30)
      def turnleft():
      player.left(30)
      def turnright():
      player.right(30)
      def increasespeed():
      global speed
      speed += 5
      def decreasespeed():
      global speed
      speed -= 5
      turtle.listen()
      turtle.onkey(turnleft, "a")
      turtle.onkey(increasespeed, "w")
      turtle.onkey(turnright, "d")
      turtle.onkey(decreasespeed, "s")
      while True:
      wn.update()
      boll.forward(7)
      player.forward(speed)
      if speed > 5:
      speed = 5
      if speed < -5:
      speed = -5
      if player.xcor() > int(300) or player.xcor() < int(-300):
      player.right(180)
      if player.ycor() > int(300) or player.ycor() < int(-300):
      player.right(180)
      d = math.sqrt(math.pow(player.xcor()-boll.xcor(),2) + math.pow(player.ycor()-boll.ycor(),2))
      if d < 17:
      break
      if boll.xcor() > int(300) or boll.xcor() < int(-300):
      boll.left(80)
      if boll.ycor() > int(300) or boll.ycor() < int(-300):
      boll.left(80)

    • @stigidystacks3747
      @stigidystacks3747 3 ปีที่แล้ว

      @@TokyoEdTech Wow what a difference!!! Thank you so much 🙏

  • @subrinakhan3573
    @subrinakhan3573 6 ปีที่แล้ว

    why its always turtle.Turtle() to make a new turtle? why the capital T turtle?

  • @badnamvines520
    @badnamvines520 7 ปีที่แล้ว +5

    pls make this code work in python 3.6.1 pls pls pls

    • @CheerupButtercup
      @CheerupButtercup 7 ปีที่แล้ว

      i have done it Python 3.6 and its fine :)

  • @アリちゃんねる-v5l
    @アリちゃんねる-v5l 4 ปีที่แล้ว

    Thank you for making an AWSOME tutorial T-T this is the best tutorial i have ever seen T-T
    question, can you make it so it can turn with A/D and Left/Right key as well?
    *maybe you don't even understand wth im saying ;-;

    • @TokyoEdTech
      @TokyoEdTech  4 ปีที่แล้ว

      Thanks - glad you liked it. To use a and d, just copy the lines with Left/Right and change Left/Right to a/d.

    • @アリちゃんねる-v5l
      @アリちゃんねる-v5l 4 ปีที่แล้ว

      @@TokyoEdTech I meant like you can use both, left/right and a/d, do you just copy the code for left and right?
      thanks for answering my question👍this was like uploaded 4 years ago xD

    • @TokyoEdTech
      @TokyoEdTech  4 ปีที่แล้ว

      @@アリちゃんねる-v5l Yep - you can use both. Just copy the left/right code and then change it to a/d. You're welcome. Keep on codin'!

    • @アリちゃんねる-v5l
      @アリちゃんねる-v5l 4 ปีที่แล้ว

      Tysm @@TokyoEdTech
      edit: yay it works =)
      I love coding, like my sibling, I started when i was like, 7 xD and entered a competition at...like 9... and won second place 👍Thanks

  • @connerrapp1785
    @connerrapp1785 6 ปีที่แล้ว

    christian Thompson, can you please help me with something!?

  • @yesveryprofesionalnameyes6055
    @yesveryprofesionalnameyes6055 6 ปีที่แล้ว +1

    @Christian Thompson
    i am working on a project, i used border boundary checking from this video... why is this code not working??
    code:
    # TURTLE GAME TEST LOL
    import turtle, random, math
    # player turtle stuff
    t = turtle.Turtle()
    t.color("white")
    t.shape("square")
    t.up()
    t.goto(0, -280)
    # Screen stuff
    sc = turtle.Screen()
    sc.bgcolor("black")
    sc.setup(1000, 680)
    sc.title("Simple Game v.1.1")
    # Border
    b = turtle.Turtle()
    b.up()
    b.goto(-450, -300)
    b.color("red")
    b.down()
    b.pensize(7)
    b.fd(900)
    print(b.xcor())
    b.lt(90)
    b.fd(625)
    b.lt(90)
    b.fd(900)
    b.lt(90)
    b.fd(625)
    b.hideturtle()
    # Functions
    def rt():
    t.fd(50)
    def lt():
    t.bk(50)
    # Making functions "rt" and "lt" auctally move you araound
    sc.onkey(rt, "Right")
    sc.onkey(lt, "Left")
    sc.listen()
    turtle.done()
    # Checking if turtle is touching border
    while True:
    if t.xcor() > 450 or t.xcor() < -450:
    t.bk(50)
    just for you to know i want to be able to move right and left only not up and down....

  • @josefrancisco471
    @josefrancisco471 7 ปีที่แล้ว

    fixed, sorry

  • @koolkd3724
    @koolkd3724 4 ปีที่แล้ว

    here is my code