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
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit a51826a

Browse files
Merge pull request #516 from chandrabosep/master
Snake Game GUI
2 parents 5e37b1f + b81ec1d commit a51826a

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed

‎projects/Alarm clock/alarm_clock.py‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Import Required Library
2+
from tkinter import *
3+
import datetime
4+
import time
5+
import winsound
6+
from threading import *
7+
8+
# Create Object
9+
root = Tk()
10+
11+
# Set geometry
12+
root.geometry("400x200")
13+
14+
# Use Threading
15+
def Threading():
16+
t1=Thread(target=alarm)
17+
t1.start()
18+
19+
def alarm():
20+
# Infinite Loop
21+
while True:
22+
# Set Alarm
23+
set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
24+
25+
# Wait for one seconds
26+
time.sleep(1)
27+
28+
# Get current time
29+
current_time = datetime.datetime.now().strftime("%H:%M:%S")
30+
print(current_time,set_alarm_time)
31+
32+
# Check whether set alarm is equal to current time or not
33+
if current_time == set_alarm_time:
34+
print("Time to Wake up")
35+
# Playing sound
36+
winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
37+
38+
# Add Labels, Frame, Button, Optionmenus
39+
Label(root,text="Alarm Clock",font=("Helvetica 20 bold"),fg="red").pack(pady=10)
40+
Label(root,text="Set Time",font=("Helvetica 15 bold")).pack()
41+
42+
frame = Frame(root)
43+
frame.pack()
44+
45+
hour = StringVar(root)
46+
hours = ('00', '01', '02', '03', '04', '05', '06', '07',
47+
'08', '09', '10', '11', '12', '13', '14', '15',
48+
'16', '17', '18', '19', '20', '21', '22', '23', '24'
49+
)
50+
hour.set(hours[0])
51+
52+
hrs = OptionMenu(frame, hour, *hours)
53+
hrs.pack(side=LEFT)
54+
55+
minute = StringVar(root)
56+
minutes = ('00', '01', '02', '03', '04', '05', '06', '07',
57+
'08', '09', '10', '11', '12', '13', '14', '15',
58+
'16', '17', '18', '19', '20', '21', '22', '23',
59+
'24', '25', '26', '27', '28', '29', '30', '31',
60+
'32', '33', '34', '35', '36', '37', '38', '39',
61+
'40', '41', '42', '43', '44', '45', '46', '47',
62+
'48', '49', '50', '51', '52', '53', '54', '55',
63+
'56', '57', '58', '59', '60')
64+
minute.set(minutes[0])
65+
66+
mins = OptionMenu(frame, minute, *minutes)
67+
mins.pack(side=LEFT)
68+
69+
second = StringVar(root)
70+
seconds = ('00', '01', '02', '03', '04', '05', '06', '07',
71+
'08', '09', '10', '11', '12', '13', '14', '15',
72+
'16', '17', '18', '19', '20', '21', '22', '23',
73+
'24', '25', '26', '27', '28', '29', '30', '31',
74+
'32', '33', '34', '35', '36', '37', '38', '39',
75+
'40', '41', '42', '43', '44', '45', '46', '47',
76+
'48', '49', '50', '51', '52', '53', '54', '55',
77+
'56', '57', '58', '59', '60')
78+
second.set(seconds[0])
79+
80+
secs = OptionMenu(frame, second, *seconds)
81+
secs.pack(side=LEFT)
82+
83+
Button(root,text="Set Alarm",font=("Helvetica 15"),command=Threading).pack(pady=20)
84+
85+
# Execute Tkinter
86+
root.mainloop()

‎projects/Snake Game/snake_game.py‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
from tkinter import *
2+
import random
3+
4+
GAME_WIDTH = 700
5+
GAME_HEIGHT = 700
6+
SPEED = 100
7+
SPACE_SIZE = 50
8+
BODY_PARTS = 3
9+
SNAKE_COLOR = "#00FF00"
10+
FOOD_COLOR = "#FF0000"
11+
BACKGROUND_COLOR = "#000000"
12+
13+
14+
class Snake:
15+
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+
class Food:
30+
31+
def __init__(self):
32+
33+
x = random.randint(0, (GAME_WIDTH / SPACE_SIZE)-1) * SPACE_SIZE
34+
y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE) - 1) * SPACE_SIZE
35+
36+
self.coordinates = [x, y]
37+
38+
canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag="food")
39+
40+
41+
def next_turn(snake, food):
42+
43+
x, y = snake.coordinates[0]
44+
45+
if direction == "up":
46+
y -= SPACE_SIZE
47+
elif direction == "down":
48+
y += SPACE_SIZE
49+
elif direction == "left":
50+
x -= SPACE_SIZE
51+
elif direction == "right":
52+
x += SPACE_SIZE
53+
54+
snake.coordinates.insert(0, (x, y))
55+
56+
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR)
57+
58+
snake.squares.insert(0, square)
59+
60+
if x == food.coordinates[0] and y == food.coordinates[1]:
61+
62+
global score
63+
64+
score += 1
65+
66+
label.config(text="Score:{}".format(score))
67+
68+
canvas.delete("food")
69+
70+
food = Food()
71+
72+
else:
73+
74+
del snake.coordinates[-1]
75+
76+
canvas.delete(snake.squares[-1])
77+
78+
del snake.squares[-1]
79+
80+
if check_collisions(snake):
81+
game_over()
82+
83+
else:
84+
window.after(SPEED, next_turn, snake, food)
85+
86+
87+
def change_direction(new_direction):
88+
89+
global direction
90+
91+
if new_direction == 'left':
92+
if direction != 'right':
93+
direction = new_direction
94+
elif new_direction == 'right':
95+
if direction != 'left':
96+
direction = new_direction
97+
elif new_direction == 'up':
98+
if direction != 'down':
99+
direction = new_direction
100+
elif new_direction == 'down':
101+
if direction != 'up':
102+
direction = new_direction
103+
104+
105+
def check_collisions(snake):
106+
107+
x, y = snake.coordinates[0]
108+
109+
if x < 0 or x >= GAME_WIDTH:
110+
return True
111+
elif y < 0 or y >= GAME_HEIGHT:
112+
return True
113+
114+
for body_part in snake.coordinates[1:]:
115+
if x == body_part[0] and y == body_part[1]:
116+
return True
117+
118+
return False
119+
120+
121+
def game_over():
122+
123+
canvas.delete(ALL)
124+
canvas.create_text(canvas.winfo_width()/2, canvas.winfo_height()/2,
125+
font=('consolas',70), text="GAME OVER", fill="red", tag="gameover")
126+
127+
128+
window = Tk()
129+
window.title("Snake game")
130+
window.resizable(False, False)
131+
132+
score = 0
133+
direction = 'down'
134+
135+
label = Label(window, text="Score:{}".format(score), font=('consolas', 40))
136+
label.pack()
137+
138+
canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH)
139+
canvas.pack()
140+
141+
window.update()
142+
143+
window_width = window.winfo_width()
144+
window_height = window.winfo_height()
145+
screen_width = window.winfo_screenwidth()
146+
screen_height = window.winfo_screenheight()
147+
148+
x = int((screen_width/2) - (window_width/2))
149+
y = int((screen_height/2) - (window_height/2))
150+
151+
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
152+
153+
window.bind('<Left>', lambda event: change_direction('left'))
154+
window.bind('<Right>', lambda event: change_direction('right'))
155+
window.bind('<Up>', lambda event: change_direction('up'))
156+
window.bind('<Down>', lambda event: change_direction('down'))
157+
158+
snake = Snake()
159+
food = Food()
160+
161+
next_turn(snake, food)
162+
163+
window.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /