-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
99 lines (77 loc) · 2.43 KB
/
snake.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
import x68k
import random
import time
from uctypes import addressof
from struct import pack
# randomize
random.seed(int(time.time() * 10))
# score
score = 0
# initial snake pos
snake_x = 32
snake_y = 24
# block lines frame buffer for collision check
block_lines = [ " " * 64 ] * (snake_y - 1)
# function key display off
funckey_mode = x68k.dos(x68k.d.CONCTRL,pack('hh',14,3))
# screen mode
x68k.crtmod(12,True)
# cursor off
x68k.curoff()
# print snake
for y in range(snake_y,32):
s = f"\x1b[{y};{snake_x}H" + "\x1b[37m" + "S"
x68k.dos(x68k.d.CONCTRL,pack('hl',1,addressof(s)))
# title
b = bytes([0x82, 0xd6, 0x82, 0xd1, 0x82, 0xb3, 0x82, 0xf1, 0x8a, 0xeb, 0x8b, 0x40, 0x88, 0xea, 0x94, 0xaf])
x68k.dos(x68k.d.CONCTRL,pack('hhh',3,24,8))
x68k.dos(x68k.d.CONCTRL,pack('hl',1,addressof(b)))
time.sleep(2)
# main loop
while True:
# check left and right keys
scan_code = x68k.dos(x68k.d.KEYCTRL,pack('hh',3,7))
if scan_code & 0x08: # left key
if snake_x > 1:
snake_x -= 1
elif scan_code & 0x20: # right key
if snake_x < 64:
snake_x += 1
# new road blocks line
block_line = " " * 64
bx1 = random.randint(1,100)
if bx1 >= 1 and bx1 <= 62:
block_line = block_line[:bx1-1] + "***" + block_line[bx1-1+3:]
bx2 = random.randint(1,200)
if bx2 >= 1 and bx2 <= 59:
block_line = block_line[:bx2-1] + "******" + block_line[bx2-1+6:]
block_lines.append(block_line)
# vsync wait
x68k.vsync()
# scroll down
x68k.dos(x68k.d.CONCTRL,pack('hhh',3,0,0))
x68k.dos(x68k.d.CONCTRL,pack('h',5))
# print block line
s = f"\x1b[0;0H" + "\x1b[32m" + block_line
x68k.dos(x68k.d.CONCTRL,pack('hl',1,addressof(s)))
# collision check
check_line = block_lines.pop(0)
if check_line[snake_x-1] == '*':
s = "\x1b[14;24H" + "\x1b[46m" + " GAME OVER "
x68k.dos(x68k.d.CONCTRL,pack('hl',1,addressof(s)))
break
# snake head
s = f"\x1b[{snake_y};{snake_x}H" + "\x1b[37m" + "S"
x68k.dos(x68k.d.CONCTRL,pack('hl',1,addressof(s)))
# increment score
score += 100
# display score
s = "\n\x1b[16;24H" + "\x1b[35m" + "YOUR SCORE: " + "\x1b[37m" + f"{score}" + "\x1b[m\r\n"
x68k.dos(x68k.d.CONCTRL,pack('hl',1,addressof(s)))
time.sleep(2)
# flush key buffer
x68k.dos(x68k.d.KFLUSH,pack('h',0))
# resume function key display mode
x68k.dos(x68k.d.CONCTRL,pack('hh',14,funckey_mode))
# cursor on
x68k.curon()