刚学习Python,编写的程序能正常运行,但火箭不能左右移动,
#主程序
import sys
import pygame
from hj_image import Hj_image
class Move_huojian:
def __init__(self):
pygame.init()
self.screen=pygame.display.set_mode((1200,600))
self.screen_name=pygame.display.set_caption("移动火箭")
self.bj_color=(90,150,190)
self.hj_image = Hj_image(self)
#self.hj_speed=0.5
def _update_screen(self):
self.screen.fill(self.bj_color)
self.hj_image.draw_hj()
pygame.display.flip()
def _event_key(self):
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.hj_image.move_right == True
elif event.key == pygame.K_LEFT:
self.hj_image.move_left == True
elif event.type == pygame.KEYUP:
if event.type == pygame.K_RIGHT:
self.hj_image.move_right == False
elif event.key == pygame.K_LEFT:
self.hj_image.move_left == False
def run_game(self):
while True:
self._event_key()
self.hj_image.gengxin()
self._update_screen()
if __name__=="__main__":
a=Move_huojian()
#火箭图片模块
import pygame
class Hj_image:
def __init__(self,hj):
self.screen=hj.screen
self.screen_rect=hj.screen.get_rect()
self.image=pygame.image.load("C:/Users/xcz000/Desktop/My Python/images/arm.bmp")
self.image_rect=self.image.get_rect()
#self.x = float(self.image_rect.x)
self.image_rect.midbottom=self.screen_rect.midbottom
self.move_right = False
self.move_left = False
def gengxin(self):
if self.move_right:
self.image_rect.x+=1
if self.move_left:
self.image_rect.x-=1
def draw_hj(self):
self.screen.blit(self.image,self.image_rect)