Skip to content

Commit 36c7e01

Browse files
committed
saving game logic for snake & food
1 parent 7ecebbc commit 36c7e01

File tree

2 files changed

+330
-0
lines changed

2 files changed

+330
-0
lines changed

.gitignore

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
######################
2+
# Node
3+
######################
4+
/node/
5+
node_tmp/
6+
node_modules/
7+
npm-debug.log.*
8+
/.awcache/*
9+
/.cache-loader/*
10+
11+
######################
12+
# SASS
13+
######################
14+
.sass-cache/
15+
16+
######################
17+
# Eclipse
18+
######################
19+
*.pydevproject
20+
.project
21+
.metadata
22+
tmp/
23+
tmp/**/*
24+
*.tmp
25+
*.bak
26+
*.swp
27+
*~.nib
28+
local.properties
29+
.classpath
30+
.settings/
31+
.loadpath
32+
.factorypath
33+
34+
# External tool builders
35+
.externalToolBuilders/**
36+
37+
# Locally stored "Eclipse launch configurations"
38+
*.launch
39+
40+
# CDT-specific
41+
.cproject
42+
43+
# PDT-specific
44+
.buildpath
45+
46+
# STS-specific
47+
/.sts4-cache/*
48+
49+
######################
50+
# IntelliJ
51+
######################
52+
.idea/
53+
*.iml
54+
*.iws
55+
*.ipr
56+
*.ids
57+
*.orig
58+
classes/
59+
out/
60+
61+
######################
62+
# Visual Studio Code
63+
######################
64+
.vscode/*
65+
!.vscode/settings.json
66+
!.vscode/tasks.json
67+
!.vscode/launch.json
68+
!.vscode/extensions.json
69+
*.code-workspace
70+
71+
######################
72+
# Maven
73+
######################
74+
/log/
75+
/target/
76+
77+
######################
78+
# Gradle
79+
######################
80+
.gradle/
81+
/build/
82+
/buildSrc/.gradle/
83+
/buildSrc/build/
84+
85+
######################
86+
# Package Files
87+
######################
88+
*.jar
89+
*.war
90+
*.ear
91+
*.db
92+
93+
######################
94+
# Windows
95+
######################
96+
# Windows image file caches
97+
Thumbs.db
98+
99+
# Folder config file
100+
Desktop.ini
101+
102+
######################
103+
# Mac OSX
104+
######################
105+
.DS_Store
106+
.svn
107+
108+
# Thumbnails
109+
._*
110+
111+
# Files that might appear on external disk
112+
.Spotlight-V100
113+
.Trashes
114+
115+
######################
116+
# Directories
117+
######################
118+
/bin/
119+
/deploy/
120+
121+
######################
122+
# Logs
123+
######################
124+
*.log*
125+
126+
######################
127+
# Others
128+
######################
129+
*.class
130+
*.*~
131+
*~
132+
.merge_file*
133+
134+
######################
135+
# Gradle Wrapper
136+
######################
137+
!gradle/wrapper/gradle-wrapper.jar
138+
139+
######################
140+
# Maven Wrapper
141+
######################
142+
!.mvn/wrapper/maven-wrapper.jar
143+
144+
######################
145+
# ESLint
146+
######################
147+
.eslintcache
148+
149+
######################
150+
# Code coverage
151+
######################
152+
/coverage/
153+
/.nyc_output/
154+
155+
# Solved folders
156+
Solved/
157+
Master/
158+
Main/
159+
*.DS_Store
160+
# Logs
161+
logs
162+
*.log
163+
npm-debug.log*
164+
yarn-debug.log*
165+
yarn-error.log*
166+
lerna-debug.log*
167+
# Diagnostic reports (https://nodejs.org/api/report.html)
168+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
169+
# Runtime data
170+
pids
171+
*.pid
172+
*.seed
173+
*.pid.lock
174+
# Directory for instrumented libs generated by jscoverage/JSCover
175+
lib-cov
176+
# Coverage directory used by tools like istanbul
177+
coverage
178+
*.lcov
179+
# nyc test coverage
180+
.nyc_output
181+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
182+
.grunt
183+
# Bower dependency directory (https://bower.io/)
184+
bower_components
185+
# node-waf configuration
186+
.lock-wscript
187+
# Compiled binary addons (https://nodejs.org/api/addons.html)
188+
build/Release
189+
# Dependency directories
190+
node_modules/
191+
jspm_packages/
192+
# TypeScript v1 declaration files
193+
typings/
194+
# TypeScript cache
195+
*.tsbuildinfo
196+
# Optional npm cache directory
197+
.npm
198+
# Optional eslint cache
199+
.eslintcache
200+
# Microbundle cache
201+
.rpt2_cache/
202+
.rts2_cache_cjs/
203+
.rts2_cache_es/
204+
.rts2_cache_umd/
205+
# Optional REPL history
206+
.node_repl_history
207+
# Output of 'npm pack'
208+
*.tgz
209+
# Yarn Integrity file
210+
.yarn-integrity
211+
# dotenv environment variables file
212+
.env
213+
.env.test
214+
# parcel-bundler cache (https://parceljs.org/)
215+
.cache
216+
# Next.js build output
217+
.next
218+
# Nuxt.js build / generate output
219+
.nuxt
220+
dist
221+
# Gatsby files
222+
.cache/
223+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
224+
# https://nextjs.org/blog/next-9-1#public-directory-support
225+
# public
226+
# vuepress build output
227+
.vuepress/dist
228+
# Serverless directories
229+
.serverless/
230+
# FuseBox cache
231+
.fusebox/
232+
# DynamoDB Local files
233+
.dynamodb/
234+
# TernJS port file
235+
.tern-port
236+
# ignore solved activities, and master homework folders
237+
solved/
238+
Solved/
239+
master/
240+
Master/

main.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from tkinter import *
2+
import random
3+
4+
GAME_WIDTH = 700
5+
GAME_HEIGHT = 700
6+
SPEED = 50
7+
SPACE_SIZE = 50
8+
BODY_PARTS = 3
9+
SNAKE_COLOR = "#00FF00"
10+
FOOD_COLOR = "#FF0000"
11+
BACKGROUND_COLOR = "#000000"
12+
13+
14+
15+
class Snake:
16+
def __init__(self):
17+
self.body_size = BODY_PARTS
18+
self.coordinates = []
19+
self.squares = []
20+
21+
for i in range(0, BODY_PARTS):
22+
self.coordinates.append([0, 0])
23+
24+
for x, y in self.coordinates:
25+
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR, tag="snake")
26+
self.squares.append(square)
27+
28+
29+
30+
31+
class Food:
32+
33+
def __init__(self):
34+
35+
x = random.randint(0, (GAME_WIDTH / SPACE_SIZE)- 1) * SPACE_SIZE
36+
y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE)- 1) * SPACE_SIZE
37+
38+
self.coordinates = [x, y]
39+
40+
canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag="food")
41+
42+
43+
44+
45+
46+
47+
def next_turn():
48+
pass
49+
50+
def change_direction(new_direction):
51+
pass
52+
53+
def check_collisions():
54+
pass
55+
56+
def game_over():
57+
pass
58+
59+
60+
window = Tk()
61+
window.title("Snake Game")
62+
window.resizable(False, False)
63+
64+
score = 0
65+
direction = 'down'
66+
67+
68+
label = Label(window, text="Score:{}".format(score), font=("Arial", 40))
69+
label.pack()
70+
71+
canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH)
72+
canvas.pack()
73+
74+
75+
window.update()
76+
77+
window_width = window.winfo_width()
78+
window_height = window.winfo_height()
79+
screen_width = window.winfo_screenwidth()
80+
screen_height = window.winfo_screenheight()
81+
82+
x = int((screen_width/2) - (window_width/2))
83+
y = int((screen_height/2) - (window_height/2))
84+
85+
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
86+
87+
snake = Snake()
88+
food = Food()
89+
90+
window.mainloop()

0 commit comments

Comments
 (0)