Home > other >  Match the game appear error: invalid command name ".! Canvas, "what should consult a great
Match the game appear error: invalid command name ".! Canvas, "what should consult a great

Time:09-16

The source code is as follows:
 
The from tkinter import *
Import the random
The import time


The class Game:
Def __init__ (self) :
Self. Tk=tk ()
Self. Tk. Title (" Mr. Stick Man Races for Exit ")
Self. Tk. The resizable (0, 0)
Self. Tk. Wm_attributes (" - the topmost ", 1)
Self. Canvas=canvas (self. Tk, width=500, height=500, highlightthickness=0)
Self. Canvas. Pack ()
Self. Tk. The update ()
Self. Canvas_height=500
Self. Canvas_width=500
The self. The bg=PhotoImage (file="background. GIF")
W=self. Bg. Width ()
H=self. Bg. Height ()
For x in the range (0, 5) :
For y in range (0, 5) :
Self. Canvas. Create_image (h * w x, y * and image=self. The bg, anchor='nw')
The self. The 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. The update ()
Time. Sleep (0.01)


The class Coords:
Def __init__ (self, the x1=0, y1=0, x2=0, y2=0) :
Self. X1=x1
Self. Y1=y1
The self. The x2=x2
Self. Y2=y2


Def within_x (co1 and co2) :
If (co2. X1 & lt; Co1. X1 & lt; Co2. X2) \
Or (co2. X1 & lt; Co1. X2 & lt; Co2. X2) \
Or (co1. X1 & lt; Co2. X1 & lt; Co1. X2) \
Or (co2. X2 & gt; Co1. X1 & gt; Co2. X2) :
Return True
The else:
Return False


Def within_y (co1 and co2) :
If (co2) y1 & lt; Co1. Y1 & lt; Co2. Y2) \
Or (co2) y1 & lt; Co1. Y2 & lt; Co2. Y2) \
Or (co1 y1 & lt; Co2. Y1 & lt; Co1. Y2) \
Or (co2, y2 & gt; Co1. Y1 & gt; Co2. Y2) :
Return True
The else:
Return False


Def collided_left (co1 and co2) :
If within_y (co1 and co2) :
If co2. X2 & gt;=co1. X1 & gt;=co2. X1:
Return True
Return False


Def collided_right (co1 and co2) :
If within_y (co1 and co2) :
If co2. X1 & lt;=co1. X2 & lt;=co2. X2:
Return True
Return False


Def collided_top (co1 and co2) :
If within_x (co1 and co2) :
If co2. Y2 & gt;=co1. Y1 & gt;=co2. Y1:
Return True
Return False


Def collided_bottom (y, co1 and co2) :
If within_x (co1 and co2) :
Y_calc=co1. Y2 + y
If co2. Y1 & lt;=y_calc & lt;=co2. Y2:
Return True
Return False


The class Sprite:
Def __init__ (self, game) :
The self. The game=game
The self. The endgame=False
Self. Coordinates=None

Def move (self) :
Pass

Def coords (self) :
Return the self. Coordinates


The 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)


The 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], the anchor='nw')
Self. X=- 2
The self. The y=0
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 (' & lt; KeyPress - Left> ', the self. Turn_left)
Game. Canvas. Bind_all (' & lt; KeyPress - Right> ', the self. Turn_right)
Game. Canvas. Bind_all (' & lt; Space> ', the self. Jump)

Def turn_left (self, evt) :
If the self. The y==0:
Self. X=- 2

Def turn_right (self, evt) :
If the self. The y==0:
Self. X=2

Def jump (self, evt) :
If the self. The y==0:
Self. Y=- 4
Self. Jump_count=0

Def the animate (self) :
If the self x!=0 and the self. The y==0:
If time. Time () - self. Last_time & gt; 0.1:
Self. Last_time=time. Time ()
Self. Current_image +=self. Current_image_add
If self. Current_image & gt;=2:
Self. Current_image_add=1
If self. Current_image & lt;=0:
Self. Current_image_add=1
If self. X & lt; 0:
If self. Y!=0:
Self. Game. Canvas. Itemconfig (self image, image=self. Images_left [2])
The else:
Self. Game. Canvas. Itemconfig (self image, image=self. Images_left [self. Current_image])
Elif self. X & gt; 0:
If self. Y!=0:
Self. Game. Canvas. Itemconfig (self image, image=self. Images_right [2])
The else:
Self. Game. Canvas. Itemconfig (self image, image=self. Images_right [self. Current_image])

Def coords (self) :
Y=list (self. Game. Canvas. Coords (self. Image))
Self. Coordinates. X1=y [0]
Self. Coordinates. Y1=y [1]
Self. Coordinates. X2=y [0] + 27
Self. Coordinates. Y2=y [1] + 30
Return the self. Coordinates

Def move (self) :
The self. The animate ()
If the self. The y & lt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related