如何在一张背景图片上移动玩家?

问题描述 投票:0回答:1

我刚刚开始接触 pygame。 对于我之前的问题,我认为我没有正确地表达我想要做的事情。

这是我尝试演示的一张快速图片:

Here is a quick picture I did to try demonstrate

这是我希望玩家移动的单个背景图像或地图。红色 X 只是角色的起点。 我尝试做到这一点,以便当我移动玩家时,背景也会跟随,就像玩家在地图中移动一样。

我已经限制了播放器无法超出实际屏幕的边界。只是现在遇到了一些麻烦,试图让单个图像沿着玩家移动,并且如果玩家到达地图的末尾,图片移动就会停止。

我见过人们在玩家移动时使用滚动和复制图像。我只想看到玩家将移动的单个图像。

我不想担心碰撞,只要能让运动正常工作即可。

这是我当前使用的代码:

import pygame 
from pygame.locals import *
from math import sin

pygame.display.set_caption("TEST")

clock = pygame.time.Clock()
time_passed = 0
class Player():
  def __init__(self,x,y):
    self.Image = pygame.image.load("myAvatar.png").convert()

    self.x = 200
   
    self.y = 200
  

  def getX(self):
    return self.rect.x

  def getY(self):
    return self.rect.y

  def handle_keys(self,screenHeight,screenWidth):
      key = pygame.key.get_pressed()
      dist = 2 

      if key[K_LEFT] and self.x > 0: 
            self.x -= 500 * time_passed
      
      if key[K_RIGHT] and self.x < screenWidth -20:
            self.x += 500 * time_passed
         
      if key[K_UP] and self.y > 0:
        self.y -= 500 * time_passed
      
      if key[K_DOWN] and self.y < screenHeight -20:
        self.y += 500 * time_passed
    


  def draw(self, game_window):
    self.Image = pygame.transform.scale(self.Image,(20,20))
    
    game_window.blit(self.Image, (int(self.x), int(self.y)))
   



class Map():
  def __init__(self):
    self.Image = pygame.image.load("test2.png").convert()




    self.rect = self.Image.get_rect()
    self.x = 0
    self.y = 0


  def draw(self, game_window,screenHeight,screenWidth):

    self.x = min(max(self.x, player.x - 2  * screenWidth / 2), player.x - screenWidth / 2)
    self.y = min(max(self.y, player.y -2  * screenHeight / 2), player.y - screenHeight / 2)



    game_window.blit(self.Image,(-self.x,-self.y))
  


class Enemy():
  def __init__ (self,x,y):
    self.Image = pygame.image.load("WC.jpg").convert()


    self.rect  = self.Image.get_rect(topleft = (x,y))


  
  def draw(self, game_window):
    self.Image = pygame.transform.scale(self.Image,(20,20))
    game_window.blit(self.Image, (self.rect.x, self.rect.y))


pygame.init()

clock = pygame.time.Clock()
screenWidth = 400
screenHeight = 400
game_window = pygame.display.set_mode((screenWidth,screenHeight))
player = Player(200,200)
map = Map()
enemy = Enemy(250,250)
leave = False
while not leave:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit() 
      running = False



  player.handle_keys(screenHeight,screenWidth)

  game_window.fill((0,0,0))
  map.draw(game_window,screenHeight,screenWidth)
  #enemy.draw(game_window)
  player.draw(game_window)
 
  pygame.display.update()
  pygame.display.flip()
  time_passed = clock.tick() / 1000

  


pygame.quit()
quit()
python pygame
1个回答
2
投票

玩家的移动取决于地图的大小。 “玩家”的

x
y
属性存储地图上的位置,并且仅限于地图的大小 (
map_size
)。玩家总是被绘制在屏幕的中心,除了靠近地图边界的地方:

class Player():
    def __init__(self, x, y):
        self.Image = pygame.image.load("myAvatar.png").convert()
        self.x = 200
        self.y = 200
  
    def handle_keys(self, map_size):
        key = pygame.key.get_pressed()
        self.x += (key[K_RIGHT] - key[K_LEFT]) * 500 * time_passed
        self.y += (key[K_DOWN] - key[K_UP]) * 500 * time_passed
        self.x = max(0, min(map_size[0]-20, self.x))
        self.y = max(0, min(map_size[1]-20, self.y))
    
    def draw(self, game_window, map_size):
        window_size = game_window.get_size()
        center = window_size[0] // 2, window_size[0] // 2
        
        pos = [self.x, self.y]
        for i in range(2):
            if center[i] < pos[i] <= map_size[i]-center[i]:
                pos[i] = center[i]
            elif pos[i] > map_size[i] - center[i]: 
                pos[i] = window_size[i] - map_size[i] + pos[i]
        game_window.blit(self.Image, (int(pos[0]), int(pos[1])))

玩家在地图上的位置以窗口为中心:

class Map():
    def __init__(self):
        self.Image = pygame.image.load("test2.png").convert()

    def draw(self, game_window):
        window_size = game_window.get_size()
        map_size = self.Image.get_size()
        x = max(0, min(map_size[0] - window_size[0], player.x - 200))
        y = max(0, min(map_size[1] - window_size[1], player.y - 200))
        game_window.blit(self.Image, (-x, -y))

应用循环:

leave = False
while not leave:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            leave = True

    player.handle_keys(map.Image.get_size())

    game_window.fill((0,0,0))
    map.draw(game_window)
    #enemy.draw(game_window)
    player.draw(game_window, map.Image.get_size())
    
    pygame.display.update()
    pygame.display.flip()
    time_passed = clock.tick() / 1000

pygame.quit()

© www.soinside.com 2019 - 2024. All rights reserved.