使用我的播放器型号移动相机会有什么好的浓缩方式?

时间:2017-09-28 21:48:39

标签: python pygame

我是python和pygame的初学者。将相机移动到游戏中会有什么好的浓缩方式? (当播放器始终位于屏幕中间时)我尝试过多种内容,但它们并没有像我希望的那样工作。我再次成为新人,如果这是一个愚蠢的问题,请告诉我。提前感谢任何困扰回答的人。

import pygame
import random
import time

pygame.init()

display_width = 1200
display_height = 1000

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)


player_width = 24
player_height = 42

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Random RPG')
clock = pygame.time.Clock()
PMF = ("PlayerModel_Forward.png")

playerSprt = pygame.image.load(PMF)

def player(x,y):
    gameDisplay.blit(playerSprt,(x,y))

def enemy1(enemy1x, enemy1y, enemy1w, enemy1h, color):
    pygame.draw.rect(gameDisplay, color, [enemy1x, enemy1y, enemy1w, enemy1h])

def death():
    message_display('Game Over')

    def text_objects(text, font):
        textSurface = font.render(text,
                              True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf', 115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()
    time.sleep(2)
    pygame.display.update()
    game_loop()

def frames(fps):
    font = pygame.font.SysFont(None, 25)
    text = font.render(str(fps), True, black)
    gameDisplay.blit(text,(0,0))

def xcoord(x_coord):
    font = pygame.font.SysFont(None, 25)
    text = font.render((str(x_coord)), True, black)
    gameDisplay.blit(text,(display_width/2,0))

def ycoord(y_coord):
    font = pygame.font.SysFont(None, 25)
    text = font.render((str(y_coord)), True, black)
    gameDisplay.blit(text,(display_width/2,25))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def game_loop():
    x = (display_width/2)
    y = (display_height/2)

    x_change = 0
    y_change = 0

    enemy1_startx = random.randrange(0, display_width)
    enemy1_starty = random.randrange (0, display_height)
    enemy1_x_change = random.randrange (-5,5)
    enemy1_y_change = random.randrange (-5,5)
    enemy1_width = 100
    enemy1_height = 100



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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_DOWN:
                    y_change = 5
                elif event.key == pygame.K_UP:
                    y_change = -5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
                    y_change = 0

        x+= x_change
        y+= y_change

        Fps = 30

        #Logic
        if gameExit == True:
            pygame.quit()
            quit()

        if x > display_width - player_width or x < 0:
            death()

        if y > enemy1_starty and y < enemy1_starty + enemy1_width or y+player_width > enemy1_starty and y + player_width < enemy1_starty + enemy1_width:

            if x > enemy1_startx and x < enemy1_startx + enemy1_width or x+player_width > enemy1_startx and x + player_width < enemy1_startx + enemy1_width:

                death()
                time.sleep(2)
                game_loop()


        if enemy1_starty > display_height or enemy1_starty < 0 or enemy1_startx > display_width or enemy1_startx < 0:
            enemy1_starty = random.randrange (0, display_height)
            enemy1_startx = random.randrange(0, display_width)
            enemy1_x_change = random.randrange (-5,5)

            enemy1_y_change = random.randrange (-5,5)




        #Drawing:
        gameDisplay.fill(black)
        player(x,y)
        enemy1 (enemy1_startx, enemy1_starty, enemy1_width, enemy1_height, red)
        enemy1_starty += enemy1_y_change
        enemy1_startx += enemy1_x_change
        frames((Fps))
        xcoord (x)
        ycoord (y)

        pygame.display.update()
        clock.tick(Fps)

game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:0)

你做得很好!我认为你可能采取了错误的方法,因为不是实际的相机移动(这实际上很难实现),通过移动屏幕上的每个其他元素,可以达到相同的效果。因此,当用户按下右键而不是向右移动时,向左移动敌人!:相关代码:

#include "stdafx.h"
#include "windows.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"

HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink, LPCWSTR lpszDesc) 
{ 
    HRESULT hres; 
    IShellLinkW* psl; 

    // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
    // has already been called.
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (LPVOID*)&psl); 
    if (SUCCEEDED(hres)) 
    { 
        IPersistFile* ppf; 

        // Set the path to the shortcut target and add the description. 
        psl->SetPath(lpszPathObj); 
        psl->SetDescription(lpszDesc); 

        // Query IShellLink for the IPersistFile interface, used for saving the 
        // shortcut in persistent storage. 
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); 

        if (SUCCEEDED(hres)) 
        { 
            // Save the link by calling IPersistFile::Save. 
            hres = ppf->Save(lpszPathLink, TRUE); 
            ppf->Release(); 
        } 

        psl->Release(); 
    } 

    return hres; 
}

void _tmain(int argc, TCHAR *argv[])
{
    if (argc > 3)
        CreateLink(argv[1], argv[2], argv[3]);
}