-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
140 lines (83 loc) · 2.77 KB
/
main.py
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
139
140
from turtle import Screen #Turtle
import time
from snake import Snake
from food import Food
from scoreboard import Scoreboard
screen = Screen()
screen.setup(width=600, height=600)
screen.title("Python Game")
screen.bgcolor("black")
screen.tracer(0) #turns tracer off
snake = Snake()
food = Food()
scoreboard = Scoreboard()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
#So far im doing it by myself
box = []
x_pos = [0, -20, -40]
#If you want to make multiple versions of a thing just use a for loop
# for boxes in range (0, 3):
# python = Turtle()
# python.color("white")
# python.shape("square")
# python.pencolor("white")
# python.penup()
# python.speed(6)
# python.goto(x = x_pos[boxes], y= 0)
# box.append(python)
game_is_on = True
while game_is_on:
screen.update()
time.sleep(0.1)
snake.move()
#Detect collision from food
if snake.head.distance(food) < 15:
food.refresh()
#Most likely going to put scoreboard updating code here too
#Did it by myself
# scoreboard.goto(0, 250)
# scoreboard.clear()
# scoreboard.score += 1
# scoreboard.write(f"Score: {scoreboard.score}", True, font= ("Arial", 24, "normal")) #This is working too
scoreboard.increased_score()
print(scoreboard.score)
#If the tail and head collide
if snake.head.distance(snake.tail) <= 1: #THIS IS WORKING! DID IT BY MYSELF
# print("Game over")
# scoreboard.score = 0
# scoreboard.clear() #Did by myself
# scoreboard.goto(0, 250)
# scoreboard.penup()
# scoreboard.write("GAME OVER", True, font= ("Arial", 24, "normal")) #This is working too
game_is_on = False
scoreboard.tailcollision()
# wall collission
if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280:
game_is_on = False
scoreboard.gameover()
#we have refactored our code so this has been commented out
# for boxes in range(len(box) - 1, 0, -1):
# new_x = box[boxes - 1].xcor()
# new_y = box[boxes - 1].ycor()
# box[boxes].goto(new_x, new_y)
# box[0].forward(20)
# box[0].left(90)
# snake_obj.move()
#not using this anymore
# for boxes in box:
# boxes.forward(20)
# def move_forward():
# python.setheading(0)
# python.forward(30)
# python.onkey(move_forward, "w")
#might have to use forloop to add more squares
# or a while loop with an if statement
# so it only adds squares when the snake eats
#Probably going to use this
#xcor()
#ycor()
screen.exitonclick()