Home > other >  For help, the ship could not move up and down, you can move around, a mystery
For help, the ship could not move up and down, you can move around, a mystery

Time:05-26

The import pygame
The import sys
The class Rocket:
Def __init__ (self) :
Pygame. The init ()
The self. The screen=pygame. Display. Set_mode ((1300600))
Self. Bg_color=(230230230)
Pygame. Display. Set_caption (' rocket ')
Self. Image=pygame. Image. The load (' E: \ python_work \ pratice \ image \ ship BMP ')
Self. Image_rect=self. Image. Get_rect ()
Self. Screen_rect=self. Screen. Get_rect ()
Self. Image_rect. Midbottom=self. Screen_rect. Midbottom
# self. Image_rect. Centerx=self. Screen_rect. Centerx
# self. Image_rect. Centery=self. Screen_rect. Centery
Self. Moving_right=False
Self. Moving_left=False
Self. Moving_up=False
Self. Moving_down=False
Self. Speed=1.5
Def _blit_image (self) :
The self. The screen. The blit (self image, self. Image_rect)
Def _check_event (self) :
For the event in pygame. Event. The get () :
If the event. The type==pygame. KEYDOWN:
If the event. The key==pygame. K_RIGHT:
Self. Moving_right=True
Elif event. The key==pygame. K_LEFT:
Self. Moving_left=True
Elif event. The key==pygame. K_UP:
Self. Moving_up==True
Elif event. The key==pygame. K_DOWN:
Self. Moving_down==True
Elif event. Type==pygame. KEYUP:
If the event. The key==pygame. K_RIGHT:
Self. Moving_right=False
Elif event. The key==pygame. K_LEFT:
Self. Moving_left=False
Elif event. The key==pygame. K_UP:
Self. Moving_up==False
Elif event. The key==pygame. K_DOWN:
Self. Moving_down==False
Elif event. Type==pygame. QUIT:
Sys. The exit ()
Def _moving (self) :
Self. X=float (self. Image_rect. X)
Self. Y=float (self. Image_rect. Y)
If self. Moving_down and self. Image_rect. Bottom Self. + y=self. Speed
If self. Moving_up and self. Image_rect. Top> 0:
Self. - y=self. Speed
If self. Moving_right and self. Image_rect. Right Self. X +=self. Speed
If self. Moving_left and self. Image_rect. Left> 0:
Self. X=self. Speed
Self. Image_rect. X=self. X
Self. Image_rect. Y=self. Y
# print (" left=% d, right=% d "% (self. Image_rect. Left, the self. Image_rect. Right))
# print (" top=% d, bottom=% d "% (self. Image_rect. Top, self. Image_rect. Bottom))
Def _update_screen (self) :
The self. The screen. The fill (self. Bg_color)
Self. _blit_image ()
Pygame. Display. Flip ()
Def run_game (self) :
Print (" left=% d, right=% d "% (self. Image_rect. Left, the self. Image_rect. Right))
Print (" top=% d, bottom=% d "% (self. Image_rect. Top, self. Image_rect. Bottom))
While True:
Self. _check_event ()
Self. _moving ()
Self. _blit_image ()
Self. _update_screen ()
If __name__=="__main__ ':
Rocket=rocket ()
Rocket. Run_game ()

  • Related