JavaScript Quiz for Beginners
24:53
Simple Java Game Library (Intro)
20:38
Intro to Java and AP CS A - Arrays
17:07
AP CS A Teacher Training - Arrays
12:00
Пікірлер
@Chessguy6969
@Chessguy6969 Күн бұрын
When i run my project the snake is already increasing size and my code is: import turtle import time import random delay = 0.1 # set up the screen wn =turtle.Screen() wn.title("Snake game by Aditya Footballer") wn.bgcolor("Blue") wn.setup(width=600, height=600) wn.tracer(0) # Tru to turn off the screen updates # Snake Head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("Black") head.penup() head.goto(0,0) head.direction = "stop" # snake food food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100) segments = [] # Functions def go_up(): head.direction = "up" def go_down(): head.direction = "down" def go_left(): head.direction = "left" def go_right(): head.direction = "right" def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20) if head.direction == "down": y = head.ycor() head.sety(y - 20) if head.direction == "left": x = head.xcor() head.setx(x - 20) if head.direction == "right": x = head.xcor() head.setx(x + 20) # keyboard bindings wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") # Main game loop while True: wn.update() # Check for a collision with the food if head.distance(food) < 20: # Move the food to a random spot x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x,y) # Add a segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment) # Move the end segments first in reverse order for index in range(len(segments)-1,0,-1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x, y) # Move segment 1 to where the head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() time.sleep(delay) wn.mainloop()
@Manga_Lov4
@Manga_Lov4 2 күн бұрын
When my Snake collabs with an apple only the segment 0 is coming to the snake, the others are spawning in the middle My entire Code: import turtle import time import random delay = 0.1 #set up the screen wn = turtle.Screen() wn.title("Snake Game") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) # Turns off the screen updates #Snake head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0,0) head.direction = "stop" # Snake food food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100) segments = [] #Funktions def go_up(): head.direction = "up" def go_down(): head.direction = "down" def go_left(): head.direction = "left" def go_right(): head.direction = "right" def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20) if head.direction == "down": y = head.ycor() head.sety(y - 20) if head.direction == "left": x = head.xcor() head.setx(x - 20) if head.direction == "right": x = head.xcor() head.setx(x + 20) # Keyboard bindings wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") #Main game loop while True: wn.update() # Check for a collision with the food if head.distance(food) < 20: # move the food to a random spot x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x, y) # Add a segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment) # Move the end segmentsfirst in reversed order for index in range(len(segments)-1, 0, -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index-1].goto(x, y) # Move segment 0 to where the head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() time.sleep(delay) wn.mainloop()
@jadeabove
@jadeabove 2 күн бұрын
I squealed a bit too loud when I got the "Player" to move. :p
@TokyoEdTech
@TokyoEdTech 2 күн бұрын
@@jadeabove It's a great feeling! Keep on codin'!
@dudewhatthe2632
@dudewhatthe2632 3 күн бұрын
my snake not moving
@dudewhatthe2632
@dudewhatthe2632 3 күн бұрын
i copied code here it is import turtle import time import random delay = 0.1 #Score score = 0 high_score= 0 # set screen wn = turtle.Screen() wn.title("Snake Game by @Valentino") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) #Snake Head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0,0) head.direction = "stop" #Snake Food food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100) segments = [] #Pen pen= turtle.Turtle() pen.speed(0) pen.shape("square") pen.color("white") pen.penup() pen.hideturtle() pen.goto(0, 260) pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal")) #Functions def go_up(): if head.direction != "down": head.direction = "up" def go_down(): if head.direction != "up": head.direction = "down" def go_left(): if head.direction != "right": head.direction = "left" def go_right(): if head.direction != "left": head.direction = "right" def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20) if head.direction == "down": y = head.ycor() head.sety(y - 20) if head.direction == "left": x = head.xcor() head.setx(x - 20) if head.direction == "right": x = head.xcor() head.setx(x + 20) #Keybord Binds wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") #Main Game Loop while True: wn.update() if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290: time.sleep(1) head.goto(0,0) head.direction = "stop" #Hide the segments for segment in segments: segment.goto(1000, 1000) #Clear the segments list segments.clear() #Reset score score = 0 delay = 0.1 pen.clear() pen.write("Score: {} High_Score: {}". format(score, high_score), align="center", font=("Courier", 24, "normal")) # Check for collision with food if head.distance(food) < 20: #Move the food to a random spot x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x,y) # Add a segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment) delay -= 0.001 #Increase Score score += 10 if score > high_score: high_score = score pen.clear() pen.write("Score: {} High_Score: {}". format(score, high_score), align="center", font=("Courier", 24, "normal")) #Move end segments first in reverse order for index in range(len(segments)-1, 0, -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x, y) # Move segment 0 to where the head is if len(segments) > 0: x = head.xcor() y = head.ycor() wn.mainloop()
@dudewhatthe2632
@dudewhatthe2632 3 күн бұрын
don't know what i did wrong pls help me
@dudewhatthe2632
@dudewhatthe2632 3 күн бұрын
luv your vids
@TokyoEdTech
@TokyoEdTech 3 күн бұрын
@@dudewhatthe2632 Hiya - first you are missing the following: move() time.sleep(delay) Plus, there is something else wrong, but I couldn't quite figure it out. Here is a link to the correct code: gist.github.com/wynand1004/ec105fd2f457b10d971c09586ec44900 Note, the order is pretty important for this one. Keep on codin'!
@valhurts
@valhurts 4 күн бұрын
That's crazy this vid is 8 years rn
@TokyoEdTech
@TokyoEdTech 4 күн бұрын
Wow - time flies!
@The-MaliX
@The-MaliX 4 күн бұрын
help me I type pen.hideturtle() but i get Exception has occurred: TypeError TPen.hideturtle() missing 1 required positional argument: 'self'
@TokyoEdTech
@TokyoEdTech 4 күн бұрын
@@The-MaliX Can you copy and paste all of your code so I can take a look?
@The-MaliX
@The-MaliX 4 күн бұрын
@@TokyoEdTech import turtle wn = turtle.Screen() wn.title("Cookie Clicker") wn.bgcolor("black") wn.register_shape("Cookie.gif") cookie = turtle.Turtle() cookie.shape("Cookie.gif") cookie.speed(0) clicks = 0 pen = turtle.Turtle pen.hideturtle() pen.color("white") pen.penup() pen.goto(0, 400) pen.write(f"Clicks: {clicks}", align="center", font=("Courier new", 32, "normal")) def clicked(x, y): global clicks clicks += 1 pen.clear() pen.write(f"Clicks: {clicks}", align="center", font=("Courier new", 32, "normal")) cookie.onclick(clicked) wn.mainloop()
@alomchakma605
@alomchakma605 5 күн бұрын
My snake is not moving
@TokyoEdTech
@TokyoEdTech 4 күн бұрын
@@alomchakma605 If you copy and paste your code, I can take a look
@SohamKapdi-j1d
@SohamKapdi-j1d 6 күн бұрын
When I tried to add this code to my space war code my turtle was not responding after just adding the splash screen so what should I do
@TokyoEdTech
@TokyoEdTech 5 күн бұрын
@@SohamKapdi-j1d If you share all the code I can take a look.
@SohamKapdi-j1d
@SohamKapdi-j1d 3 күн бұрын
@@TokyoEdTech what should i am not able send the link to the code
@TokyoEdTech
@TokyoEdTech 3 күн бұрын
@@SohamKapdi-j1d You can copy and paste it here
@SohamKapdi-j1d
@SohamKapdi-j1d 2 күн бұрын
@@TokyoEdTech i tried that to it was giving me error i tried it many times so if have any other way to send the code to you can you tell me please
@Bubble_Kayl36
@Bubble_Kayl36 6 күн бұрын
it appears but wont follow (do you need the whole code or is this enough?) #add segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment) #move end segments first in reverse order for index in range(len(segments)-1 , 0 , -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x , y) #move segment 0 to where head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x , y) move() time.sleep(delay) wn.mainloop()
@TokyoEdTech
@TokyoEdTech 6 күн бұрын
@Bubble_Kayl36 Hi. Yes, could you provide all the code so I can test it?
@Bubble_Kayl36
@Bubble_Kayl36 2 күн бұрын
@@TokyoEdTech #snake import turtle import time import random delay = 0.1 #set up screen wn = turtle.Screen() wn.title("Snake Game by Bubble") wn.bgcolor("green") wn.setup(width=600 , height=600) wn.tracer(0) #turns off screen updates #snake head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0,0) head.direction = "stop" #food food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100) #body segments = [] #functions def go_up(): head.direction = "up" def go_down(): head.direction = "down" def go_left(): head.direction = "left" def go_right(): head.direction = "right" def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20) if head.direction == "down": y = head.ycor() head.sety(y - 20) if head.direction == "left": x = head.xcor() head.setx(x - 20) if head.direction == "right": x = head.xcor() head.setx(x + 20) #key binding wn.listen() wn.onkeypress(go_up , "w") wn.onkeypress(go_down , "s") wn.onkeypress(go_left , "a") wn.onkeypress(go_right , "d") #main game loop while True: wn.update() #check for collision with food if head.distance(food) < 20: #move food to random spot X = random.randint(-290,290) Y = random.randint(-290,290) food.goto(X , Y) #add segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment) #move end segments first in reverse order for index in range(len(segments)-1 , 0 , -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x,y) #move segment 0 to where head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() time.sleep(delay) wn.mainloop() sorry for the wait, youtube didn't give me a notification
@LarryTheWeirdone
@LarryTheWeirdone 7 күн бұрын
So every time I type in while True: the colon is there but when I run it opens terminal which is normal but it says expected : when it’s there?
@TokyoEdTech
@TokyoEdTech 7 күн бұрын
@@LarryTheWeirdone Can you copy and paste the code and the error message?
@LarryTheWeirdone
@LarryTheWeirdone 4 күн бұрын
@TokyoEdTech hey sorry I found out what the error was. I thought there was only one Colon but there was 2. And I forget which line it was since I already deleted that project
@TokyoEdTech
@TokyoEdTech 4 күн бұрын
@LarryTheWeirdone All good - glad you got it sorted!
@COOLGAMING416
@COOLGAMING416 7 күн бұрын
Sir can u pls provide source code of this
@TokyoEdTech
@TokyoEdTech 7 күн бұрын
@COOLGAMING416 Sure - here you go: github.com/wynand1004/Projects/tree/master/SpaceWar
@COOLGAMING416
@COOLGAMING416 7 күн бұрын
Thnx a lot sir
@SohamKapdi-j1d
@SohamKapdi-j1d 9 күн бұрын
when trying to use more things such as lives so what happens when i add both on the screen the game is not able to undo the 0 in the score and 3 in the lives and it writes over the old number so what should i do not face that problem
@TokyoEdTech
@TokyoEdTech 7 күн бұрын
Use .clear on the pen turtle. Then redraw all the text.
@SohamKapdi-j1d
@SohamKapdi-j1d 7 күн бұрын
@@TokyoEdTech thanks 😊😊
@MelloTheHero
@MelloTheHero 10 күн бұрын
turn.listen() doesn't work?
@TokyoEdTech
@TokyoEdTech 9 күн бұрын
Hi. There is no turn.listen() - it should probably be turtle.listen()
@saturn5tony
@saturn5tony 10 күн бұрын
An fyi, the little indian format is the way the 6502 deals with a 16 bit number. Thanks for sharing !
@TokyoEdTech
@TokyoEdTech 10 күн бұрын
@@saturn5tony Cheers - thanks for the info!
@ziaulhaiderchowdhury9363
@ziaulhaiderchowdhury9363 11 күн бұрын
Thanks a lot . That was a good lesson for the beginners . My high score is 400.
@TokyoEdTech
@TokyoEdTech 11 күн бұрын
You're welcome. 400 points is impressive! Keep on codin' and please subscribe!
@xyzxyz6095
@xyzxyz6095 16 күн бұрын
Bună ziua. Mulțumesc pentru lecția de Pyth0n. Salutări din Franța.
@konoyaro09
@konoyaro09 17 күн бұрын
1:33 how did you get that list,in my python there is no list by default please help
@TokyoEdTech
@TokyoEdTech 17 күн бұрын
@@konoyaro09 It depends on which editor or IDE you are using. Check out Geany or VS Code.
@konoyaro09
@konoyaro09 17 күн бұрын
1:32 in my python i dont get that list,how can i fix that
@himankgaming7555
@himankgaming7555 18 күн бұрын
My actual interest for general programming and logic building started from here and today after working as a software developer, I just came back here to appreciate the efforts that has changed one life, Thank you sir for this great tutorial playlist
@TokyoEdTech
@TokyoEdTech 18 күн бұрын
@@himankgaming7555 That is amazing! Thank you for letting me know. Congratulations on your new job! Keep on codin'!
@thelegith3619
@thelegith3619 19 күн бұрын
sir what IDE are you using beacuse im a beginner and i have jupyter notebook and it isn"t working on it
@TokyoEdTech
@TokyoEdTech 19 күн бұрын
This won't work in Jupyter Notebook. These days I use Geany - this video will help you get started: kzbin.info/www/bejne/j6aQZ4V6eLRleLs
@Balamayooran
@Balamayooran 20 күн бұрын
Hi sir, can we do this optimization through from scipy.optimize import linprog
@TokyoEdTech
@TokyoEdTech 20 күн бұрын
@@Balamayooran I believe so - might want to try it and report back!
@Balamayooran
@Balamayooran 19 күн бұрын
@@TokyoEdTech , Hi sir, thanks, when it comes to area, then this will be non-linear type, so I think from scipy.optimiza import linprog is not suited, your one is correct
@TokyoEdTech
@TokyoEdTech 19 күн бұрын
@Balamayooran Thanks for the update!
@erioluwaalvinolorundare9726
@erioluwaalvinolorundare9726 22 күн бұрын
NameError: name 'random' is not defined. Did you forget to import 'random'? this is what it says the error
@TokyoEdTech
@TokyoEdTech 18 күн бұрын
So, it tells you the error, and it tells you exactly how to fix it. At the top of the program where you have import turtle, add import random
@erioluwaalvinolorundare9726
@erioluwaalvinolorundare9726 22 күн бұрын
and also if i move the snake and it hit the red ball it will stop automatically and say error
@erioluwaalvinolorundare9726
@erioluwaalvinolorundare9726 22 күн бұрын
if i press a or d for the snake it wont move
@TokyoEdTech
@TokyoEdTech 22 күн бұрын
@@erioluwaalvinolorundare9726 If you share your code I will take a look.
@erioluwaalvinolorundare9726
@erioluwaalvinolorundare9726 22 күн бұрын
can you help me out
@erioluwaalvinolorundare9726
@erioluwaalvinolorundare9726 22 күн бұрын
hi tokyoedthech
@NamVuongLe
@NamVuongLe 22 күн бұрын
Hi
@monkeyedits9694
@monkeyedits9694 24 күн бұрын
I got a problem my sprite doesnt move anymore, import os import random #Import the Turtle module import turtle #Required by MacOSX to show the window turtle.fd(0) #Set the animations speed to the maximum turtle.speed(0) #Change the background color turtle.bgcolor("black") #Hide the default turtle turtle.ht() #This saves memory turtle.setundobuffer(1) #This speeds up drawing turtle.tracer(1) class Sprite(turtle.Turtle): def __init__(self, spriteshape, color, startx, starty): turtle.Turtle.__init__(self, shape = spriteshape) self.speed(0) self.penup() self.color(color) self.fd(0) self.goto(startx, starty) self.speed = 1 def move(self): self.fd(self.speed) class Player(Sprite): def __init__(self, spriteshape, color, startx, starty): Sprite.__init__(self, spriteshape, color, startx, starty) self.speed = 4 self.lives = 3 def turn_left(self): self.lt(45) def turn_right(self): self.lt(45) def accelerate(self): self.speed += 1 def accelerate (self): self.speed -=1 #Create my sprites player = Player("triangle", "white", 0, 0) #Keyboard bindings turtle.onkey(player.turn_left, "Left") turtle.listen() turtle.onkey(player.turn_right, "Right") turtle.listen() turtle.onkey(player.accelerate_up, "Up") turtle.listen() turtle.onkey(player.decelerate, "Down") turtle.listen() #Main game loop while True: player.move() delay = raw_input("Press enter to finish. > ")
@TokyoEdTech
@TokyoEdTech 24 күн бұрын
@@monkeyedits9694 Are you getting any error message?
@patientson
@patientson 25 күн бұрын
This is an absolute requirement for data analyst and cyber security
@TokyoEdTech
@TokyoEdTech 25 күн бұрын
@@patientson Glad you think so!
@user-vp2lg9gu5l
@user-vp2lg9gu5l 26 күн бұрын
I love your videos; I first followed them in 2019 when I got into python but then life got in the way and I hadn't coded in 4 years so i spent hours trying to figure out which youtuber it was who taught me the first time and then I found your Pong playlist finally! I got right back into it with your guidence! I love how you don’t just show us what to do but explain why everything is done and thus allow us to alter the code we write to differ from yours to personalise it for instance with this game I decided when the snake hits the border it should just come back from the other side of the screen and so I tried that, and it may not be perfect coding but I was able to make it work from what you have taught! I have also added sounds and a “GAME OVER” screen , as well as making the high score save between games (code is below for anyone who wants to do the same and can’t figure it out! - you will need to pick some sounds you like I get mine from freesound.org/ and adding sounds is explained here: kzbin.info/www/bejne/j4vOn6KFjKiUoJY&ab_channel=TokyoEdtech for help on how the high score saving works I used this vid: kzbin.info/www/bejne/fXSYYZebrbamiNE&ab_channel=TokyoEdtech) your great explanations also mean that when I make a typo or differ from your code slightly to personalise it and it doesn’t work I'm able to pause your video and try and figure it out myself since you have not just shown but taught, for instance with this code everything worked except after you lose the score would reset to 0 and stay at 0, it took me 10 mins to realise that I had missed an indent on lines 149 and 150 but I got there eventually! Code: import turtle import winsound import time import random import pickle delay = 0.1 # Score score = 0 high_score = 0 high_score = pickle.load(open("score.dat", "rb")) # this reads the high score file and makes the high score save - if you are copying this code remove this and run once without this line then reinsert it # Screen setup wn = turtle.Screen() wn.title("Snake by Lewis") wn.bgcolor("light green") wn.setup(width=600, height=600) wn.tracer(0) # Turns off screen updates to increase running speed # Snake head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("dark green") head.penup() head.goto(0,0) head.direction = "stop" # Mouse (food) food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0,100) # Body segments segments = [] # Score board pen = turtle.Turtle() pen.speed(0) pen.shape("square") pen.color("black") pen.penup() pen.hideturtle() pen.goto(0, 260) pen.write(f"Score: {score} High Score: {high_score}", align="center", font=("Courier", 18, "normal")) # "GAME OVER" message gameover_pen = turtle.Turtle() gameover_pen.speed(0) gameover_pen.shape("square") gameover_pen.color("red") gameover_pen.penup() gameover_pen.hideturtle() gameover_pen.goto(0, 0) # Functions def go_up(): if head.direction != "down": head.direction = "up" def go_down(): if head.direction != "up": head.direction = "down" def go_left(): if head.direction != "right": head.direction = "left" def go_right(): if head.direction != "left": head.direction = "right" def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20) gameover_pen.clear() # if you just put this after the gameover message it will instantly vanish even if you use a time delay for some reason, so my solution was to get rid of the message when you click a key to move to start the next game if head.direction == "down": y = head.ycor() head.sety(y - 20) gameover_pen.clear() if head.direction == "left": x = head.xcor() head.setx(x - 20) gameover_pen.clear() if head.direction == "right": x = head.xcor() head.setx(x + 20) gameover_pen.clear() # Key bindings wn.listen() wn.onkeypress(go_up,"w") wn.onkeypress(go_down,"s") wn.onkeypress(go_left,"a") wn.onkeypress(go_right,"d") # Main Game loop while True: wn.update() # Collisions with border if head.xcor() > 290: y = head.ycor() head.goto(-290, y) if head.xcor() < -290: y = head.ycor() head.goto(290, y) if head.ycor() > 290: x = head.xcor() head.goto(x, -290) if head.ycor() < -290: x = head.xcor() head.goto(x, 290) # Collision with food if head.distance(food) < 20: winsound.PlaySound("foodsound.wav", winsound.SND_ASYNC) x = random.randint(-290,290) y = random.randint(-290,290) food.goto(x,y) # Add a segment new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("green") new_segment.penup() segments.append(new_segment) # Shorten delay delay -= 0.001 # Increase score score += 1 if score > high_score: high_score = score pen.clear() pen.write(f"Score: {score} High Score: {high_score}", align="center", font=("Courier", 18, "normal")) # Move end segments first in reverse order for index in range(len(segments)-1, 0, -1): x = segments[index-1].xcor() y = segments[index-1].ycor() segments[index].goto(x, y) # Move segment 0 (closest to head) to where head is if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x,y) move() # Check for head collision with body segments for segment in segments: if segment.distance(head) < 20: winsound.PlaySound("gameover.wav", winsound.SND_ASYNC) gameover_pen.clear() # idk why this changes things but putting this anywhere else causes the snake to imedietly move left upon respawn regardless of the head.direction = "stop" code head.goto(0, 0) head.direction = "stop" # Hide the segments for segment in segments: segment.goto(1000, 1000) # Clear segments list segments.clear() # reset score, delay, and sound delay = 0.1 score = 0 # update score display + GAMEOVER pen.clear() pen.write(f"Score: {score} High Score: {high_score}", align="center", font=("Courier", 18, "normal")) gameover_pen.clear() # gameover code has to be here or it just doesnt show up unsure why??? gameover_pen.write("GAME OVER!", align="center", font=("Courier", 32, "bold")) time.sleep(2.527) # this is how long the gameover sound lasts # Saves the high score as a file pickle.dump(high_score, open("score.dat", "wb")) time.sleep(delay) wn.mainloop()
@ChristinaGoddeti
@ChristinaGoddeti 27 күн бұрын
Can we run this project in pycharm
@TokyoEdTech
@TokyoEdTech 27 күн бұрын
@@ChristinaGoddeti Yep!
@HaliamhJaved
@HaliamhJaved 28 күн бұрын
7:06 my code is the exact same as yours except for colors but it isn’t moving at all
@TokyoEdTech
@TokyoEdTech 28 күн бұрын
@@HaliamhJaved If you share your code I'll take a look.
@harshitasahu178
@harshitasahu178 Ай бұрын
Hii sir My snake is not moving and it doesn't give any error message I use Python 3.8.6
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@harshitasahu178 Can you paste the code here so I can take a look?
@rayaneachchaq6302
@rayaneachchaq6302 Ай бұрын
thank u sooo much man appreciate the efforts
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@rayaneachchaq6302 You are welcome! Keep on codin' and please subscribe!
@pythonBoss
@pythonBoss Ай бұрын
bro you are great super bro can you post more videos like this
@TokyoEdTech
@TokyoEdTech 29 күн бұрын
Thanks! I have a number of game tutorials - be sure to check them out! Keep on codin' and please subscribe!
@Ripblank87
@Ripblank87 Ай бұрын
#import the module import os import random import turtle turtle.fd(0) turtle.speed(0) turtle.bgcolor("black") turtle.ht() turtle.setundobuffer(1) class Sprite(turtle.Turtle): def __init__(self, spiriteshape, color, startx, starty): turtle.Turtle.__init__(self, shape = spiriteshape) self.speed(0) self.penup() self.color(color) self.fd(0) self.goto(startx, starty) self.speed = 1 def move(self): self.fd(self.speed) class Player(Sprite): def __init__(self, spiriteshape, color, startx, starty): Sprite.__init__(self, shape = spiriteshape) def turn_left(self): self.lf(45) #Sprites player = Sprite("triangle", "white", 0, 0) #keyboard bindings #main game loop while True: player.move() in this code thats close to yours i couldn't use turn.left key binds or any key binds
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@Ripblank87 You are missing the code that does the keybinding. What did you actually type?
@Ripblank87
@Ripblank87 Ай бұрын
@TokyoEdTech uh i just copied what you did in the second video also i removed self.speed = 4 and self.lives = 3 Because they were making errors
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@Ripblank87 As I mentioned you don't have the keybinding code - go back, watch the video, and make sure you don't miss that part.
@Ripblank87
@Ripblank87 Ай бұрын
Alright thank you
@eclipse_cta
@eclipse_cta Ай бұрын
Thanks a lot! this tutorial is very easy to follow! would reccomend this a lot. also thank you so much for giving explanations as to (example) why we use penup, etc. :-)
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@eclipse_cta You're quite welcome. Keep on codin' and please subscribe!
@VanditaTiwari_28
@VanditaTiwari_28 Ай бұрын
Hey my code is not running
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@VanditaTiwari_28 If you share your code and any error messages I can take a look.
@Ripblank87
@Ripblank87 Ай бұрын
How to get self to work
@TokyoEdTech
@TokyoEdTech Ай бұрын
I'm not sure what you mean - can you share the code and any error messages?
@Ripblank87
@Ripblank87 Ай бұрын
@TokyoEdTech it says self is not defined
@Ripblank87
@Ripblank87 Ай бұрын
@TokyoEdTech by the way you are a legend
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@Ripblank87 Please share the code - copy and paste it here.
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@Ripblank87 Thanks - I try!
@outlander6643
@outlander6643 Ай бұрын
can i use visual studio code to do this ? if so what extension do i need to run the calculator to open as a window?
@TokyoEdTech
@TokyoEdTech 29 күн бұрын
Hiya - sure, it should be no problem. You don't need any extensions as tkinter usually comes with Python.
@Stories-m6u
@Stories-m6u Ай бұрын
1:12 not Pythagoras theorem again ahhhhh
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@Stories-m6u It pops up when you least expect it!
@afcnft-12
@afcnft-12 Ай бұрын
Your videos help me on tests !
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@afcnft-12 Awesome - glad I could help. Keep on codin' and please subscribe!
@mohammadahmadzee5060
@mohammadahmadzee5060 Ай бұрын
can we make the snake realistic
@TokyoEdTech
@TokyoEdTech Ай бұрын
You can use images for the head and body, but that complicates things a bit.
@xoxo.lxly.26
@xoxo.lxly.26 Ай бұрын
hey how did you import the sound? on mac how did u save it on folder and all plesase explain me.
@TokyoEdTech
@TokyoEdTech 29 күн бұрын
Usually you do "Save as..." and then choose the folder. Alternatively, once it is in the downloads, copy and paste it into the folder.
@rickeymelson1204
@rickeymelson1204 Ай бұрын
hello sir. i'm getting the AttributeError 'Turtle' object has no attribute 'size. import os import random #Import the Turtle module import turtle turtle.fd(0) #set the animation speed turtle.speed(0) #change the background turtle.bgcolor("black") #hide the default turtle turtle.ht() #this saves memory turtle.setundobuffer(1) #this speeds up the drawing turtle.tracer(1) class Sprite(turtle.Turtle): def __init__(self, spriteshape, color, startx, starty): turtle.Turtle.__init__(self, shape = spriteshape) self.speed(0) self.penup() self.color(color) self.fd(0) self.goto(startx, starty) self.speed = 4 def move(self): self.fd(self.speed) #Border detection if self.xcor() > 290: self.setx(290) self.rt(60) if self.xcor() < -290: self.setx(290) self.rt(60) if self.ycor() > 290: self.sety(290) self.rt(60) if self.ycor() < - 290: set.sety(290) self.rt(60) class Player(Sprite): def __init__(self, spriteshape, color, startx, starty): Sprite.__init__(self, spriteshape, color, startx, starty) self_speed = 4 self_lives = 3 def turn_left(self): self.lt(45) def turn_right(self): self.rt(45) def accelerate(self): self.speed += 1 def decelerate(self): self.speed -= 1 class Game(): def __init__(self): self.level = 1 self.score = 0 self.state = "playing" self.pen = turtle.Turtle() self.lives = 3 def draw_border(self): #Draw Border self.pen.speed(0) self.pen.color("white") self.pen.size(3) self.pen.penup() self.pen.goto(-300, 300) self.pen.pendown() for side in range(4): self.pen.fd(600) self.pen.rt(60) self.pen.penup() self.pen.ht() #create game object game = Game() #draw the game border game.draw_border() #create my sprites player = Sprite("triangle", "white", 0, 0) #keyboard binding turtle.onkey(player.turn_left, "Left") turtle.onkey(player.turn_right, "Right") turtle.onkey(player.accelerate, "Up") turtle.onkey(player.decelerate, "Down") turtle.listen() #Main game Loop while True: player.move() delay = input("press enter to finish. > ")
@TokyoEdTech
@TokyoEdTech Ай бұрын
Hiya. Try self.pen.pensize(3)
@houdatahiri4596
@houdatahiri4596 Ай бұрын
hello every one i dont know why the green screen dont show up in my screen and i have a projet presantation tomorrow please help me guys
@houdatahiri4596
@houdatahiri4596 Ай бұрын
thats my error guys > & c:/Users/Lenovo/Desktop/exercice/.venv/Scripts/python.exe c:/Users/Lenovo/xDesktop/exercice/preparatin.py/snak.py x Traceback (most recent call last): File "c:\Users\Lenovo\Desktop\exercice\preparatin.py\snak.py", line 3, in <module> wn = turtle.screen() ^^^^^^^^^^^^^ AttributeError: module 'turtle' has no attribute 'screen'. Did you mean: 'Screen'? PS C:\Users\Lenovo\Desktop\exercice>
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@houdatahiri4596 The error message tells you exactly what the problem is and how to fix it. screen -> Screen
@xoxo.lxly.26
@xoxo.lxly.26 Ай бұрын
Umm sorry for troubling u again but when i shoot the missile it passes straight thorough the enemy here is my code: ___________________CODE___________________ import os import random #Import the Turtle module import turtle #Required by MacOSX to show the window turtle.fd(0) #Set the animation speed to the maxiumum turtle.speed(0) #Change the background color turtle.bgcolor("black") #Hide the default turtle turtle.ht() #This saves memory turtle.setundobuffer(1) #This speeds up drawing turtle.tracer(1) class Sprite(turtle.Turtle): def __init__(self, spriteshape, color, startx, starty): turtle.Turtle.__init__(self, shape = spriteshape) self.speed(0) self.penup() self.color(color) self.fd(0) self.goto(startx, starty) self.speed = 1 def move(self): self.fd(self.speed) #Bounary detection if self.xcor() > 290: self.setx(290) self.rt(60) if self.ycor() < -290: self.setx(-290) self.rt(60) if self.ycor() > 290: self.sety(290) self.rt(60) if self.xcor() < -290: self.sety(-290) self.rt(60) def is_collision(self, other): if (self.xcor() >= (other.xcor() - 20)) and \ (self.xcor() <= (other.xcor() + 20)) and \ (self.ycor() >= (other.ycor() - 20)) and \ (self.ycor() <= (other.ycor() + 20)): return True else: return False class Player(Sprite): def __init__(self, spriteshape, color, startx, starty): Sprite.__init__(self, spriteshape, color, startx, starty) self.speed = 4 self.lives = 3 def turn_left(self): self.lt(45) def turn_right(self): self.rt(45) def accelerate(self): self.speed += 1 def decelerate(self): self.speed -= 1 class Enemy(Sprite): def __init__(self, spriteshape, color, startx, starty): Sprite.__init__(self, spriteshape, color, startx, starty) self.speed = 6 self.setheading(random.randint(0,360)) class Missile(Sprite): def __init__(self, spriteshape, color, startx, starty): Sprite.__init__(self, spriteshape, color, startx, starty) self.shapesize(stretch_wid=0.3, stretch_len=0.4, outline = None) self.speed = 20 self.status = "ready" self.goto(-1000, 1000) def fire(self): if self.status == "ready": self.goto(player.xcor(),player.ycor()) self.setheading(player.heading()) self.status = "firing" def move(self): if self.status == "ready": self.goto(-1000, 1000) if self.status == "firing": self.fd(self.speed) #Border check if self.xcor() < -290 or self.xcor() > 290 or \ self.ycor() < -290 or self.ycor() > 290: self.goto(-1000,1000) self.status = "ready" class Game(): def __init__(self): self.level = 1 self.score = 0 self.state = "playing" self.pen = turtle.Turtle() self.lives = 3 def draw_border(self): #Draw border self.pen.speed(0) self.pen.color("white") self.pen.pensize(3) self.pen.penup() self.pen.goto(-300, 300) self.pen.pendown() for side in range(4): self.pen.fd(600) self.pen.rt(90) self.pen.penup() self.pen.ht() #Create game object game = Game() #Draw the game border game.draw_border() #Create my sprites player = Player("triangle", "white", 0, 0) enemy = Enemy("circle", "red", -100, 0) missile = Missile("circle", "orange", 0, 0) #Keyboard bindings turtle.onkey(player.turn_left, "Left") turtle.onkey(player.turn_right, "Right") turtle.onkey(player.accelerate, "Up") turtle.onkey(player.decelerate, "Down") turtle.onkey(missile.fire, "space") turtle.listen() #Main game loop while True: player.move() enemy.move() missile.move() #Check for a collision with the player if player.is_collision(enemy): x = random.randint(-250, 250) y = random.randint(-250, 250) enemy.goto(x, y) #Check for a collision between the missile and the enemy if player.is_collision(enemy): x = random.randint(-250, 250) y = random.randint(-250, 250) enemy.goto(x, y) missile.status = "ready" delay = raw_input("Press enter to finish. >") FINISH!
@Cr3cked
@Cr3cked Ай бұрын
TYSM. You helped me explore so many things about turtle. just 1 question, what is the difference between turtle.Turtle() And turtle.Pen()? I've Been Trying To Figure It Out For 3 Years Since I Started Python
@TokyoEdTech
@TokyoEdTech 29 күн бұрын
You're welcome. turtle.Turtle() creates a new turtle object that can be moved around on the screen. There is no turtle.Pen(), but there is turtle.pen() If you mean: pen = turtle.Turtle() then I am creating a new turtle object that I am (in this case) using to print the text on the screen. Keep on codin' and please subscribe!
@Cr3cked
@Cr3cked 29 күн бұрын
@@TokyoEdTech Strange, Since My Turtle Module Has "t = turtle.Pen()"?
@TokyoEdTech
@TokyoEdTech 28 күн бұрын
@@Cr3cked Can you copy and paste your code here so I can take a look? There is a turtle.pen() method but it is lowercase.
@Cr3cked
@Cr3cked 28 күн бұрын
@@TokyoEdTech (This is in pycharm) import turtle t = turtle.Pen() win = turtle.Screen() t.speed(0) def fd(): t.forward(10) def lt(): t.left(90) def rt(): t.right(90) def bk(): t.backward(10) win.listen() win.onkeypress(lt, 'a') win.onkeypress(rt, 'd') win.onkeypress(fd, 'w') win.onkeypress(bk, 's') win.mainloop()
@TokyoEdTech
@TokyoEdTech 28 күн бұрын
@@Cr3cked I had to actually go into the turtle code itself. Pen is an alias of Turtle. This is found on line 3884. Link: github.com/python/cpython/blob/main/Lib/turtle.py#L3884 Mystery solved! It would be nice if this were included in the documentation somewhere.
@Cr3cked
@Cr3cked Ай бұрын
AINT NO WAY YOU ARE FRICKIN CODING A GAME WITH TURTLE. LIKE BRO HOWWWW. I ALWAYS MAKE A CONTROLABBLE ARROW BUT THIS IS NEXT LEVEL
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@Cr3cked Hahahaha! The turtle module is surprisingly capable - check this one out! kzbin.info/aero/PLlEgNdBJEO-kK78GXDVzytiZlJtCyiFyW&si=2nAI_bZJnnVpVXFw
@VivekPatel-zv8hv
@VivekPatel-zv8hv Ай бұрын
have created the same game but still am getting bigger as i move on the screen?
@TokyoEdTech
@TokyoEdTech Ай бұрын
@@VivekPatel-zv8hv Can you share the code so I can take a look?