你如何让相机跟随播放器(滚动)?

时间:2016-01-02 13:41:51

标签: python python-3.x scroll pygame

很抱歉再问一次,我之前已经问过这个问题,但我根本无法弄清楚......

我正在制作一个鸟瞰游戏,我终于得到了所有的碰撞,能够画出水平和其他什么,但我现在得到了这个问题我在哪里对于如何让相机跟随播放器,我几乎一无所知。

任何帮助都将非常感谢,提前感谢。

以下是完整代码:

import pygame
import time

pygame.init()

display_width = 1200
display_height = 800

red = (255,0,0)
green = (0,255,0)

black = (0,0,0)

x = 0
y = 0

block_width = 1
block_height = 1

block_size = 1

clock = pygame.time.Clock()

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("My Game")


def drawBlock(block_width,block_height,x,y):
    pygame.draw.rect(gameDisplay, red, [x,y,block_width,block_height])

def gameLoop(x,y):

    collisionArray = []

    canMoveUp = True
    canMoveDown = True
    canMoveLeft = True
    canMoveRight = True

    appendBlockXY = True

    P_count = 0
    P_countFunction = True

    L_count = 0

    W_count = 0

    y_change = 0
    x_change = 0

    player_movement = 5

    playerSize = 50

    pX = display_width/2 
    pY = display_height/2

    gameExit = False

    calculateBlockX = True
    calculateBlockY = True

    while not gameExit:


        level =  ["W","W","W","W","W","W","W","W","W","W","W","W",
                  "P","P","P","P","P","P","P","P","P","P","P","P","L",
                  "P","N","N","N","N","N","N","N","N","N","N","P","L",
                  "P","N","N","N","N","N","N","N","N","P","N","P","L",                                       
                  "P","N","N","N","N","N","N","N","N","N","N","P","L",                                     
                  "P","N","N","N","N","N","P","N","N","N","N","P","L",
                  "P","N","N","N","N","N","P","N","N","N","N","P","L",
                  "P","N","N","N","N","N","N","N","N","N","N","P","L",
                  "P","N","N","N","N","N","N","N","N","N","N","P","L",
                  "P","N","N","N","P","N","N","N","N","N","N","P","L",
                  "P","P","P","P","P","P","P","P","P","P","P","P","L","S"]    
#EVENT HANDLING

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if canMoveUp:
                    if event.key == pygame.K_UP:
                        y_change = -player_movement

                if canMoveDown:
                    if event.key == pygame.K_DOWN:
                        y_change = player_movement

                if canMoveLeft:
                    if event.key == pygame.K_LEFT:
                        x_change = -player_movement

                if canMoveRight:
                    if event.key == pygame.K_RIGHT:
                        x_change = player_movement

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN: 
                    y_change = 0
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        pX += x_change
        pY += y_change

        gameDisplay.fill(black)


#CALCULATING THE SIZE OF THE BLOCKS SO THEY FIT W/ A VARIETY OF RESOLUTIONS


        if calculateBlockX:

            for element in level:
                if element == "W":
                    W_count += 1
                if element == "S":
                    block_width = (display_width/W_count) 
                    calculateBlockX = False

        if calculateBlockY:

            for element in level:
                if element == "L":
                    L_count += 1
                if element == "S":
                    block_height = (display_height/L_count)
                    calculateBlockY = False


#COUNTING HOW MANY BLOCKS THERE ARE IN THE LEVEL ARRAY (P)

        if P_countFunction:
            for element in level:
                if element == "P":
                    P_count += 1
                if element == "S":
                    print(P_count)
                    P_countFunction = False


#ALL THE X AND Ys OF ALL THE BLOCKS IN THE LEVEL, AND ACTUALLY DRAWING THEM

        blockXY = []

        for element in level:
            if element == "N":
                 x += block_width
            if element == "L":                                              
                 y += block_height
                 x = 0
            if element == "P": 
                drawBlock(block_width,block_height,x,y)                
                blockXY.append(x)
                blockXY.append(y)
                if appendBlockXY:
                    if len(collisionArray) > P_count:
                        del(collisionArray[P_count])
                        print(collisionArray)
                        appendBlockXY = False
                    collisionArray.append(blockXY)
                blockXY = []
                x += block_width
            if element == "S":
                y = 0

#COLLISION DETECTION

        canMoveUp = True
        canMoveDown = True
        canMoveLeft = True
        canMoveRight = True

        for XnY in collisionArray:

            if pY > XnY[1] and pY < XnY[1] + block_height or pY +  playerSize > XnY[1] and pY < XnY[1] + block_height:

                if pX - block_width == XnY[0]:
                    canMoveLeft = False
                    x_change = 0

            if pY > XnY[1] and pY < XnY[1] + block_height or pY +  playerSize > XnY[1] and pY < XnY[1] + block_height:

                if pX + playerSize == XnY[0]:
                    canMoveRight = False
                    x_change = 0

            if pX > XnY[0] and pX < XnY[0] + block_width or pX + playerSize > XnY[0] and pX < XnY[0] + block_width:

                if pY - block_height == XnY[1]:
                    canMoveUp = False
                    y_change = 0

            if pX > XnY[0] and pX < XnY[0] + block_width or pX + playerSize > XnY[0] and pX < XnY[0] + block_width:

                if XnY[1] - playerSize == pY:
                    canMoveDown = False
                    y_change = 0

        pygame.draw.rect(gameDisplay, green, [pX,pY,playerSize,playerSize])

        clock.tick(120)

        pygame.display.update()


    pygame.quit()
    quit()

gameLoop(x,y)        
pygame.display.update()

0 个答案:

没有答案