Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_bountyhunter.py
- from tkinter import *
- import random
- import time
- from tkinter import messagebox
- # Set up the tkinter window
- window = Tk()
- window.title("Bounty Hunter")
- window.geometry("600x600+10+10")
- # Define colors
- COLOR_BLACK = "#000000"
- COLOR_WHITE = "#FFFFFF"
- COLOR_RED = "#FF0000"
- COLOR_BLUE = "#0000FF"
- COLOR_GREEN = "#00FF00"
- COLOR_GRAY = "#C0C0C0"
- # Define player properties
- player_size = 20
- player_x = 290
- player_y = 290
- # Define goal properties
- goal_size = 20
- goal_x = random.randint(20, 580)
- goal_y = random.randint(20, 580)
- # Define game properties
- goals_achieved = 0
- time_limit = 30
- time_left = time_limit
- game_running = False
- canvas = Canvas(window, width=600, height=600, bg=COLOR_WHITE)
- canvas.pack()
- MAIN_GOALS = [(x, y) for x in range(60, 540, 60) for y in range(40, 560, 40)]
- goals = []
- def draw_objects():
- canvas.delete(ALL)
- canvas.create_text(300, 300, text=time_left, font=("Arial", 300), fill=COLOR_GRAY)
- canvas.create_rectangle(player_x - player_size, player_y - player_size, player_x + player_size, player_y + player_size, fill=COLOR_BLUE)
- canvas.create_rectangle(goal_x - goal_size, goal_y - goal_size, goal_x + goal_size, goal_y + goal_size, fill=COLOR_RED)
- def move_player(event):
- global player_x, player_y
- player_x = event.x
- player_y = event.y
- def move_target():
- global goal_x, goal_y
- if not goals:
- random.shuffle(MAIN_GOALS)
- goals[:] = MAIN_GOALS[:]
- goal_x, goal_y = goals.pop()
- canvas.bind("<Motion>", move_player)
- game_running = True
- init = True
- while game_running:
- if init:
- start_time = time.time()
- goals_achieved = 0
- time_left = time_limit
- move_target()
- init = False
- current_time = int(time.time() - start_time)
- if current_time - 1 < time_limit:
- time_left = time_limit - current_time
- if player_x - player_size < goal_x < player_x + player_size and player_y - player_size < goal_y < player_y + player_size:
- goals_achieved += 1
- move_target()
- else:
- message = "Game Over!\nYou achieved a total of {} goals.".format(goals_achieved)
- message += "\n\nDo you want to play again?"
- game_running = init = messagebox.askyesno("Game Over", message)
- draw_objects()
- canvas.update()
- window.destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement