-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (127 loc) · 3.36 KB
/
main.py
File metadata and controls
138 lines (127 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import turtle
import time
import random
# Screen Setup
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("black")
wn.setup(width=600, height=600)
wn.tracer(0)
# Game Variables
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "stop"
food = turtle.Turtle()
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)
segments = []
player_score = 0
high_score = 0
# Score Display
score_display = turtle.Turtle()
score_display.speed(0)
score_display.color("white")
score_display.penup()
score_display.hideturtle()
score_display.goto(0, 260)
score_display.write("Press P to Play | High Score: 0", align="center", font=("Courier", 18, "bold"))
# Author Display
author = turtle.Turtle()
author.speed(0)
author.color("white")
author.penup()
author.hideturtle()
author.goto(0, -280)
author.write("codebyimran", align="center", font=("Arial", 16, "normal"))
# Movement 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":
head.sety(head.ycor() + 20)
elif head.direction == "down":
head.sety(head.ycor() - 20)
elif head.direction == "left":
head.setx(head.xcor() - 20)
elif head.direction == "right":
head.setx(head.xcor() + 20)
# Reset Game
def reset_game():
global player_score
time.sleep(0.5)
head.goto(0, 0)
head.direction = "stop"
for seg in segments:
seg.goto(1000, 1000)
segments.clear()
player_score = 0
update_score()
def update_score():
score_display.clear()
score_display.write(f"Score: {player_score} | High Score: {high_score}", align="center", font=("Courier", 18, "bold"))
# Game Loop
def game_loop():
global player_score, high_score
wn.update()
# Move segments
for i in range(len(segments) - 1, 0, -1):
segments[i].goto(segments[i - 1].xcor(), segments[i - 1].ycor())
if segments:
segments[0].goto(head.xcor(), head.ycor())
move()
# Border collision
if abs(head.xcor()) > 290 or abs(head.ycor()) > 290:
reset_game()
# Food collision
if head.distance(food) < 20:
x = random.randint(-280, 280)
y = random.randint(-280, 280)
food.goto(x, y)
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("green")
new_segment.penup()
segments.append(new_segment)
player_score += 1
if player_score > high_score:
high_score = player_score
update_score()
# Self collision
for seg in segments:
if seg.distance(head) < 20:
reset_game()
break
wn.ontimer(game_loop, 100)
# Start & Exit
def start_game():
score_display.clear()
update_score()
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
game_loop()
def exit_game():
wn.bye()
# Keyboard Start/Exit
wn.listen()
wn.onkeypress(start_game, "p") # Press P to start
wn.onkeypress(exit_game, "e") # Press E to exit
wn.mainloop()