import tkinter as tk import alien_invasion from PIL import Image, ImageTk class GameWelcomeScreen(tk.Tk): def __init__(self): super().__init__() self.title("欢迎界面") self.geometry("1300x750") # 设置窗口大小 # 添加标签和按钮 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_image_as_background(self, image_path): img = Image.open(image_path) img = img.resize((self.root.winfo_reqwidth(), self.root.winfo_reqheight()), Image.Resampling.LANCZOS) photo = ImageTk.PhotoImage(img) self.canvas = tk.Canvas(self.root, width=self.root.winfo_reqwidth(), height=self.root.winfo_reqheight()) self.canvas.pack(fill="both", expand=True) # 在Canvas上放置图片 self.canvas.create_image(0, 0, image=photo, anchor="nw") self.canvas.image = photo def start_button(self): print("开始游戏") 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()