-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathakito.py
More file actions
70 lines (58 loc) · 1.68 KB
/
akito.py
File metadata and controls
70 lines (58 loc) · 1.68 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
import random
import os
import time
# Ukuran papan
WIDTH = 20
HEIGHT = 10
def print_board(snake, food):
os.system('cls' if os.name == 'nt' else 'clear')
for y in range(HEIGHT):
row = ""
for x in range(WIDTH):
if (x, y) in snake:
row += "O"
elif (x, y) == food:
row += "*"
else:
row += "."
print(row)
def move_snake(direction, snake):
head_x, head_y = snake[0]
if direction == "w":
head_y -= 1
elif direction == "s":
head_y += 1
elif direction == "a":
head_x -= 1
elif direction == "d":
head_x += 1
new_head = (head_x, head_y)
return new_head
def main():
snake = [(WIDTH // 2, HEIGHT // 2)]
direction = "d"
food = (random.randint(0, WIDTH-1), random.randint(0, HEIGHT-1))
score = 0
while True:
print_board(snake, food)
print(f"Skor: {score}")
print("Kontrol: W=atas, S=bawah, A=kiri, D=kanan")
# Input arah
inp = input("Arah: ").lower()
if inp in ["w", "a", "s", "d"]:
direction = inp
new_head = move_snake(direction, snake)
# Cek tabrakan
if (new_head in snake) or not (0 <= new_head[0] < WIDTH and 0 <= new_head[1] < HEIGHT):
print("Game Over!")
break
snake.insert(0, new_head)
# Makan makanan
if new_head == food:
score += 1
food = (random.randint(0, WIDTH-1), random.randint(0, HEIGHT-1))
else:
snake.pop()
time.sleep(0.1)
if __name__ == "__main__":
main()b