Python编程:从入门到实践——火箭上下左右移动

##2020.02.13 python学习日记##

check_events()

def check_events(rocket):
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type==pygame.KEYDOWN:
            if event.key==pygame.K_RIGHT:
                rocket.moving_right=True
            elif event.key==pygame.K_LEFT:
                rocket.moving_left=True
            elif event.key==pygame.K_UP:
                rocket.moving_up=True
            elif event.key==pygame.K_DOWN:
                rocket.moving_down=True

        elif event.type==pygame.KEYUP:
            if event.key==pygame.K_RIGHT:
                rocket.moving_right=False
            elif event.key==pygame.K_LEFT:
                rocket.moving_left=False
            elif event.key==pygame.K_UP:
                rocket.moving_up=False
            elif event.key==pygame.K_DOWN:
                rocket.moving_down=False

class Rocket():

class Rocket():
    def __init__(self,screen):
        self.screen = screen

        self.image = pygame.image.load('rocket.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        self.moving_right=False
        self.moving_left=False
        self.moving_up=False
        self.moving_down=False

    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.rect.centerx += 1
        if self.moving_left and self.rect.left > 0:
            self.rect.centerx -= 1
        if self.moving_up and self.rect.top> 0:
            self.rect.centery -= 1
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.rect.centery += 1

    def blitme(self):
        self.screen.blit(self.image, self.rect)

你可能感兴趣的:(python学习日记)