Python3.8.10每日一练(停车场小游戏)

import tkinter as tk
import random


class ParkingGame:
    def __init__(self, root):
        self.root = root
        self.root.title("停车场小游戏")
        self.root.geometry("600x600")
        self.root.resizable(False, False)

        # 游戏设置
        self.car_size = 40
        self.slot_size = 50
        self.speed = 5
        self.game_running = False

        # 创建游戏界面
        self.create_widgets()

        # 绑定键盘事件
        self.root.bind("<KeyPress>", self.on_key_press)

    def create_widgets(self):
        # 创建游戏画布
        self.canvas = tk.Canvas(self.root, width=600, height=500, bg="gray")
        self.canvas.pack(pady=10)

        # 创建开始按钮
        self.start_button = tk.Button(self.root, text="开始游戏", command=self.start_game)
        self.start_button.pack(pady=10)

        # 创建状态标签
        self.status_label = tk.Label(self.root, text="准备开始", font=("Arial", 14))
        self.status_label.pack()

    def start_game(self):
        # 重置游戏状态
        self.game_running = True
        self.status_label.config(text="游戏进行中")
        self.start_button.config(state=tk.DISABLED)

        # 清空画布
        self.canvas.delete("all")

        # 创建停车场
        self.create_parking_lot()

        # 创建汽车
        self.create_car()

        # 创建目标停车位
        self.create_target_slot()

        # 开始游戏循环
        self.game_loop()

    def create_parking_lot(self):
        # 创建停车场边界
        self.canvas.create_rectangle(50, 50, 550, 450, outline="black", width=2)

        # 创建停车位
        self.slots = []

        # 上方停车位
        for i in range(5):
            x1 = 100 + i * 80
            y1 = 50
            x2 = x1 + self.slot_size
            y2 = y1 + self.slot_size
            slot = self.canvas.create_rectangle(x1, y1, x2, y2, outline="white", width=2)
            self.slots.append(slot)

        # 下方停车位
        for i in range(5):
            x1 = 100 + i * 80
            y1 = 400
            x2 = x1 + self.slot_size
            y2 = y1 + self.slot_size
            slot = self.canvas.create_rectangle(x1, y1, x2, y2, outline="white", width=2)
            self.slots.append(slot)

    def create_car(self):
        # 在停车场中央创建汽车
        x = 300
        y = 250
        self.car = self.canvas.create_rectangle(
            x - self.car_size // 2, y - self.car_size // 2,
            x + self.car_size // 2, y + self.car_size // 2,
            fill="red"
        )

        # 汽车方向
        self.car_dx = 0
        self.car_dy = 0

    def create_target_slot(self):
        # 随机选择一个目标停车位
        self.target_slot = random.choice(self.slots)
        self.canvas.itemconfig(self.target_slot, outline="green", width=3)

    def on_key_press(self, event):
        # 处理键盘事件
        key = event.keysym

        if key == "Up":
            self.car_dy = -self.speed
        elif key == "Down":
            self.car_dy = self.speed
        elif key == "Left":
            self.car_dx = -self.speed
        elif key == "Right":
            self.car_dx = self.speed

    def on_key_release(self, event):
        # 处理键盘释放事件
        key = event.keysym

        if key == "Up" or key == "Down":
            self.car_dy = 0
        elif key == "Left" or key == "Right":
            self.car_dx = 0

    def move_car(self):
        # 移动汽车
        self.canvas.move(self.car, self.car_dx, self.car_dy)

        # 获取汽车位置
        car_coords = self.canvas.coords(self.car)
        x1, y1, x2, y2 = car_coords

        # 碰撞检测 - 边界
        if x1 <= 50:
            self.canvas.move(self.car, 50 - x1, 0)
        elif x2 >= 550:
            self.canvas.move(self.car, 550 - x2, 0)

        if y1 <= 50:
            self.canvas.move(self.car, 0, 50 - y1)
        elif y2 >= 450:
            self.canvas.move(self.car, 0, 450 - y2)

    def check_win(self):
        # 检查汽车是否停入目标停车位
        car_coords = self.canvas.coords(self.car)
        slot_coords = self.canvas.coords(self.target_slot)

        cx1, cy1, cx2, cy2 = car_coords
        sx1, sy1, sx2, sy2 = slot_coords

        # 检查汽车是否完全在停车位内
        if (cx1 >= sx1 and cx2 <= sx2 and
                cy1 >= sy1 and cy2 <= sy2):
            self.game_running = False
            self.status_label.config(text="恭喜你,停车成功!")
            self.start_button.config(state=tk.NORMAL)

    def game_loop(self):
        if self.game_running:
            self.move_car()
            self.check_win()
            self.root.after(20, self.game_loop)


if __name__ == "__main__":
    root = tk.Tk()
    game = ParkingGame(root)
    root.mainloop()

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容