pyramid escape

Introduction

A small game, made with tkinter

source code

from tkinter import *
import random
import time

class Game:
    def __init__(self):
        self.tk = Tk()    #创建tk对象
        self.tk.title("金字塔大逃亡")    #创建标题
        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="background.gif")   #创建变量bg
        w = self.bg.width()    #获取图像宽高
        h = self.bg.height()
        for x in range(0, 5):    #逐个打印到画布
            for y in range(0, 5):
                self.canvas.create_image(x * w, y * 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()
                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):    # 创建within_x函数,用来判断一组x坐标(x1和x2)是否与另一组x坐标交叉
    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) \
            or (co2.x2 > co1.x1 and co2.x2 < co1.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 (co2.y2 > co1.y1 and co2.y2 < co1.y1):
        return True
    else:
        return False

def collided_left(co1, co2):    #左侧相撞
    if within_y(co1, co2):
        if co1.x1 <= co2.x2 and co1.x1 >= co2.x1:
            return True
    return False

def collided_right(co1, co2):    #右侧相撞
    if within_y(co1, co2):
        if co1.x2 >= co2.x1 and co1.x2 <= co2.x2:
            return True
    return False

def collided_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="stick-L1.gif"),
            PhotoImage(file="stick-L2.gif"),
            PhotoImage(file="stick-L3.gif")
        ]
        self.images_right = [    #向右运动
            PhotoImage(file="stick-R1.gif"),
            PhotoImage(file="stick-R2.gif"),
            PhotoImage(file="stick-R3.gif")
        ]
        self.image = game.canvas.create_image(200, 470, image=self.images_left[0], anchor='nw')
        self.x = -2    #x的运动增量
        self.y = 0    #y的运动增量
        self.current_image = 0    #当前屏幕上的小人图形索引
        self.current_image_add = 1     
        self.jump_count = 0    #跳跃计时器
        self.last_time = time.time()    #当前时间
        self.coordinates = Coords()   #装载坐标
        game.canvas.bind_all('<KeyPress-Left>', self.turn_left)   #按键绑定
        game.canvas.bind_all('<KeyPress-Right>', self.turn_right)
        game.canvas.bind_all('<space>', self.jump)
       
    def turn_left(self, evt):   #小人左转
        if self.y == 0:
            self.x = -2

    def turn_right(self, evt):   #小人右转
        if self.y == 0:
            self.x = 2

    def jump(self, evt):   #小人跳跃
        if self.y == 0:
            self.y = -4
            self.jump_count = 0

    def animate(self):
        if self.x != 0 and self.y == 0:    #判断角色是否在移动和跳跃
            if time.time() - self.last_time > 0.1:    #当前时间减去调用时间,判断是否要化下一序列图案
                self.last_time = time.time()    #重新计时
                self.current_image += self.current_image_add    #变换图像索引
                if self.current_image >= 2:
                    self.current_image_add = -1
                if self.current_image <= 0:
                    self.current_image_add = 1
        if self.x < 0:
            if self.y != 0:
                self.game.canvas.itemconfig(self.image, image=self.images_left[2])
            else:
                self.game.canvas.itemconfig(self.image, image=self.images_left[self.current_image])
        elif self.x > 0:
            if self.y != 0:
                self.game.canvas.itemconfig(self.image, image=self.images_right[2])
            else:
                self.game.canvas.itemconfig(self.image, image=self.images_right[self.current_image])

    def coords(self):
        xy = list(self.game.canvas.coords(self.image))    #返回图像的位置
        self.coordinates.x1 = xy[0]
        self.coordinates.y1 = xy[1]
        self.coordinates.x2 = xy[0] + 27     #抠出图像本身的宽高
        self.coordinates.y2 = xy[1] + 30
        return self.coordinates
    
    def move(self):    
        self.animate()    #调用animate函数  
        if self.y < 0:    #判断跳跃状态,决定上移下移
            self.jump_count += 1
            if self.jump_count > 20:
                self.y = 4
        if self.y > 0:
            self.jump_count -= 1
            
        co = self.coords()   #返回角色位置
        left = True     #创建布尔值,后续用来返回角色碰撞状态
        right = True
        top = True
        bottom = True
        falling = True   
    
        if self.y > 0 and co.y2 >= self.game.canvas_height:    #如果底部大于角色的高度,y设为0,不让角色继续下落
            self.y = 0
            bottom = False
        elif self.y < 0 and co.y1 <= 0:    
            self.y = 0
            top = False
            
        if self.x > 0 and co.x2 >= self.game.canvas_width:
            self.x = 0
            right = False
        elif self.x < 0 and co.x1 <= 0:
            self.x = 0
            left = False

        for sprite in self.game.sprites:     #对精灵列表进行循环,把每个值赋给sprite
            if sprite == self:    #判断精灵是不是当前角色
                continue
            sprite_co = sprite.coords()   #得到新精灵坐标
            if top and self.y < 0 and collided_top(co, sprite_co):
                self.y = -self.y
                top = False
                #判断是否有碰撞
            if bottom and self.y > 0 and collided_bottom(self.y, co, sprite_co):
                self.y = sprite_co.y1 - co.y2
                if self.y < 0:
                    self.y = 0
                bottom = False
                top = False
               #判断底部是不是超出边缘
            if bottom and falling and self.y == 0 and co.y2 < self.game.canvas_height and collided_bottom(1, co, sprite_co):
                falling = False
                #检查左右
            if left and self.x < 0 and collided_left(co, sprite_co):
                self.x = 0
                left = False
                if sprite.endgame:
                    self.game.running = False

            if right and self.x > 0 and collided_right(co, sprite_co):
                self.x = 0
                right = False
                if sprite.endgame:
                    self.game.running = False
            #不在地面下落
        if falling and bottom and self.y == 0 and co.y2 < self.game.canvas_height:
            self.y = 4
        
        self.game.canvas.move(self.image, self.x, self.y)
    
class DoorSprite(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 / 2), y + height)
        self.endgame = True

g=Game()
platform1 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 0, 480, 100, 10)
platform2 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 150, 440, 100, 10)
platform3 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 300, 400, 100, 10)
platform4 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 300, 160, 100, 10)
platform5 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 175, 350, 66, 10)
platform6 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 50, 300, 66, 10)
platform7 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 170, 120, 66, 10)
platform8 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 45, 60, 66, 10)
platform9 = PlatformSprite(g, PhotoImage(file="platform3.gif"), 170, 250, 32, 10)
platform10 = PlatformSprite(g, PhotoImage(file="platform3.gif"), 230, 200, 32, 10)
g.sprites.append(platform1)
g.sprites.append(platform2)
g.sprites.append(platform3)
g.sprites.append(platform4)
g.sprites.append(platform5)
g.sprites.append(platform6)
g.sprites.append(platform7)
g.sprites.append(platform8)
g.sprites.append(platform9)
g.sprites.append(platform10)
door = DoorSprite(g, PhotoImage(file="door1.gif"), 45, 30, 40, 35)
g.sprites.append(door)
sf = StickFigureSprite(g)
g.sprites.append(sf)
g.mainloop()

material 

 background

door1

 

door2

 

platform1

 

platform2

 

platform3

 

stick-L1 

stick-L2

 

stick-L3

 

stick-R1

 

stick-R2

 

stick-R3

 

transparent-image 

Summarize

It is still easier to make small games with pygame. You can take what you like.

Guess you like

Origin blog.csdn.net/https_wyk/article/details/129225367