import tkinter as tk import alien_invasion class GameWelcomeScreen(tk.Tk): def __init__(self): super().__init__() self.title("欢迎界面") self.geometry("1300x750") # 设置窗口大小 # self.set_background_image() # 添加标签和按钮 self.welcome_label = tk.Label(self, text="WELCOM TO \nThe Alien Invasion!", font=("comicsansms", 50), foreground="lightblue") self.welcome_label.pack(pady=50) # 使用pack布局管理器 self.start_button = tk.Button(self, text="进入游戏", font=("comicsansms", 20), command=self.start_button, background="lightblue") self.start_button.pack(pady=20) self.quit_button = tk.Button(self, text="退出游戏", font=("comicsansms", 20), command=self.quit, background="lightgreen") self.quit_button.pack(pady=20) self.about_button = tk.Button(self, text="关于游戏", font=("comicsansms", 20), command=self.about_button, background="lightyellow") self.about_button.pack(pady=20) self.mainloop() # def set_background_image(self): # # 加载图片 # bg_image = tk.PhotoImage(file="images/bg.gif") # # 创建一个标签来承载图片,并设置到最底层 # bg_label = tk.Label(self, image=bg_image) # bg_label.place(x=0, y=0, relwidth=1, relheight=1) # # 确保图片保持引用,否则它会被Python的垃圾回收机制销毁 # bg_label.image = bg_image def start_button(self): self.withdraw() # 点击开始游戏后隐藏开始界面 alien_invasion.run_game() def about_button(self): print("本游戏是外星人入侵游戏") root = tk.Tk() # 建立第二个窗口 root.title("关于游戏") root.geometry("400x300") tk.Label(root, text="这是一款使用Pygame包来开发的2D游戏。" "\n它在玩家每消灭一群向下移动的外星人后,将玩家提高一个等级。\n每射杀一个外星人获得的相应的分数,等级越高," "\n游戏的节奏越快,难度越大,击杀每个外星人获得的分数就越高。\n" "玩家的任务就是击落每个从屏幕上方移动下来的外星人,\n当外星人碰到飞船或者屏幕底部时该飞船被摧毁,\n" "每个玩家可拥有三艘飞船,当三艘飞船都被摧毁时结束游戏\n得出最高分。\n\n\n" "游戏使用:左右键移动飞船,空格键发射子弹", font=("comicsansms", 10), foreground='black').pack(pady=20) root.mainloop() if __name__ == "__main__": app = GameWelcomeScreen() app.mainloop()