Advertisement
Nikmosi

asdasdasd

Mar 23rd, 2023
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.49 KB | None | 0 0
  1. import tkinter
  2. import random
  3.  
  4. health = 0
  5.  
  6. N_ENEMIES = 4  # Число врагов
  7. N_FIRES = 6  # Число клеток, заполненных огнем
  8.  
  9. def prepare_and_start():
  10.     global player, exit, fires, enemies, gun, GUN_SIZE, PLAYER_HEALTH, GUN_DIRECTION, SPAWN_SIZE
  11.     canvas.delete("all")
  12.     player_pos = (random.randint(1, N_X - 1) * step,
  13.                   random.randint(1, N_Y - 1) * step)
  14.     exit_pos = (random.randint(1, N_X - 1) * step,
  15.                 random.randint(1, N_Y - 1) * step)
  16.     player = canvas.create_oval(
  17.         (player_pos[0], player_pos[1]),
  18.         (player_pos[0] + step, player_pos[1] + step),
  19.         fill='green')
  20.     exit = canvas.create_oval(
  21.         (exit_pos[0], exit_pos[1]),
  22.         (exit_pos[0] + step, exit_pos[1] + step),
  23.         fill='yellow')
  24.     fires = []
  25.     for i in range(N_FIRES):
  26.         fire_pos = (random.randint(1, N_X - 1) * step,
  27.                     random.randint(1, N_Y - 1) * step)
  28.         fire = canvas.create_oval(
  29.             (fire_pos[0], fire_pos[1]),
  30.             (fire_pos[0] + step, fire_pos[1] + step),
  31.             fill='red')
  32.         fires.append(fire)
  33.     enemies = []
  34.     for i in range(N_ENEMIES):
  35.         enemy_pos = (random.randint(0, N_X - 1) * step,
  36.                      random.randint(0, N_Y - 1) * step)
  37.         enemy = canvas.create_oval(
  38.             (enemy_pos[0], enemy_pos[1]),
  39.             (enemy_pos[0] + step, enemy_pos[1] + step),
  40.             fill='white')
  41.         enemies.append((enemy, random.choice([always_right, random_move])))
  42.     label.config(text="Найди выход!")
  43.     master.bind("<KeyPress>", key_pressed)
  44.  
  45.  
  46. def always_right():
  47.     return step, 0
  48.  
  49.  
  50. def random_move():
  51.     return random.choice([(step, 0), (-step, 0), (0, step), (0, -step)])
  52.  
  53.  
  54. def move_wrap(obj, move):
  55.     canvas.move(obj, move[0], move[1])
  56.     x = canvas.coords(obj)[0]
  57.     y = canvas.coords(obj)[1]
  58.     if x < 0:
  59.         canvas.move(obj, N_X * step, 0)
  60.     if x >= N_X * step:
  61.         canvas.move(obj, -N_X * step, 0)
  62.     if y < 0:
  63.         canvas.move(obj, 0, N_Y * step)
  64.     if y >= N_Y * step:
  65.         canvas.move(obj, 0, -N_Y * step)
  66.  
  67.  
  68. def do_nothing(x):
  69.     pass
  70.  
  71. def always_right():
  72.     return (step, 0)
  73.  
  74.  
  75. def random_move():
  76.     return random.choice([(step, 0), (-step, 0), (0, step), (0, -step)])
  77.  
  78.  
  79. def check_move():
  80.     if canvas.coords(player) == canvas.coords(exit):
  81.         label.config(text="Победа!")
  82.         master.bind("<KeyPress>", do_nothing)
  83.     for f in fires:
  84.         if canvas.coords(player) == canvas.coords(f):
  85.             label.config(text="Ты проиграл!")
  86.             master.bind("<KeyPress>", do_nothing)
  87.         for e in enemies:
  88.             if canvas.coords(player) == canvas.coords(e[0]):
  89.                 label.config(text="Ты проиграл!")
  90.                 master.bind("<KeyPress>", do_nothing)
  91.  
  92.  
  93. # if canvas.coords(player) == canvas.coords(exit):
  94.     #     label.config(text="Победа!")
  95.     #     master.bind("<KeyPress>", do_nothing)
  96.     # for f in fires:
  97.     #     if canvas.coords(player) == canvas.coords(f):
  98.     #         player_health -= 10
  99.     #         label.config(text=f"Ты проиграл! Здоровье: {player_health}")
  100.     #         if player_health <= 0:
  101.     #             label.config(text="Ты проиграл! Игра окончена.")
  102.     #             master.bind("<KeyPress>", do_nothing)
  103.  
  104.  
  105. def key_pressed(event):
  106.     if event.keysym == 'W':
  107.         move_wrap(player, (0, -step))
  108.     if event.keysym == 'S':
  109.         move_wrap(player, (0, step))
  110.     if event.keysym == 'A':
  111.         move_wrap(player, (-step, 0))
  112.     if event.keysym == 'D':
  113.         move_wrap(player, (step, 0))
  114.     for enemy in enemies:
  115.         direction = enemy[1]()  # вызвать функцию перемещения у "врага"
  116.         move_wrap(enemy[0], direction)  # произвести  перемещение
  117.  
  118.  
  119. step = 60  # Размер клетки
  120. N_X = 20
  121. N_Y = 15  # Размер сетки
  122. master = tkinter.Tk()
  123. label = tkinter.Label(master, text="Найди выход")
  124. label.pack()
  125. canvas = tkinter.Canvas(master, bg='blue',
  126.                         height=N_Y * step, width=10)
  127. player_pos = (random.randint(0, N_X - 1) * step,
  128.               random.randint(0, N_Y - 1) * step)
  129. exit_pos = (random.randint(0, N_X - 1) * step,
  130.             random.randint(0, N_Y - 1) * step)
  131.  
  132. player = canvas.create_oval((player_pos[0], player_pos[1]),
  133.                             (player_pos[0] + step, player_pos[1] + step),
  134.                             fill='green')
  135. exit = canvas.create_oval((exit_pos[0], exit_pos[1]),
  136.                           (exit_pos[0] + step, exit_pos[1] + step),
  137.                           fill='yellow')
  138. label = tkinter.Label(master, text="Найди выход")
  139. label.pack()
  140. health -= 10  # decrease player health by 10 if hit by fire
  141. if health <= 0:
  142.     label.config(text="Ты проиграл!")
  143.     master.unbind("<KeyPress>")
  144. else:
  145.     label.config(text=f"Твое здоровье: {health}")  # update health label
  146. step = 60
  147. N_X = 10
  148. N_Y = 10
  149. canvas = tkinter.Canvas(master, bg='blue',
  150.                         width=step * N_X, height=step * N_Y)
  151.  
  152.  
  153. canvas.pack()
  154. restart = tkinter.Button(master, text="Начать заново",
  155.                          command=prepare_and_start)
  156. restart.pack()
  157.  
  158. health = 100  # set initial player health
  159. player = None
  160. exit = None
  161. fires = []
  162.  
  163. prepare_and_start()
  164. check_move()
  165. master.bind("<KeyPress>", key_pressed)
  166. master.mainloop()
  167.  
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement