-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_game_linux.py
More file actions
114 lines (106 loc) · 4.42 KB
/
snake_game_linux.py
File metadata and controls
114 lines (106 loc) · 4.42 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
def main():
def leader_board_scores():
from collections import namedtuple
things = []
f = open('scores/high_score', 'r')
contents = f.read()
if len(contents) > 0:
cool = list(contents.split('\n'))
f.close()
cool.pop(len(cool) - 1)
scores = namedtuple('scores', 'name score')
for i in range(len(cool)):
name, value = str(cool[i - 1]).split(':')
things.append(scores(name, value))
final = sorted(things, key=lambda x: getattr(x, 'score'), reverse=True)
if len(things) >= 3:
for i in range(3):
name3, value3 = final[i]
window[str(i) + '-'].update(f'{name3}: {value3}')
else:
for i in range(len(final)):
name3, value3 = final[i]
window[str(i) + '-'].update(f'{name3}: {value3}')
def high_scores(score):
import PySimpleGUI as sg
a = sg.PopupGetText('Name') + ':' + str(score)
f = open('scores/high_score', 'a')
f.write(str(a)+'\n')
f.close()
import PySimpleGUI as sg
import random as r
import time
sg.theme('DarkPurple6')
grid_size = 20
snake = []
head = []
colors = ['red', 'blue', 'orange']
score = 0
x, y = 5, 5
food = (10, 10)
cx, cy = 0, 0
segments = 3
score_temp = sg.T(str(score), key='-score-', size=(20, 2), justification='center',
font=('arial', 10),
text_color='white')
body = sg.Text('Snake', font=('arial', 20), justification='center', size=(60, 0))
grid = ([sg.Button(key=(row, column), size=(2, 1), button_color=('white', 'black')) for row in range(grid_size)]
for column in range(grid_size))
top3 = ([sg.Text(' ', key=f'{i}-', font=('Bold', 15), justification='center', size=(10, 2))] for i in range(3))
controlpanel = [[sg.B('⇧',size=(2,0) ,font=('arial',20),key='up')],
[sg.B('⇦',size=(2,0), font=('arial',20), key='left'),sg.B('⇨',size=(2,0), font=('arial',20), key='right')],
[sg.B('⇩',size=(2,0), font=('arial',20), key='down')]
]
leaderboard = [[sg.Text('Score', justification='center', size=(20, 0))],
[score_temp],
[sg.Column(top3)],
[sg.Frame('hi',controlpanel,element_justification='center')]
]
layout = [[body], [sg.Frame('game', grid, size=(300,200)),
sg.Frame('Stats', leaderboard, font=('Bolded', 15), border_width=10, size=(200, 750),
element_justification='center', vertical_alignment='top-center',
background_color='DarkBlue')]]
window = sg.Window('hi', layout, finalize=True, size=(900, 800))
leader_board_scores()
window[food].update(button_color=('green', 'green'))
while True:
event, value = window.read(50)
if x >= grid_size or x < 0 or y < 0 or y >= grid_size:
high_scores(score)
break
else:
if event == 'up' and event != 'down':
cx = 0
cy = -1
if event == 'down' and event != 'up':
cx = 0
cy = 1
if event == 'left' and event != 'right':
cx = -1
cy = 0
if event == 'right' and event != 'left':
cx = 1
cy = 0
oldx, oldy = x, y
x, y = x + cx, y + cy
if x != oldx or y != oldy:
snake.insert(0, (oldx, oldy))
head = snake[0]
if len(snake) > segments:
window[snake[-1]].update(button_color=('white', 'black'))
snake.pop(len(snake)-1)
window[snake[0]].update(button_color=('blue', colors[r.randint(0, 2)]))
if head == food:
while food in snake:
food = (r.randint(0, grid_size - 1), r.randint(0, grid_size - 1))
else:
segments = segments + 1
window[food].update(button_color=('green', 'green'))
score = score + 1
window['-score-'].update('score: ' + str(score))
if head in snake[1:-1]: # death
break
time.sleep(0.025)
window.close()
if __name__ in "__main__":
main()