Python For Kids之火柴人逃生游戏

from tkinter import *import timeclass Game: def __init__(self): self.tk = Tk() self.tk.title("Mr. Stick Man Races for the Exit") self.tk.resizable(0, 0) self.tk...
摘要由CSDN通过智能技术生成
from tkinter import *
import time

class Game:
    def __init__(self):
        self.tk = Tk()
        self.tk.title("Mr. Stick Man Races for the Exit")
        self.tk.resizable(0, 0)
        self.tk.wm_attributes("-topmost", 1)
        self.canvas = Canvas(self.tk, width=500, height=500, highlightthickness=0)
        self.canvas.pack()
        self.tk.update()
        self.canvas_height = 500
        self.canvas_width = 500
        self.bg = PhotoImage(file="E:\\stickman\\background.gif")
        self.bg2 = PhotoImage(file="E:\\stickman\\background2.gif")
        self.bg_shelf = PhotoImage(file="E:\\stickman\\shelf.gif")
        self.bg_lamp = PhotoImage(file="E:\\stickman\\lamp.gif")
        w = self.bg.width()
        h = self.bg.height()
        count = 0
        draw_background = 0
        for x in range(0, 5):
            for y in range(0, 5):
                if draw_background == 1:
                    self.canvas.create_image(x * w, y * h, image=self.bg, anchor='nw')
                    draw_background = 0
                else:
                    count = count + 1
                    if count == 5:
                        self.canvas.create_image(x * w, y * h, image=self.bg_shelf, anchor='nw')
                    elif count == 9:
                        self.canvas.create_image(x * w, y * h, image=self.bg_lamp, anchor='nw')
                    else:
                        self.canvas.create_image(x * w, y * h, image=self.bg2, anchor='nw')
                    draw_background = 1
        self.sprites = []
        self.running = True
        self.game_over_text = self.canvas.create_text(250, 250, text='YOU WIN!', state='hidden')

    def mainloop(self):
        while 1:
            if self.running == True:
                for sprite in self.sprites:
                    sprite.move()
            else:
                time.sleep(1)
                self.canvas.itemconfig(self.game_over_text, state='normal')
            self.tk.update_idletasks()
            self.tk.update()
            time.sleep(0.01)


class Coords:
    def __init__(self, x1=0, y1=0, x2=0, y2=0):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2


def within_x(co1, co2):
    if (co1.x1 > co2.x1 and co1.x1 < co2.x2) \
            or (co1.x2 > co2.x1 and co1.x2 < co2.x2) \
            or (co2.x1 > co1.x1 and co2.x1 < co1.x2) \
  
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值