Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from tkinter import *
- import time
- root = Tk()
- root.title('test_game')
- root.resizable(0, 0)
- root.wm_attributes('-topmost', 1)
- canvas = Canvas(bg='green', width=500, height=500)
- canvas.pack()
- class Char:
- def __init__(self):
- self.body = canvas.create_oval(50, 50, 70, 70, fill='orange')
- self.x = 0
- canvas.bind_all('<d>', self.turn_right)
- canvas.bind_all('<a>', self.turn_left)
- def turn_right(self, event):
- self.x = 2
- def turn_left(self, event):
- self.x = -2
- def draw(self):
- canvas.move(self.body, self.x, 0)
- pos = canvas.coords(self.body)
- if pos[0] <= 0:
- self.x = 0
- elif pos[2] >= 500:
- self.x = 0
- class Enemy:
- def __init__(self):
- self.text = None
- self.hp = 100
- self.body = canvas.create_rectangle(350, 350, 450, 450, fill='brown')
- self.hp_body = canvas.create_rectangle(350, 330, 350 + self.hp, 340, fill='red')
- self.gui = canvas.create_text(310, 310, text=f'hp: {self.hp}/100')
- def death(self):
- self.text = canvas.create_text(250, 250, text='Дом разрушен')
- canvas.delete(self.body)
- canvas.delete(self.hp_body)
- canvas.delete(self.gui)
- canvas.after(2000, self.respawn)
- def respawn(self):
- canvas.delete(self.text)
- self.__init__()
- def update(self):
- canvas.delete(self.hp_body)
- self.hp_body = canvas.create_rectangle(350, 330, 350 + self.hp, 340, fill='red')
- class Fireball:
- def __init__(self, event):
- self.dmg = 10
- self.x_speed = 0
- self.y_speed = 0
- self.body = None
- self.spawn(event)
- def spawn(self, event):
- char_pos = canvas.coords(char.body)
- self.body = canvas.create_oval(char_pos[0], char_pos[1], char_pos[2] - 10,
- char_pos[3] - 10, fill='yellow')
- # x = random.randint(-2, 2)
- # y = random.randint(-2, 2)
- vector = event.x - char_pos[0], event.y - char_pos[1]
- len_v = math.sqrt(vector[0] ** 2 + vector[1] ** 2)
- vector = vector[0] / len_v, vector[1] / len_v
- self.x_speed = vector[0] * 5
- self.y_speed = vector[1] * 5
- # canvas.move(self.body, x, y)
- def draw(self):
- canvas.move(self.body, self.x_speed, self.y_speed)
- def hit_enemy(self):
- enemy_pos = canvas.coords(enemy.body)
- if not enemy_pos:
- return
- pos = canvas.coords(self.body)
- if pos[2] >= enemy_pos[0] and pos[0] <= enemy_pos[2]:
- if enemy_pos[1] <= pos[3] <= enemy_pos[3]:
- enemy.hp -= self.dmg
- canvas.itemconfig(enemy.gui, text=f'hp: {enemy.hp}/100')
- canvas.delete(self.body)
- fireballs.remove(self)
- enemy.update()
- if enemy.hp <= 0:
- # canvas.create_text(250, 250, text='Дом разрушен')
- enemy.death()
- return False
- enemy = Enemy()
- char = Char()
- fireballs = []
- def shot(event):
- global fireballs
- fireball = Fireball(event)
- fireballs.append(fireball)
- canvas.bind_all('<Button-1>', shot)
- while True:
- char.draw()
- root.update_idletasks()
- for f in fireballs:
- f.draw()
- f.hit_enemy()
- root.update()
- time.sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement