有图有真相
地图移动
话不多说,直接上代码,后期更新解释
import sys
import pygame
class Character:
"""管理游戏主角"""
def __init__(self, screen):
self.screen = screen
self.screen_rect = self.screen.get_rect()
self.image = pygame.image.load('image/ship.png').convert_alpha()
self.image_rect = self.image.get_rect()
self.image_rect.center = self.screen_rect.center
self.speed = 3
self.x = float(self.image_rect.x)
self.y = float(self.image_rect.y)
def _update_position(self):
self.image_rect.x = int(self.x)
self.image_rect.y = int(self.y)
def gave_xy(self):
return (int(self.x), int(self.y))
def update_xy(self, position): # 位于边界时需要移动目标
if position == 'up' and self.image_rect.top > self.screen_rect.top:
self.y -= self.speed
elif position == 'down' and self.image_rect.bottom < self.screen_rect.bottom:
self.y += self.speed
elif position == 'left' and self.image_rect.left > self.screen_rect.left:
self.x -= self.speed
elif position == 'right' and self.image_rect.right < self.screen_rect.right:
self.x += self.speed
self._update_position()
def display(self):
self.screen.blit(self.image, self.image_rect)
class Bg_image:
"""管理游戏的背景移动的类"""
def __init__(self, screen):
self.screen = screen
self.screen_rect = self.screen.get_rect()
self.bg_image = pygame.image.load('image/ptoto_1.png').convert()
self.bg_image_rect = self.bg_image.get_rect()
self.bg_image_rect.center = self.screen_rect.center
self.speed = 3
self.x = float(self.bg_image_rect.x)
self.y = float(self.bg_image_rect.y)
def update_xy(self, position):
if position == 'up':
self.y += self.speed
elif position == 'down':
self.y -= self.speed
elif position == 'left':
self.x += self.speed
elif position == 'right':
self.x -= self.speed
self.bg_image_rect.x = int(self.x)
self.bg_image_rect.y = int(self.y)
def border(self):
t1 = self.bg_image_rect.left >= self.screen_rect.left
t2 = self.bg_image_rect.right <= self.screen_rect.right
t3 = self.bg_image_rect.top >= self.screen_rect.top
t4 = self.bg_image_rect.bottom <= self.screen_rect.bottom
isok = {}
isok['left'] = t1
isok['right'] = t2
isok['up'] = t3
isok['down'] = t4
return isok
def gave_xy(self):
return (int(self.x), int(self.y))
def gave_wh(self):
return (self.bg_image_rect.width, self.bg_image_rect.height)
def display(self):
self.screen.blit(self.bg_image, self.bg_image_rect)
class MainGame:
"""管理游戏的主类"""
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((300, 300))
self.screen_rect = self.screen.get_rect()
pygame.display.set_caption('间隙之间')
self.bg = Bg_image(self.screen)
self.bg_wh = self.bg.gave_wh()
self.character = Character(self.screen)
self.move_up = False
self.move_down = False
self.move_left = False
self.move_right = False
self.clock = pygame.time.Clock() # 游戏大时钟
def run_game(self):
while True:
self.clock.tick(120)
self._check_event()
self._update_screen()
def _check_event(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
else:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
else:
if event.key == pygame.K_w:
self.move_up = True
if event.key == pygame.K_s:
self.move_down = True
if event.key == pygame.K_a:
self.move_left = True
if event.key == pygame.K_d:
self.move_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
self.move_up = False
if event.key == pygame.K_s:
self.move_down = False
if event.key == pygame.K_a:
self.move_left = False
if event.key == pygame.K_d:
self.move_right = False
self._update_position()
def _update_position(self):
if self.move_up:
self._select_move('up')
if self.move_down:
self._select_move('down')
if self.move_left:
self._select_move('left')
if self.move_right:
self._select_move('right')
def _select_move(self, position):
image_xy = self.bg.gave_xy()
character_xy = self.character.gave_xy()
role_x = character_xy[0] - image_xy[0]
role_y = character_xy[1] - image_xy[1]
t1 = role_x <= self.screen_rect.width // 2
t2 = role_x >= self.bg_wh[0] - self.screen_rect.width // 2
t3 = role_y <= self.screen_rect.height // 2
t4 = role_y >= self.bg_wh[1] - self.screen_rect.height // 2
border = self.bg.border()
if position == 'left' and (border['left'] or t2):
self.character.update_xy('left')
elif position == 'right' and (border['right'] or t1):
self.character.update_xy('right')
elif position == 'up' and (border['up'] or t4):
self.character.update_xy('up')
elif position == 'down' and (border['down'] or t3):
self.character.update_xy('down')
else:
self.bg.update_xy(position)
def _update_screen(self):
self.screen.fill((0, 0, 0))
self.bg.display()
self.character.display()
pygame.display.update()
if __name__ == '__main__':
myGame = MainGame()
myGame.run_game()