在pygame中是否有一个简单的公式来设置相机跟随玩家?

问题描述

我尝试使用公式:xoffset = screen_width/2 - player_x,yoffset = screen_height/2 - player_y 但它不起作用。我还尝试根据玩家的动作更改我的 xoffset 和 yoffset,但实际上很容易找到一些错误。 所以我想使用一个工作公式来考虑玩家的位置而不是移动。 我是编码初学者,这是我的第一个电子游戏。 感谢您的帮助。

import pygame
from pygame.locals import *
import time

pygame.init()

#Screen
screen_x = 1200
screen_y = 675
screen = pygame.display.set_mode((screen_x,screen_y))
pygame.display.set_caption('Portugheesen game')
zoom = 1
celle_size = 15*zoom

def draw_grid():
    for linea in range(0,80):
        pygame.draw.line(screen,(255,255,255),(0,linea * celle_size),(screen_x,linea *         celle_size))
        pygame.draw.line(screen,(linea * celle_size,0),screen_y))

#Images
background1 =     pygame.image.load("C:/Users/claudio/Desktop/Immagini_portugheesen/jungle_background.jpg")
background1 = pygame.transform.scale(background1,(1200,755))
grass_block = pygame.image.load("C:/Users/claudio/Desktop/Immagini_portugheesen/grass_block.jpg")
dirt_block = pygame.image.load("C:/Users/claudio/Desktop/Immagini_portugheesen/dirt_block.png")

world_celle1 = [
[1,1,1],[1,2,3,2],]

class Camera():

    def __init__(self):
        self.x = 0
        self.y = 0

    def update(self):
        self.x = 0
        self.y = 0


class World():

    def __init__(self,world_celle):
        self.lista_celle = []

        riga_c = 0
        for riga in world_celle:
            colonna_c = 0
            for cella in riga:
                if cella == 1:
                    img = pygame.transform.scale(grass_block,(celle_size,celle_size))
                    img_rect = img.get_rect()
                    img_rect.x = colonna_c * celle_size
                    img_rect.y = riga_c * celle_size
                    cella = [img,img_rect]
                    self.lista_celle.append(cella)
                if cella == 2:
                    img = pygame.transform.scale(dirt_block,img_rect]
                    self.lista_celle.append(cella)
                colonna_c = colonna_c + 1
            riga_c = riga_c + 1

    def draw(self):
        for i in self.lista_celle:
            i[1].x = i[1].x - camera.x
            i[1].y = i[1].y - camera.y
            screen.blit(i[0],i[1])


class Player:

    def __init__(self,x,y):
        img = pygame.image.load("C:/Users/claudio/Desktop/Immagini_portugheesen/player.jpg")
        img = pygame.transform.scale(img,(15,30))
        self.image = img
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.vel_y = 0
        self.jump_delay = 0
        self.jump_count = 0
        self.stop_jump = False

    def update(self):
        dx = 0
        dy = 0

        #Keypressed
        key = pygame.key.get_pressed()
        if(key[pygame.K_LEFT]):
            dx = dx - 5
        if(key[pygame.K_RIGHT]):
            dx = dx + 5
        if(key[pygame.K_SPACE] or key[pygame.K_UP]):
            if(pygame.time.get_ticks() > self.jump_delay):
                self.vel_y = self.vel_y - 20
                #self.jump_count = self.jump_count + 1
                #if(self.jump_count == 2):
                    #self.stop_jump = True
            self.jump_delay = pygame.time.get_ticks() + 50


        #Gravity
        self.vel_y = self.vel_y + 1
        if(self.vel_y >= 10):
            self.vel_y = 10
        dy = dy + self.vel_y

        #Collisions
        for cella in world1.lista_celle:
            if cella[1].colliderect(self.rect.x + dx,self.rect.y,self.image.get_width(),self.image.get_height()):
                dx = 0
            if cella[1].colliderect(self.rect.x,self.rect.y + dy,self.image.get_height()):
                if (self.vel_y > 0):
                    dy = cella[1].top - self.rect.bottom
                elif (self.vel_y < 0):
                    dy = cella[1].bottom - self.rect.top
                    self.vel_y = 0

        self.rect.x += dx
        self.rect.y += dy
        screen.blit(self.image,self.rect)




#Main loop
run = True
player1 = Player(100,screen_y - 400)
world1 = World(world_celle1)
clock = pygame.time.Clock()
camera = Camera()
while run:
    clock.tick(60)
    screen.blit(background1,0))
    player1.update()
    #draw_grid()
    camera.update()
    world1.draw()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    pygame.display.update()

pygame.quit()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)