Advertisement
here2share

# tk_bountyhunter.py

Jan 19th, 2024
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. # tk_bountyhunter.py
  2.  
  3. from tkinter import *
  4. import random
  5. import time
  6. from tkinter import messagebox
  7.  
  8. # Set up the tkinter window
  9. window = Tk()
  10. window.title("Bounty Hunter")
  11. window.geometry("600x600+10+10")
  12.  
  13. # Define colors
  14. COLOR_BLACK = "#000000"
  15. COLOR_WHITE = "#FFFFFF"
  16. COLOR_RED = "#FF0000"
  17. COLOR_BLUE = "#0000FF"
  18. COLOR_GREEN = "#00FF00"
  19. COLOR_GRAY = "#C0C0C0"
  20.  
  21. # Define player properties
  22. player_size = 20
  23. player_x = 290
  24. player_y = 290
  25.  
  26. # Define goal properties
  27. goal_size = 20
  28. goal_x = random.randint(20, 580)
  29. goal_y = random.randint(20, 580)
  30.  
  31. # Define game properties
  32. goals_achieved = 0
  33. time_limit = 30
  34. time_left = time_limit
  35. game_running = False
  36.  
  37. canvas = Canvas(window, width=600, height=600, bg=COLOR_WHITE)
  38. canvas.pack()
  39.  
  40. MAIN_GOALS = [(x, y) for x in range(60, 540, 60) for y in range(40, 560, 40)]
  41. goals = []
  42.  
  43. def draw_objects():
  44.     canvas.delete(ALL)
  45.     canvas.create_text(300, 300, text=time_left, font=("Arial", 300), fill=COLOR_GRAY)
  46.     canvas.create_rectangle(player_x - player_size, player_y - player_size, player_x + player_size, player_y + player_size, fill=COLOR_BLUE)
  47.     canvas.create_rectangle(goal_x - goal_size, goal_y - goal_size, goal_x + goal_size, goal_y + goal_size, fill=COLOR_RED)
  48.  
  49. def move_player(event):
  50.     global player_x, player_y
  51.     player_x = event.x
  52.     player_y = event.y
  53.    
  54. def move_target():
  55.     global goal_x, goal_y
  56.     if not goals:
  57.         random.shuffle(MAIN_GOALS)
  58.         goals[:] = MAIN_GOALS[:]
  59.     goal_x, goal_y = goals.pop()
  60.  
  61. canvas.bind("<Motion>", move_player)
  62.  
  63. game_running = True
  64. init = True
  65. while game_running:
  66.     if init:
  67.         start_time = time.time()
  68.         goals_achieved = 0
  69.         time_left = time_limit
  70.         move_target()
  71.         init = False
  72.        
  73.     current_time = int(time.time() - start_time)
  74.     if current_time - 1 < time_limit:
  75.         time_left = time_limit - current_time
  76.         if player_x - player_size < goal_x < player_x + player_size and player_y - player_size < goal_y < player_y + player_size:
  77.             goals_achieved += 1
  78.             move_target()
  79.     else:
  80.         message = "Game Over!\nYou achieved a total of {} goals.".format(goals_achieved)
  81.         message += "\n\nDo you want to play again?"
  82.         game_running = init = messagebox.askyesno("Game Over", message)
  83.  
  84.     draw_objects()
  85.     canvas.update()
  86.  
  87. window.destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement