-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathBaby_Snake_Eater_by_tkinter.py
75 lines (59 loc) · 2.23 KB
/
Baby_Snake_Eater_by_tkinter.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
from tkinter import Canvas, Tk
import time
import random
CANVAS_WIDTH = 400
CANVAS_HEIGHT = 400
SIZE = 20
# if you make this larger, the game will go slower
DELAY = 0.1
def main():
root = Tk()
canvas = Canvas(root, width=CANVAS_WIDTH, height=CANVAS_HEIGHT)
canvas.pack()
# Set up the player and goal rectangles
player = canvas.create_rectangle(0, 0, SIZE, SIZE, fill="blue")
goal = canvas.create_rectangle(360, 360, 380, 380, fill="red")
# Set the initial movement direction
movement_direction = [1, 0]
# Event handling
root.bind("<KeyPress>", lambda event: handle_key_press(event, movement_direction))
root.focus_set()
# Animation loop
while True:
canvas.move(player, SIZE * movement_direction[0], SIZE * movement_direction[1])
# Update the world for one heartbeat
player_x = canvas.coords(player)[0]
player_y = canvas.coords(player)[1]
if player_x > CANVAS_WIDTH - SIZE or player_x < 0 or player_y > CANVAS_HEIGHT - SIZE or player_y < 0:
game_over(canvas, root)
break
if player_x == canvas.coords(goal)[0] and player_y == canvas.coords(goal)[1]:
hit_goal(canvas, goal)
root.update()
time.sleep(DELAY)
def handle_key_press(event, movement_direction):
key = event.keysym
if key == "Left":
movement_direction[0] = -1
movement_direction[1] = 0
elif key == "Right":
movement_direction[0] = 1
movement_direction[1] = 0
elif key == "Up":
movement_direction[0] = 0
movement_direction[1] = -1
elif key == "Down":
movement_direction[0] = 0
movement_direction[1] = 1
def game_over(canvas, root):
# Display game over message
canvas.create_text(CANVAS_WIDTH/2, CANVAS_HEIGHT/2, text="Game Over!", font=("Arial", 30), fill="red")
root.quit()
def hit_goal(canvas, goal):
# Generate random x-coordinate as a multiple of SIZE
rand_x = random.randrange(0, CANVAS_WIDTH - SIZE + 1, SIZE)
# Generate random y-coordinate as a multiple of SIZE
rand_y = random.randrange(0, CANVAS_HEIGHT - SIZE + 1, SIZE)
canvas.coords(goal, rand_x, rand_y, rand_x + SIZE, rand_y + SIZE)
if __name__ == '__main__':
main()