Advertisement
CodeCrusader

Task For You #Complete The Game

Jun 10th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | Source Code | 0 0
  1. # I want YOU to try complete this game.
  2. #  1. Make the food eating system work
  3. #  2. When the obstacle/enemy touches you, lose.
  4. #  3. [OPTIONAL CHALLENGE] Make a level system that gets harder and harder.
  5. #  4. [OPTIONAL CHALLENGE] Make cleaner UI
  6.  
  7. # WHEN DONE, WRITE A COMMENT TO THE PASTEBIN URL OF YOUR CODE!
  8.  
  9. import tkinter as tk
  10. import random
  11. import math
  12.  
  13. # Initialize the window
  14. window = tk.Tk()
  15. window.title("Retro Game")
  16. window.geometry("500x500")
  17.  
  18. # Game variables
  19. character_pos = [50, 50]
  20. character_speed = 2
  21. food_pos = [random.randint(50, 450), random.randint(50, 450)]
  22. obstacle_pos = [random.randint(50, 450), random.randint(50, 450)]
  23. obstacle_speed = 0.5
  24. score = 0
  25. score_label = None  # Declare score_label as a global variable
  26.  
  27. # Function to move the character
  28. def move_character(event):
  29.     key = event.keysym
  30.  
  31.     if key == "Up":
  32.         character_pos[1] -= character_speed
  33.     elif key == "Down":
  34.         character_pos[1] += character_speed
  35.     elif key == "Left":
  36.         character_pos[0] -= character_speed
  37.     elif key == "Right":
  38.         character_pos[0] += character_speed
  39.  
  40.     check_collision()
  41.  
  42. # Function to update the obstacle's position
  43. def update_obstacle():
  44.     global obstacle_pos
  45.  
  46.     dx = character_pos[0] - obstacle_pos[0]
  47.     dy = character_pos[1] - obstacle_pos[1]
  48.     angle = math.atan2(dy, dx)
  49.     obstacle_pos[0] += obstacle_speed * math.cos(angle)
  50.     obstacle_pos[1] += obstacle_speed * math.sin(angle)
  51.  
  52.     check_collision()
  53.  
  54. # Function to check collision between character and objects/obstacles
  55. def check_collision():
  56.     global score
  57.  
  58.     if character_pos[0] == food_pos[0] and character_pos[1] == food_pos[1]:
  59.         score += 1
  60.         food_pos[0] = random.randint(50, 450)
  61.         food_pos[1] = random.randint(50, 450)
  62.  
  63.     update_game()
  64.  
  65. # Function to update the game
  66. def update_game():
  67.     canvas.delete("all")
  68.  
  69.     canvas.create_rectangle(character_pos[0] - 10, character_pos[1] - 10,
  70.                             character_pos[0] + 10, character_pos[1] + 10,
  71.                             fill="blue")
  72.  
  73.     canvas.create_oval(food_pos[0] - 5, food_pos[1] - 5,
  74.                        food_pos[0] + 5, food_pos[1] + 5,
  75.                        fill="green")
  76.  
  77.     canvas.create_rectangle(obstacle_pos[0] - 10, obstacle_pos[1] - 10,
  78.                             obstacle_pos[0] + 10, obstacle_pos[1] + 10,
  79.                             fill="red")
  80.  
  81.     score_label.config(text="Score: {}".format(score))
  82.  
  83. # Create the canvas for drawing
  84. canvas = tk.Canvas(window, bg="white", width=500, height=500)
  85. canvas.pack()
  86.  
  87. # Create a label to display the score
  88. score_label = tk.Label(window, text="Score: 0")
  89. score_label.pack(pady=10)
  90.  
  91. # Bind keyboard events to the character movement function
  92. window.bind("<KeyPress>", move_character)
  93. window.focus_set()
  94.  
  95. # Start the game loop
  96. def game_loop():
  97.     update_obstacle()
  98.     window.after(10, game_loop)
  99.  
  100. game_loop()
  101.  
  102. # Start the game
  103. update_game()
  104.  
  105. # Run the tkinter event loop
  106. window.mainloop()
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement