import random import tkinter from base import Base from settings import CanvasSettings, EnemyOneSettings, EnemyTwoSettings, EnemyThreeSettings settings_canvas = CanvasSettings() settings_one = EnemyOneSettings() settings_two = EnemyTwoSettings() settings_three = EnemyThreeSettings() class Enemy_1(Base): def __init__(self, root, canvas, queue, tags): super(Enemy_1, self).__init__(root, canvas, queue, settings_one.enemy_1_lives, tags) self.xpos = random.randint( settings_one.enemy_1_width // 2, (settings_canvas.canvas_width - settings_one.enemy_1_width // 2)) self.ypos = -(settings_one.enemy_1_height // 2) self.bg_image = tkinter.PhotoImage(file=settings_one.enemy_1_path) self.speed = settings_one.enemy_1_speed self.NW_xpos = self.xpos - settings_one.enemy_1_width // 2 self.NW_ypos = self.ypos - settings_one.enemy_1_height // 2 self.SE_xpos = self.xpos + settings_one.enemy_1_width // 2 self.SE_ypos = self.ypos + settings_one.enemy_1_height // 2 def run(self): self.canvas.move(self.image, 0, self.speed) self.ypos += self.speed self.NW_xpos = self.xpos - settings_one.enemy_1_width // 2 self.NW_ypos = self.ypos - settings_one.enemy_1_height // 2
from settings import CanvasSettings canvas_settings = CanvasSettings() class Base(): def __init__(self,root,canvas,queue, lives, tags): self.root = root self.canvas = canvas # 对象的生命值 self.lives = lives # 对象的标签 self.tags = tags # 传递消息的队列 self.queue = queue # 创建图片 # def create_image(self, x, y): # img = tkinter.PhotoImage(file = self.path) # self.canvas.create_image(x, y, image = img, tags=self.tags) # 获取图片 def get_image(self, image): self.image = image # 将自己的对角坐标放入队列中 # def put_pos(self, NW_x, NW_y, SE_x, SE_y): # self.queue.put({(self.tags + '_pos'):(NW_x,NW_y,SE_x,SE_y)}) # 更新自己的血量,同时检测血量是否为0 def update_lives(self): self.lives -= 1