Example #1
0
File: gui.py Project: An571sha/GUI
    def __init__(self):
        threading.Thread.__init__(self)
        self.root = tk.Tk()
        self.root.title('Ducky GUI')
        self.root.geometry('1920x1080')
        self.gboll = True
        self.initialise_map = mapping.Mapping(tk)
        self.canvas_and_ducky = self.initialise_map.gen_map('udem1')
        self.canvas = self.canvas_and_ducky[0]
        self.ducky = self.canvas_and_ducky[1]
        self.generated_grid = grid.Grid(self.canvas)

        self.number_of_clicks = 0
        self.text_y_display = tk.StringVar()
        self.text_x_display = tk.StringVar()
        self.text_x_actual = tk.StringVar()
        self.text_y_actual = tk.StringVar()
        self.distance_traveled_display = tk.StringVar()
        self.start_point = tk.StringVar()
        self.end_point = tk.StringVar()
        self.display_speed = tk.StringVar()
        self.label_explain = tk.StringVar()

        self.myduckietown = DuckietownEnv(GUI=self,
                                          domain_rand=False,
                                          draw_bbox=False,
                                          map_name="udem1")
        self.START = [0, 0]
        self.END = [0, 0]
        self.start = []
        self.end = []
        self.ovals = []

        self.speed = 2
        self.sleep_time = 0.2
        self.counter = 0

        self.start_set = False
        self.end_set = False

        self.line = astar.main(self.START, self.END)
        self.path = findpath.Findpath(self.canvas)

        self.coordinate_elements = {
            'display_x': 0,
            'display_y': 0,
            'actual_x': 0,
            'actual_y': 0,
            'distance_traveled': 0,
            'number_clicks': 0
        }
    def reload(self, editor, tiles, tile_width, tile_height, tile_file):
        self.editor = editor

        self.tile_file = tile_file
        self.tiles = tiles
        self.tile_width = tile_width
        self.tile_height = tile_height
        self.grid_width = math.ceil(self.tile_file.width / self.tile_width)
        self.grid_height = math.ceil(self.tile_file.height / self.tile_height)

        self.tile_list = []

        self.grid = grid.Grid()

        for i in range(len(self.tiles)):
            t = tile.Tile(self.tiles, i, batch=self.main_batch, tag=self.editor.tag[i])
            SLOTS_IN_ROW = math.ceil(self.tile_file.width / self.tile_width)
            t.x = self.tile_width * (i % SLOTS_IN_ROW)
            t.y = self.tile_height * int(i / SLOTS_IN_ROW)
            self.tile_list.append(t)
    def __init__(self, editor, tiles, tile_width, tile_height, tile_file):
        self.main_batch = pyglet.graphics.Batch()
        self.editor = editor

        self.tile_file = tile_file
        self.tiles = tiles
        self.chosen_tile = 0
        self.chosen_w = 1
        self.chosen_h = 1
        self.tile_width = tile_width
        self.tile_height = tile_height
        self.grid_width = math.ceil(self.tile_file.width / self.tile_width)
        self.grid_height = math.ceil(self.tile_file.height / self.tile_height)

        self.tile_list = []

        self.grid = grid.Grid()

        for i in range(len(self.tiles)):
            t = tile.Tile(self.tiles, i, batch=self.main_batch, tag=self.editor.tag[i])
            SLOTS_IN_ROW = math.ceil(self.tile_file.width / self.tile_width)
            t.x = self.tile_width * (i % SLOTS_IN_ROW)
            t.y = self.tile_height * int(i / SLOTS_IN_ROW)
            self.tile_list.append(t)
Example #4
0
    def __init__(self,parent,main):
        '''Displays all the widgets for the main Tower Defence Game.'''
        pygame.mixer.Sound.play(button_accept)

        if os.path.isfile("./modules/wave_settings.pixel"): # Check is file exsists
            os.remove("./modules/wave_settings.pixel") #Remove wave_settings file
        else:
            wave_data = open("./modules/wave_settings.pixel","w") # Create file

        sort_speed = open("./modules/sort_speed.pixel","w")
        sort_speed.write(str(0.03))

        self.tower_list = ['red','blue','green','yellow','purple','brown']
        self.tower_cost_list = [50,100,250,500,750,5]
        
        Window.__init__(self,parent) # Inherets the attributes and methods from class Window

        self.imageList()
        initial_data = Game_Constants()
        
        self.health = initial_data.health
        self.money = initial_data.money
        self.wave = 1

        pygame.mixer.music.stop() # Cancels all music currently playing.
        pygame.mixer.music.load("./audio/bgm/biscuits.wav")# Plays song in first parameter.
        pygame.mixer.music.set_volume(self.current_settings[2]/100)
        pygame.mixer.music.play(-1)
        
        self.main = main # Remebers frame binded to class so that it can be later restored.
        self.main.pack_forget() # Forgets the main_frame. (Doesn't delete just hides it.)

        self.parent.bind("<Escape>",self.main_menu)

        # --- Widgets for Options interface. --- #
        self.main_frame = Frame(self.parent) # Stores the frames, frame and display.
        self.main_frame.pack(fill=BOTH, expand=True)

        self.frame = Frame(self.main_frame,bg="#666666",relief=RIDGE)
        self.frame.pack(fill=Y, side=RIGHT)

        self.round_data = Frame(self.frame,bg="#666666",relief=RIDGE)
        self.round_data.pack(fill=BOTH)

        self.sort_data = Frame(self.frame,bg="#666666",relief=RIDGE)
        self.sort_data.pack(fill=BOTH)
        self.sort_data_left = Frame(self.sort_data, bg="#666666",relief=RIDGE)
        self.sort_data_left.pack(fill=BOTH)
        self.sort_data_right = Frame(self.sort_data, bg="#666666",relief=RIDGE)
        self.sort_data_right.pack(fill=BOTH)

        self.button_data = Frame(self.frame,bg="#666666",relief=RIDGE)
        self.button_data.pack(fill=BOTH)

        self.tower_d1 = Frame(self.button_data,bg="#666666",relief = RIDGE)
        self.tower_d2 = Frame(self.button_data,bg="#666666",relief = RIDGE)
        self.tower_d3 = Frame(self.button_data, bg="#666666", relief=RIDGE)
        self.tower_d3.pack(side=BOTTOM,fill=BOTH)
        self.tower_d2.pack(side=BOTTOM,fill=BOTH)
        self.tower_d1.pack(side=BOTTOM,fill=BOTH)

        self.display = Frame(self.main_frame,bg="#999999")
        self.display.pack(fill=BOTH,expand=True)

        self.game_canvas = Canvas(self.display, bg="black")
        self.game_canvas.pack(fill=BOTH,expand=True,padx=5,pady=5)

        self.photo = Image.open("./images/maps/"+self.current_settings[0])
        self.photo = self.photo.resize((700,600),Image.ANTIALIAS)
        self.photo = ImageTk.PhotoImage(self.photo)
        
        self.game_canvas.create_image(0,0,image=self.photo,anchor=NW)
        

        # -- Game_Option Frame -- #
        message_health = "Health: "+str(self.health)
        self.health_label = Label(self.round_data,text=message_health,font=("Fixedsys",14),
                                  bg="#666666",fg="white")
        self.health_label.pack(fill=X)

        self.message_money = "Money: "+str(self.money)
        self.money_label = Label(self.round_data,text=self.message_money,font=("Fixedsys",14),
                                 bg="#666666",fg="white")
        self.money_label.pack(fill=X)

        self.seperator = ttk.Separator(self.round_data).pack(fill=X)
                                                        
        self.round_button = Button(self.round_data, text="Start Wave "+str(self.wave), font=("Fixedsys",14),
                              command=lambda: self.wave_start())
        self.round_button.pack(fill=X,padx=5,pady=5)

        self.stats = Button(self.round_data, text="Game Stats", font=("Fixedsys",14),
                              command=lambda: Game_Overlay())
        self.stats.pack(fill=X,padx=5,pady=5)

        self.main_menu_button = Button(self.round_data, text="Main Menu", font=("Fixedsys",14),
                              command=lambda: self.main_menu())
        self.main_menu_button.pack(fill=X,padx=5,pady=5)

        self.seperator = ttk.Separator(self.round_data).pack(fill=X)

        self.sort_canvas = Canvas(self.sort_data_left, width=90, height=170)
        self.sort_canvas.pack(padx=25,pady=5)

        self.bubble_sort = Button(self.sort_data_right, text="Bubble Sort", font=("Fixedsys",14),
                                  command=lambda: self.bubble(self.sort_canvas,self.game_grid.sort_grid))
        self.bubble_sort.pack(fill=X,padx=5,pady=5)

        self.sort_options = Button(self.sort_data_right, text="Sort Options", font=("Fixedsys",14),
                                 command=lambda: self.s_options())
        self.sort_options.pack(fill=X, padx=5, pady=5)

        self.game_grid = grid.Grid(self.game_canvas,self.sort_canvas,self.current_settings[3],False)

        self.separator = ttk.Separator(self.sort_data).pack(side=BOTTOM,fill=X)

        self.turret1 = Button(self.tower_d1, text="T1\n"+str(self.tower_cost_list[0]), font=("Fixedsys",14),width=8,height=3,
                              command=lambda: self.set_ID(0))
        self.turret1.pack(side=LEFT,padx=15,pady=3)
        
        self.turret2 = Button(self.tower_d1, text="T2\n"+str(self.tower_cost_list[1]), font=("Fixedsys",14),width=8,height=3,
                              command=lambda: self.set_ID(1))
        self.turret2.pack(side=RIGHT,padx=15,pady=3)
        #self.turret2.config(state=DISABLED)
        self.turret3 = Button(self.tower_d2, text="T3\n"+str(self.tower_cost_list[2]), font=("Fixedsys",14),width=8,height=3,
                              command=lambda: self.set_ID(2))
        self.turret3.pack(side=LEFT,padx=15,pady=3)
        #.turret3.config(state=DISABLED)
        self.turret4 = Button(self.tower_d2, text="T4\n"+str(self.tower_cost_list[3]), font=("Fixedsys",14),width=8,height=3,
                              command=lambda: self.set_ID(3))
        self.turret4.pack(side=RIGHT,padx=15,pady=3)
        #self.turret4.config(state=DISABLED)
        self.turret5 = Button(self.tower_d3, text="T5\n"+str(self.tower_cost_list[4]), font=("Fixedsys",14),height=3,width=8,
                              command=lambda: self.set_ID(4))
        self.turret5.pack(side=LEFT,padx=15,pady=3)
        #self.turret5.config(state=DISABLED)
        self.barricade6 = Button(self.tower_d3, text="Ba\n"+str(self.tower_cost_list[5]), font=("Fixedsys",14),height=3,width=8,
                                 command=lambda: self.set_ID(5))
        self.barricade6.pack(side=RIGHT,padx=15,pady=3)

        self.start = time.time()
Example #5
0
    def __init__(self,parent,main):
        pygame.mixer.Sound.play(button_accept)

        if os.path.isfile("./modules/wave_settings.pixel"): # Check is file exsists
            os.remove("./modules/wave_settings.pixel") #Remove wave_settings file
        else:
            wave_data = open("./modules/wave_settings.pixel","w")

        sort_speed = open("./modules/sort_speed.pixel","w")
        sort_speed.write(str(0.03))

        self.tower_list = ['red','blue','green','yellow','purple','brown']
        self.tower_cost_list = [50,100,250,500,750,5]
        
        Window.__init__(self,parent) # Inherets the attributes and methods from class Window

        self.imageList()
        initial_data = Game_Constants()

        self.inst = 0
        
        self.health = initial_data.health
        self.money = initial_data.money
        self.wave = 'Tutorial'

        pygame.mixer.music.stop() # Cancels all music currently playing.
        pygame.mixer.music.load("./audio/bgm/biscuits.wav")# Plays song in first parameter.
        pygame.mixer.music.set_volume(self.current_settings[2]/100)
        pygame.mixer.music.play(-1)

        self.main = main # Remebers frame binded to class so that it can be later restored.
        self.main.pack_forget() # Forgets the main_frame. (Doesn't delete just hides it.)

        self.parent.bind("<Escape>",self.main_menu)

        # --- Widgets for Options interface. --- #
        self.main_frame = Frame(self.parent) # Stores the frames, frame and display.
        self.main_frame.pack(fill=BOTH, expand=True)

        self.frame = Frame(self.main_frame,bg="#666666",relief=RIDGE)
        self.frame.pack(fill=Y, side=RIGHT)

        self.round_data = Frame(self.frame,bg="#666666",relief=RIDGE)
        self.round_data.pack(fill=BOTH)

        self.sort_data = Frame(self.frame,bg="#666666",relief=RIDGE)
        self.sort_data.pack(fill=BOTH)
        self.sort_data_left = Frame(self.sort_data, bg="#666666",relief=RIDGE)
        self.sort_data_left.pack(fill=BOTH)
        self.sort_data_right = Frame(self.sort_data, bg="#666666",relief=RIDGE)
        self.sort_data_right.pack(fill=BOTH)

        self.button_data = Frame(self.frame,bg="#666666",relief=RIDGE)
        self.button_data.pack(fill=BOTH)

        self.tower_d1 = Frame(self.button_data,bg="#666666",relief = RIDGE)
        self.tower_d2 = Frame(self.button_data,bg="#666666",relief = RIDGE)
        self.tower_d3 = Frame(self.button_data, bg="#666666", relief=RIDGE)
        self.tower_d3.pack(side=BOTTOM,fill=BOTH)
        self.tower_d2.pack(side=BOTTOM,fill=BOTH)
        self.tower_d1.pack(side=BOTTOM,fill=BOTH)

        self.display = Frame(self.main_frame,bg="#666666")
        self.display.pack(fill=BOTH,expand=True)
        self.display_bottom = Frame(self.display,bg="#666666")
        self.display_bottom.pack(side=BOTTOM)
        self.display_bottom_l = Frame(self.display_bottom,bg="#666666")
        self.display_bottom_l.pack(side=LEFT)
        self.display_bottom_r = Frame(self.display_bottom,bg="#666666")
        self.display_bottom_r.pack(side=RIGHT)

        self.game_canvas = Canvas(self.display, bg="black")
        self.game_canvas.pack(fill=BOTH,expand=True,padx=5,pady=5)
        self.next_button = Button(self.display_bottom_r, text="Next",font=("Fixedsys",16),width=15,
                                  bg="white", command=lambda: self.instruction('next'))
        self.next_button.pack(side=BOTTOM,padx=10,pady=2)
        self.back_button = Button(self.display_bottom_l, text="Back",font=("Fixedsys",16),width=15,
                                  state = DISABLED,bg="#d1d1d1",command=lambda:self.instruction('back'))
        self.back_button.pack(side=BOTTOM,padx=10,pady=2)

        self.photo = Image.open("./images/maps/"+self.current_settings[0])
        self.photo = self.photo.resize((700,600),Image.ANTIALIAS)
        self.photo = ImageTk.PhotoImage(self.photo)
        
        self.game_canvas.create_image(0,0,image=self.photo,anchor=NW)

        self.textTutorial = self.game_canvas.create_text(333,250,text='--------------------------------\nThe main idea of the game is to\nkill the monsters that will try to reach\nyour town entrance. In order to do that\nyou have to place towers that will\nfire at them. Towers will cost money\nand you can unlock new towers\n by collecting dropped parts from the killed monsters.\nYou can also build barricades\nto make the monsters have a longer path\nso your turrets will have more time to\nkill them.\n--------------------------------',
                                                         font=("Fixedsys",16),justify=CENTER)
        self.text1 = self.game_canvas.create_text(128,20,text="", font=("Fixedsys",16))
        self.text2 = self.game_canvas.create_text(570,540,text="", font=("Fixedsys",16))
        self.text3 = self.game_canvas.create_text(560,65,text="",font=("Fixedsys",16))
        self.text4 = self.game_canvas.create_text(495,99,text="",font=("Fixedsys",16),justify=CENTER)
        self.text5 = self.game_canvas.create_text(455,390,text="",font=("Fixedsys",16),justify=CENTER)
        self.text6 = self.game_canvas.create_text(465,460,text="",font=("Fixedsys",16),justify=CENTER)
        self.text7 = self.game_canvas.create_text(340,250,text="",font=("Fixedsys",25),justify=CENTER)
        self.text8 = self.game_canvas.create_text(345,350,text="",font=("Fixedsys",16),justify=CENTER)

        # -- Game_Option Frame -- #
        message_health = "Health: "+str(self.health)
        self.health_label = Label(self.round_data,text=message_health,font=("Fixedsys",14),
                                  bg="#666666",fg="white")
        self.health_label.pack(fill=X)

        self.message_money = "Money: "+str(self.money)
        self.money_label = Label(self.round_data,text=self.message_money,font=("Fixedsys",14),
                                 bg="#666666",fg="white")
        self.money_label.pack(fill=X)

        self.seperator = ttk.Separator(self.round_data).pack(fill=X)
                                                        
        self.round_button = Button(self.round_data, text="Start Wave "+str(self.wave), font=("Fixedsys",14),
                              command=lambda: self.wave_start(),state=DISABLED)
        self.round_button.pack(fill=X,padx=5,pady=5)

        self.stats = Button(self.round_data, text="Game Stats", font=("Fixedsys",14),
                              command=lambda: Game_Overlay(),state=DISABLED)
        self.stats.pack(fill=X,padx=5,pady=5)

        self.main_menu_button = Button(self.round_data, text="Main Menu", font=("Fixedsys",14),
                              command=lambda: self.main_menu())
        self.main_menu_button.pack(fill=X,padx=5,pady=5)

        self.seperator = ttk.Separator(self.round_data).pack(fill=X)

        self.sort_canvas = Canvas(self.sort_data_left, width=90, height=170)
        self.sort_canvas.pack(padx=25,pady=5)

        self.game_grid = grid.Grid(self.game_canvas,self.sort_canvas,self.current_settings[3],True)

        self.bubble_sort = Button(self.sort_data_right, text="Bubble Sort", font=("Fixedsys",14),
                                  state=DISABLED,command=lambda: self.bubble(self.sort_canvas,self.game_grid.sort_grid))
        self.bubble_sort.pack(fill=X,padx=5,pady=5)

        self.quick_sort = Button(self.sort_data_right, text="Sort Options", font=("Fixedsys",14),
                                 state=DISABLED,command=lambda: self.s_options())
        self.quick_sort.pack(fill=X, padx=5, pady=5)

        self.separator = ttk.Separator(self.sort_data).pack(side=BOTTOM,fill=X)

        self.turret1 = Button(self.tower_d1, text="T1\n"+str(self.tower_cost_list[0]), font=("Fixedsys",14),width=8,height=3,
                              state=DISABLED,command=lambda: self.set_ID(0))
        self.turret1.pack(side=LEFT,padx=15,pady=3)
        
        self.turret2 = Button(self.tower_d1, text="T2\n"+str(self.tower_cost_list[1]), font=("Fixedsys",14),width=8,height=3,
                              state=DISABLED,command=lambda: self.set_ID(1))
        self.turret2.pack(side=RIGHT,padx=15,pady=3)
        self.turret3 = Button(self.tower_d2, text="T3\n"+str(self.tower_cost_list[2]), font=("Fixedsys",14),width=8,height=3,
                              state=DISABLED,command=lambda: self.set_ID(2))
        self.turret3.pack(side=LEFT,padx=15,pady=3)
        self.turret4 = Button(self.tower_d2, text="T4\n"+str(self.tower_cost_list[3]), font=("Fixedsys",14),width=8,height=3,
                              state=DISABLED,command=lambda: self.set_ID(3))
        self.turret4.pack(side=RIGHT,padx=15,pady=3)
        self.turret5 = Button(self.tower_d3, text="T5\n"+str(self.tower_cost_list[4]), font=("Fixedsys",14),height=3,width=8,
                              state=DISABLED,command=lambda: self.set_ID(4))
        self.turret5.pack(side=LEFT,padx=15,pady=3)
        self.barricade6 = Button(self.tower_d3, text="Ba\n"+str(self.tower_cost_list[5]), font=("Fixedsys",14),height=3,width=8,
                                 state=DISABLED,command=lambda: self.set_ID(5))
        self.barricade6.pack(side=RIGHT,padx=15,pady=3)
Example #6
0
    def __init__(self, *args, **kwargs):
        super(EditorWindow, self).__init__(*args, **kwargs)

        # Mouse coordinates
        self.mouse_x = 0
        self.mouse_y = 0

        # Set key handler.
        self.keys = pyglet.window.key.KeyStateHandler()
        self.push_handlers(self.keys)

        self.current_screen = 0  # 0 - drawing, 1 - choosing tiles

        self.main_batch = pyglet.graphics.Batch()
        self.tiles_batch = pyglet.graphics.Batch()

        self.camera = camera.Camera(
            self,
            cameras=2)  # 2 cameras, for 2 different views, grid and choosing
        self.invert_scroll = True

        self.replace_occupied_slots = False  # If on, then you will be able to just switch the tile in drawing

        self.draw_tags = True
        self.tag_label = pyglet.text.Label('',
                                           font_name='Born2bSportyV2',
                                           font_size=12)

        self.tile_width = 50
        self.tile_height = 50

        self.screen_ratio = (16, 9)

        self.drag_button = mouse.MIDDLE

        self.drawing_area_width = 20
        self.drawing_area_height = 10
        self.background = None  # This is not working yet :PP

        self.brush_size = 1
        self.brush_rotation = 0
        self.draw_tile = 0
        self.current_tile_file = 'texture_example.png'
        self.tiles_image = pyglet.resource.image(
            self.current_tile_file)  # Tile file not stripped
        self.tile_images = pyglet.image.ImageGrid(
            self.tiles_image,
            math.ceil(self.tiles_image.height / self.tile_height),
            math.ceil(self.tiles_image.width / self.tile_width))
        self.tag = []
        for i in range(len(self.tile_images)):
            self.tag.append('DEFAULT')

        self.grid = grid.Grid()

        self.draw_preview = tile.Tile(self.tile_images, self.draw_tile)
        self.new_draw_preview()  # Set everything

        self.tile_list = []  # This list contains all the tiles in the editor

        self.choose_area = tile_choose.TileChoose(self, self.tile_images,
                                                  self.tile_width,
                                                  self.tile_height,
                                                  self.tiles_image)
        # This object has it's own variables, I recommend check the tile_choose file out

        self.current_level_file = ''  # The file, that we are working with, very useful, makes everything quicker

        pyglet.clock.schedule_interval(self.window_tick, 1 / 120)

        # Gets print put every time we run a program
        print('---------------------------------------------------------')
        print('Press [SPACE] to show settings')
        print(
            'Press [C] to change tile graphically, [T] on tile to change tag')
        print(
            'Use mouse [MIDDLE] to drag menu, [LEFT] to draw, [RIGHT] to delete'
        )
        print('---------------------------------------------------------')