Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MertcanYavasoglu committed Sep 24, 2023
1 parent 26202c1 commit c789485
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions Snake Game/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
canvas.pack()

direction = 3 # 0-up 1-down 2-left 3-right
player = canvas.create_rectangle(0, 0, 30, 30, fill='green')
player = canvas.create_rectangle(600, 600, 630, 630, fill='green')
berry_x = random.randrange(0, 1890, 30)
berry_y = random.randrange(0,1050, 30)
berry = canvas.create_rectangle(berry_x, berry_y, berry_x+30, berry_y+30, fill='white')

def move_up(event):
global direction
Expand Down Expand Up @@ -43,11 +46,31 @@ def check_coords():
return "no_collision"

def berry_spawn():
berry_x = random.randrange(0, 1890, 30)
berry_y = random.randrange(0,1050, 30)
berry = canvas.create_rectangle(berry_x, berry_y, berry_x+30, berry_y+30, fill='white')
global berry
global berry_x
global berry_y
coords_berry = canvas.coords(berry)
coords_player = canvas.coords(player)
base_x = 0
base_y= 0
moveable_limit_x_left = canvas.coords(berry)[0]
moveable_limit_x_right = 1920 - canvas.coords(berry)[2]
moveable_limit_y_down = 1080 - canvas.coords(berry)[3]
moveable_limit_y_up = canvas.coords(berry)[1]
if coords_berry == coords_player:
list_x = moveable_limit_x_left, moveable_limit_x_right
list_y = moveable_limit_y_down, moveable_limit_y_up
choice_x = random.choice(list_x)
choice_y = random.choice(list_y)
if choice_x == moveable_limit_x_right:
base_x = coords_berry[2]
if choice_y == moveable_limit_y_down:
base_y = coords_berry[3]

berry_move_x = random.randrange(base_x, choice_x, 30)
berry_move_y = random.randrange(base_y, choice_y, 30)
canvas.move(berry, berry_move_x, berry_move_y)
window.after(250, berry_spawn)

def game_loop():
collision_result = check_coords()
Expand Down

0 comments on commit c789485

Please sign in to comment.