Python Escape game

Source: Internet
Author: User

From the example of < learning Python programming >, the villain runs on the stone until it reaches the door.

I made the following improvements:

1. Modularity:

    • helper.py contains base classes and collision detection functions
    • man.py Villain
    • Door,py Gate
    • stone.py feet of stone
    • game.py main function, initialize canvas, game main loop
2. Fix a bug, the original person stepping on the stone will fallif BTM and falling and self.y = = 0 \
and (Co.y2 >= self.game.canvas_h \
or Collide_bottom (1, CO, s_co)): #man stand on the stone
Falling = False

Material:

Background bg.gif

Door D_1.gif

A running villain.

Man_l_1

Man_l_2

Man_l_3

Man_r_1

Man_r_2

Man_r_3


Modular Code:

helper.py

 #Objectclass sprite:def __init__ (self, game): Self.game = Gameself.endgame = falseself.co = Nonedef Move (self):p assdef coords (self): return self.coclass coords:def __init__ (self, x1 = 0, y1 = 0, x2 = 0, y2 = 0): self.x1 = x1self.x2 = X2self.y1 = Y1self.y2 = Y2#intersectiondef inter_x (co1, CO2): if (co1.x1 >= co2.x2) or (co1.x2 <= co2.x1): Return Falseelse:return truedef inter_y (co1, CO2): if (co1.y1 >= co2.y2) or (Co1.y2 <= co2.y1): return Falseelse:return truedef collide_left (co1, CO2): if Inter_y (CO1,CO2) and co1.x1 <= co2.x2 and co1.x1 >= co2.x1 : Return Truereturn falsedef collide_right (co1, CO2): if Inter_y (CO1,CO2) and co1.x2 <= co2.x2 and co1.x2 >= co2.x1:re Turn Truereturn falsedef collide_top (co1, CO2): if inter_x (CO1,CO2) and Co1.y1 <= co2.y2 and Co1.y1 >= Co2.y1:return Truereturn falsedef collide_bottom (y, Co1, CO2): y = co1.y2 + yif inter_x (CO1,CO2) and y <= co2.y2 and y >= co2.y1:re Turn Truereturn False 

stone.py


door.py

From helper import *from tkinter import *class Door (Sprite):d ef __init__ (self, game, X, y): sprite.__init__ (self, game); #ca ll fatherself.photo_image = photoimage (file = ' d_1.gif '); self.image = Game.canvas.create_image (x, y, image = Self.photo_i Mage, anchor = ' NW ') self.co = Coords (x, y, x + +, y +) Self.endgame = True

man.py

From helper import *import timefrom tkinter import *class Man (Sprite):d ef __init__ (self, game): sprite.__init__ (self, game ); #call fatherself.image_l = [Photoimage (file= "Man_l_1.gif"), Photoimage (file= "Man_l_2.gif"), Photoimage (file= "Man_ L_3.gif ")]self.image_r = [Photoimage (file=" Man_r_1.gif "), Photoimage (file=" Man_r_2.gif "), Photoimage (file=" Man_r_ 3.gif ")]self.image = game.canvas.create_image (0, 0, image=self.image_l[0], anchor= ' NW ') self.x = -2SELF.Y = 0self.current_img = 0#image Indexself.current_img_add = 1self.jump_count = 0#use for jumpself.last_time = Time.time () Self . CO = Coords () game.canvas.bind_all ("<Key-Left>", Self.turn_left) Game.canvas.bind_all ("<Key-Right>", Self.turn_right) Game.canvas.bind_all ("<Key-space>", Self.jump) def turn_left (self, evt): #if self.y = = 0:self.x = -2def turn_right (Self, evt): #if self.y = = 0:self.x = 2def jump (self, evt): if self.y = = 0:self.y = -4self.jump_count = 0#im Portant change img of mandef animate (self): if self.x! = 0 and Self.y = = 0:iF time.time ()-Self.last_time > 0.1: #change img slowlyself.last_time = time.time () self.current_img + = Self.current_i Mg_addif self.current_img >= 2:self.current_img_add = -1elif self.current_img <= 0:self.current_img_add = 1if self. x < 0:if Self.y! = 0:self.game.canvas.itemconfig (self.image, image = Self.image_l[2]) Else: Self.game.canvas.itemconfig (self.image, image = Self.image_l[self.current_img]) If self.x > 0:if self.y! = 0: Self.game.canvas.itemconfig (self.image, image = Self.image_r[2]) else:self.game.canvas.itemconfig (self.image, image = Self.image_r[self.current_img]) def coords (self): XY = Self.game.canvas.coords (self.image) self.co.x1 = xy[0] Self.co.y1 = XY[1]SELF.CO.X2 = xy[0] + 27self.co.y2 = xy[1] + 30return self.co#importantdef Move (self): self.animate () #for Jump case, update yif self.y < 0:self.jump_count = Self.jump_count + 1if self.jump_count > 20:self.y = 4elif self.y > 0:self.jump_count = self.jump_count-1#collision checkco = Self.coords () left = TRueright = Truetop = Truebtm = Truefalling = True#1.collide with canvasif self.y > 0 and co.y2 >= self.game.canvas_h : self.y = 0BTM = Falseelif self.y < 0 and co.y2 <= 0:self.y = 0top = Falseif self.x > 0 and co.x2 >= self.game . canvas_w:self.x = 0right = Falseelif self.x < 0 and co.x2 <= 0:self.x = 0left = False#2.collide with stonefor s in self.game.sprites:if s = = Self:continues_co = S.coords () if top and Self.y < 0 and Collide_top (CO, S_co): #collide Topsel F.y =-Self.ytop = Falseif BTM and self.y > 0 and Collide_bottom (SELF.Y, CO, S_co): #collide btm#self.y = S_co.y1-co.  y2#if Self.y < 0:SELF.Y = 0btm = Falsetop = Falseif BTM and falling and self.y = = 0 and (co.y2 >= Self.game.canvas_h or Collide_bottom (1, CO, s_co)): #man stand on the stonefalling = Falseif left and self.x < 0 and Collide_left (CO, S_c O): #collide leftself.x = 0left = Falseif S.endgame: #if s is doorself.game.running = Falseif right and self.x > 0 and Co Llide_right (Co, s_co): #cOllide rightself.x = 0right = Falseif S.endgame: #if s is doorself.game.running = Falseprint ("BTM is%s"% BTM); #let the M An fallif falling and btm and self.y = = 0and Co.y2 < SELF.GAME.CANVAS_H:SELF.Y = 4 Self.game.canvas.move (self.image, SE lf.x, SELF.Y)

game.py

From tkinter import *import randomfrom stone import *from man import *from door import * #Game controllerclass Game:def        __init__ (self): self.tk = TK () self.tk.title ("Run away from Gost House") self.tk.resizable (0,0) Self.tk.wm_attributes ("-topmost", 1) Self.canvas = Canvas (self.tk, Width = $, height = $, highlightthickness  =0) Self.canvas.pack () self.tk.update () Self.canvas_h = Self.canvas_w = self.bg            = Photoimage (file= "Bg.gif") 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.sprit ES = [] self.running = True def loop (self): while 1:if self.running:for sprit            E in Self.sprites:sprite.move () self.tk.update_idletasks () self.tk.update () Time.sleep (0.01) G = Game () for I in range: x = random.randint (0,%) y = Random.randint ( -5, 5) width = random.randint (+) G.s Prites.append (Stone (g, X, i + y, width, ten)) #doordoor = Door (g, g.sprites.append) Laststone = Stone (g, 50, 300 , G.sprites.append (laststone) m = Man (g) g.sprites.append (M) G.loop ()


Python Escape game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.