火柴人逃脱小游戏(python 超详细)

本文介绍如何使用Python制作火柴人逃脱小游戏,涵盖精灵冲突检测、运动及碰撞检测等核心内容,附带详细代码解析和资源分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家好,今天我们开始做一个新的游戏——火柴人逃脱。
以下是效果图:
在这里插入图片描述
温馨提示:代码里有图片资源,你可以自己用GIMP画,也可以去下载:
图片
我们来看第一部分代码:

from tkinter import *
import time,random

这一部分没什么特别的,你可以到我的Ball专栏中看细解。

class Game:
    def __init__(self):
        self.tk=Tk()
        self.tk.title('stick man game')
        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_width=500
        self.canvas_height=500
        self.bg=PhotoImage(file='../image/background.gif')
        w=self.bg.width()
        h=self.bg.height()
        for i in range(0,5):
            for j in range(0,5):
                self.canvas.create_image(i*w,j*h,image=self.bg,anchor='nw')
        self.sprites=[]
        self.running=True
    def mainloop(self):
        while 1:
            if self.running==True:
                for sprite in self.sprites:
                    sprite.move()
            else:
                canvas.create_text(100, 150, text='ok', fill='blue', font=('Times', 20))
            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.x1<co2.x1) or (co2.x2>co1.x1 and co2.x1<co1.x2)\
        or (co1.x2>co2.x1 and co1.x2<co2.x1):
        return True
    else:
        return False
def within_y(co1,co2):
    if (co1.y1 > co2.y1 and co1.y1<co2.y2) or (co1.y2>co2.y1 and co1.y2<co2.y2) or (co2.y1>co1.y1 and co2.y1<co1.y2)\
        or (co1.y2>co2.y1 and co1.y2<co2.y1):
        return True
    else:
        return False
def colided_left(co1,co2):
    if within_y(co1,co2):
        if co1.x1<=co2.x2 and co1.x1>=co2.x1:
            return True
    return False
def colided_right(co1,co2):
    if within_y(co1,co2):
        if co1.x2>=co2.x1 and co1.x2<=co2.x2:
            return True
    return False
def colided_top(co1,co2):
    if within_x(co1,co2):
        if co1.y1<=co2.y2 and co1.y1>=co2.y1:
            return True
    return False
def collided_bottom(y,co1,co2):
    if within_x(co1,co2):
        y_calc=co1.y2+y
        if y_calc>=co2.y1 and y_calc<=co2.y2:
            return True
    return False

第三部分主要是精灵的冲突检测。

class Sprite:
    def __init__(self,game):
        self.game=game
        self.endgame=False
        self.coordinates=None
    def move(self):
        pass
    def coords(self):
        return self.coordinates

class PlatformSprite(Sprite):
    def __init__(self,game,photo_image,x,y,width,height):
        Sprite.__init__(self, game)
        self.photo_image=photo_image
        self.image=game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')
        self.coordinates=Coords(x,y,x+width,y+height)
class StickFigureSprite(Sprite):
    def __init__(self,game):
        Sprite.__init__(self,game)
        self.images_left=[
            PhotoImage(file='../image/stick-L1.gif'),
            PhotoImage(file='../image/stick-L2.gif'),
            PhotoImage(file='../image/stick-L3.gif')
        ]
        self.images_right = [
            PhotoImage(file='../image/stick-R1.gif'),
            PhotoImage(file='../image/stick-R2.gif'),
            PhotoImage(file='../image/stick-R3.gif')
        ]
        self.image
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值