上述2部分对接上
pygame部分我们只加载个背景,然后里面放1只乌龟精灵,用键盘的上下左右键来控制,直接给出代码:
乌龟精灵代码(DemoSpirit.py):
import pygame class DemoSpirit(pygame.sprite.Sprite): def __init__(self, target, screen_size, position): pygame.sprite.Sprite.__init__(self) self.target_surface = target self.screen_size = screen_size self.position = position self.image = pygame.image.load("resources\\wugui.png").convert_alpha() self.image = pygame.transform.smoothscale(self.image, (50, 50)) def draw(self): # random_text = font_200.render('***', True, white_color) self.target_surface.blit(self.image, self.position) def move_left(self): if self.position[0]-10 > 0: self.position=(self.position[0]-10, self.position[1]) def move_right(self): if self.position[0]+10 < self.screen_size[0]: self.position=(self.position[0]+10, self.position[1]) def move_up(self): if self.position[1] - 10 > 0: self.position=(self.position[0], self.position[1]-10) def move_down(self): if self.position[1] + 10 < self.screen_size[1]: self.position=(self.position[0], self.position[1]+10)