Home > other >  Pray god to solve pygame keydown and keyup problems at the same time
Pray god to solve pygame keydown and keyup problems at the same time

Time:11-28

 import pygame 


The class MainProgress:
Screen_width=500
Screen_height=300
Screen=None
@ staticmethod
Def start_game () :
Pygame. Display. The init ()
MainProgress. Screen=pygame. Display. Set_mode ((MainProgress. Screen_width, MainProgress. Screen_height))
Pygame. Display. Set_caption (' Tank war ')
Pygame. Surface. The fill (MainProgress. Screen (255, 255, 255))
MainProgress. Screen. Blit (MainProgress set_text (), (0, 0))

@ staticmethod
Def set_text () :
Pygame. The font. The init ()
The font=pygame. The font. SysFont (20) "blackbody,"
Text=the font. Render (' your score of 10, True, (0, 0, 0))
Return the text

The class BaseItem (pygame. Sprite. Sprite) :
Def __init__ (self) :
Pygame. Sprite. Sprite. __init__ (self)


The class Tank () :
Def __init__ (self) :
The self. The speed=10
The self. The direction="up"
Self. Life=3
Self. Images={
"Up" : pygame. Image. The load (r "E: \ PythonNote \ image \ p1tankU GIF"),
"Down" : pygame. Image. The load (r "E: \ PythonNote \ image \ p1tankD GIF"),
"Left" : pygame. Image. The load (r "E: \ PythonNote \ image \ p1tankL GIF"),
"Right" : pygame. Image. The load (r "E: \ PythonNote \ image \ p1tankR GIF"),
}
Self. Image=self. Images [self direction]
The self. The rect=self. Image. Get_rect () # the rect a surface can use the rect. Left (top, bottom, right, center, centerx, centery) changes position
The self. The rect. Centerx x=250 # modified center coordinates,
The self. The rect. Centery=290
Self. Up_move=False
Self. Down_move=False
Self. Left_move=False
Self. Right_move=False

Def get_event (self) :
Events=pygame. Event. The get ()
For the event in the events:
If the event. The type==pygame. QUIT:
The quit ()
Elif event. Type==pygame. KEYDOWN:
Print (' key down ')
If the event. The key==pygame. K_w:
The self. The direction="up"
Self. Up_move=True
Elif event. The key==pygame. K_a:
The self. The direction="left"
Self. Left_move=True
Elif event. The key==pygame. K_s:
The self. The direction="down"
Self. Down_move=True
Elif event. The key==pygame. K_d:
The self. The direction="right"
Self. Right_move=True
Elif event. The key==pygame. K_SPACE:
Print (' fire ')
Elif event. Type==pygame. KEYUP:
Print (' key up ')
If the event. The key==pygame. K_w:
Self. Up_move=False
Elif event. The key==pygame. K_a:
Self. Left_move=False
Elif event. The key==pygame. K_s:
Self. Down_move=False
Elif event. The key==pygame. K_d:
Self. Right_move=False

Def move_tank (self) :
If self. Up_move and self. The rect. Top & gt; 0:
The self. The rect. Centery -=1
If self. Down_move and self. The rect. Bottom & lt;=MainProgress. Screen_height:
The self. The rect. Centery +=1
If self. Left_move and self. The rect. Left & gt; 0:
The self. The rect. Centerx -=1
If self. Right_move and self. The rect. Right & lt;=MainProgress. Screen_width:
The self. The rect. Centerx +=1
Self. Image=self. Images [self direction]
MainProgress. Screen. Blit (tank) image, tank. The rect)
Pygame. Display. Flip ()


Tank=tank ()

While True:
MainProgress. Start_game ()
Tank. Get_event ()
Tank. Move_tank ()



I want to achieve continuous mobile tanks,
Key is tanks move getevent this function, I think no problem, but each press movement can also prompt keydown and keyup, so will not be able to realize the tank continuous motion, the great god help see
  • Related