forked from Sonu2345/HactoberFest23
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turtle game where you can control turtle using arrow keys.
- Loading branch information
1 parent
cfadf02
commit a9292cf
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import turtle | ||
|
||
# Create the game window | ||
wn = turtle.Screen() | ||
wn.title("Turtle Game") | ||
wn.bgcolor("white") | ||
|
||
# Create the player turtle | ||
player = turtle.Turtle() | ||
player.shape("turtle") | ||
player.color("blue") | ||
player.speed(0) | ||
player.penup() | ||
player.goto(0, 0) | ||
player.direction = "stop" | ||
|
||
# Functions to control the player's movement | ||
def move_up(): | ||
player.direction = "up" | ||
|
||
def move_down(): | ||
player.direction = "down" | ||
|
||
def move_left(): | ||
player.direction = "left" | ||
|
||
def move_right(): | ||
player.direction = "right" | ||
|
||
# Keyboard bindings | ||
wn.listen() | ||
wn.onkeypress(move_up, "Up") | ||
wn.onkeypress(move_down, "Down") | ||
wn.onkeypress(move_left, "Left") | ||
wn.onkeypress(move_right, "Right") | ||
|
||
# Main game loop | ||
while True: | ||
wn.update() | ||
|
||
# Move the player | ||
if player.direction == "up": | ||
y = player.ycor() | ||
player.sety(y + 20) | ||
|
||
if player.direction == "down": | ||
y = player.ycor() | ||
player.sety(y - 20) | ||
|
||
if player.direction == "left": | ||
x = player.xcor() | ||
player.setx(x - 20) | ||
|
||
if player.direction == "right": | ||
x = player.xcor() | ||
player.setx(x + 20) | ||
|
||
# Close the game window when done (unreachable in this example) | ||
wn.mainloop() |