Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1ccfdb7

Browse files
committed
base files
0 parents  commit1ccfdb7

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Simple Snake Game in Python 3 for RANDOM, TIME, turtle themes

‎snake.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Simple Snake Game in Python 3 for RANDOM, TIME, turtle themes
2+
3+
4+
importturtle
5+
importtime
6+
importrandom
7+
8+
delay=0.1
9+
10+
# Score
11+
score=0
12+
high_score=0
13+
14+
# Set up the screen
15+
wn=turtle.Screen()
16+
wn.title("Snake Game by SUBUX")
17+
wn.bgcolor("darkblue")
18+
wn.setup(width=600,height=600)
19+
wn.tracer(0)# Turns off the screen updates
20+
21+
# Snake head
22+
head=turtle.Turtle()
23+
head.speed(0)
24+
head.shape("square")
25+
head.color("black")
26+
head.penup()
27+
head.goto(0,0)
28+
head.direction="stop"
29+
30+
# Snake food
31+
food=turtle.Turtle()
32+
food.speed(0)
33+
food.shape("circle")
34+
food.color("red")
35+
food.penup()
36+
food.goto(0,100)
37+
38+
segments= []
39+
40+
# Pen
41+
pen=turtle.Turtle()
42+
pen.speed(0)
43+
pen.shape("square")
44+
pen.color("white")
45+
pen.penup()
46+
pen.hideturtle()
47+
pen.goto(0,260)
48+
pen.write("Score: 0 High Score: 0",align="center",font=("Courier",24,"normal"))
49+
50+
# Functions
51+
defgo_up():
52+
ifhead.direction!="down":
53+
head.direction="up"
54+
55+
defgo_down():
56+
ifhead.direction!="up":
57+
head.direction="down"
58+
59+
defgo_left():
60+
ifhead.direction!="right":
61+
head.direction="left"
62+
63+
defgo_right():
64+
ifhead.direction!="left":
65+
head.direction="right"
66+
67+
defmove():
68+
ifhead.direction=="up":
69+
y=head.ycor()
70+
head.sety(y+20)
71+
72+
ifhead.direction=="down":
73+
y=head.ycor()
74+
head.sety(y-20)
75+
76+
ifhead.direction=="left":
77+
x=head.xcor()
78+
head.setx(x-20)
79+
80+
ifhead.direction=="right":
81+
x=head.xcor()
82+
head.setx(x+20)
83+
84+
# Keyboard bindings
85+
wn.listen()
86+
wn.onkeypress(go_up,"w")
87+
wn.onkeypress(go_down,"s")
88+
wn.onkeypress(go_left,"a")
89+
wn.onkeypress(go_right,"d")
90+
91+
# Main game loop
92+
whileTrue:
93+
wn.update()
94+
95+
# Check for a collision with the border
96+
ifhead.xcor()>290orhead.xcor()<-290orhead.ycor()>290orhead.ycor()<-290:
97+
time.sleep(1)
98+
head.goto(0,0)
99+
head.direction="stop"
100+
101+
# Hide the segments
102+
forsegmentinsegments:
103+
segment.goto(1000,1000)
104+
105+
# Clear the segments list
106+
segments.clear()
107+
108+
# Reset the score
109+
score=0
110+
111+
# Reset the delay
112+
delay=0.1
113+
114+
pen.clear()
115+
pen.write("Score: {} High Score: {}".format(score,high_score),align="center",font=("Courier",24,"normal"))
116+
117+
118+
# Check for a collision with the food
119+
ifhead.distance(food)<20:
120+
# Move the food to a random spot
121+
x=random.randint(-290,290)
122+
y=random.randint(-290,290)
123+
food.goto(x,y)
124+
125+
# Add a segment
126+
new_segment=turtle.Turtle()
127+
new_segment.speed(0)
128+
new_segment.shape("square")
129+
new_segment.color("grey")
130+
new_segment.penup()
131+
segments.append(new_segment)
132+
133+
# Shorten the delay
134+
delay-=0.001
135+
136+
# Increase the score
137+
score+=10
138+
139+
ifscore>high_score:
140+
high_score=score
141+
142+
pen.clear()
143+
pen.write("Score: {} High Score: {}".format(score,high_score),align="center",font=("Courier",24,"normal"))
144+
145+
# Move the end segments first in reverse order
146+
forindexinrange(len(segments)-1,0,-1):
147+
x=segments[index-1].xcor()
148+
y=segments[index-1].ycor()
149+
segments[index].goto(x,y)
150+
151+
# Move segment 0 to where the head is
152+
iflen(segments)>0:
153+
x=head.xcor()
154+
y=head.ycor()
155+
segments[0].goto(x,y)
156+
157+
move()
158+
159+
# Check for head collision with the body segments
160+
forsegmentinsegments:
161+
ifsegment.distance(head)<20:
162+
time.sleep(1)
163+
head.goto(0,0)
164+
head.direction="stop"
165+
166+
# Hide the segments
167+
forsegmentinsegments:
168+
segment.goto(1000,1000)
169+
170+
# Clear the segments list
171+
segments.clear()
172+
173+
# Reset the score
174+
score=0
175+
176+
# Reset the delay
177+
delay=0.1
178+
179+
# Update the score display
180+
pen.clear()
181+
pen.write("Score: {} High Score: {}".format(score,high_score),align="center",font=("Courier",24,"normal"))
182+
183+
time.sleep(delay)
184+
185+
wn.mainloop()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp