2015-05-10 56 views
0

我有一個pygame的clamp_ip()方法的問題。我似乎無法弄清楚問題所在。我爲我的相機有一個矩形,並且叫player.clamp_ip(camera),但它無法鉗位。我的代碼在github上。我也使用Python 3.4運行最新的pygame。另一個令人擔憂的是它沒有引發任何錯誤。Pygame Clamp-IP小​​故障?

Github上鍊接:https://github.com/Restioson/SquirrelMan

import pygame #3rd-party libraries 
import os, sys #Standard libraries 
importpath = os.path.split(os.getcwd()) 
importpath = os.path.join(importpath[0], "libraries") 
sys.path.append(importpath) #Allow us to import the library 

import resourcemanager #Project-specific library 
import player as player_ 
import pygnamelib #3rd-party libraries 
from logHelper import * 



def run(screenRect, gameScreen, boundaryRect): 
    camera = pygame.rect.Rect(0, 0, 640, 480) 
    def eventhandler(player, textures, gamevariables, event, keyinput, gameScreen, fx): #Handle events 
     returntext = False 

     ##Player movement 

     if keyinput[pygame.K_ESCAPE] or event.type == pygame.QUIT: #If the player presses escape, exit the game 
      returntext = 'End' 

     if keyinput[pygame.K_UP] and (keyinput[pygame.K_LEFT] or keyinput[pygame.K_RIGHT]): 
      player.rect.y = player.rect.y - player.SPEED/2 
      camera.y = camera.y - player.SPEED 
      log("Moved 1 block up") 

      if keyinput[pygame.K_LEFT]: 
       player.rect.x = player.rect.x - player.SPEED/2 
       camera.x = camera.x - player.SPEED 
       log("Moved 1 block left") 

      elif keyinput[pygame.K_RIGHT]: 
       player.rect.x = player.rect.x + int(player.SPEED/2) 
       camera.x = camera.x + player.SPEED 
       log('Moved 1 block right') 


     elif keyinput[pygame.K_UP]: 
      player.rect.y = player.rect.y - player.SPEED 
      camera.y = camera.y - player.SPEED * 2 
      log("Moved 1 block up") 

     if keyinput[pygame.K_DOWN] and (keyinput[pygame.K_LEFT] or keyinput[pygame.K_RIGHT]): 
      player.rect.y = player.rect.y + player.SPEED/2 
      camera.y = camera.y + player.SPEED 
      log('Moved one block down') 
      if keyinput[pygame.K_LEFT]: 
       player.rect.x = player.rect.x - player.SPEED/2 
       camera.x = camera.x - player.SPEED 
       log("Moved 1 block left") 

      elif keyinput[pygame.K_RIGHT]: 
       player.rect.x = player.rect.x + player.SPEED/2 
       camera.x = camera.x + player.SPEED 
       log('Moved 1 block right') 

     elif keyinput[pygame.K_DOWN]: 
      player.rect.y = player.rect.y + player.SPEED 
      camera.y = camera.y + player.SPEED *2 
      log('Moved one block down') 

     if keyinput[pygame.K_LEFT]: 
      player.rect.x = player.rect.x - player.SPEED 
      camera.x = camera.x - player.SPEED*2 
      log("Moved 1 block left") 

     if keyinput[pygame.K_RIGHT]: 
      player.rect.x = player.rect.x + player.SPEED 
      camera.x = camera.x + player.SPEED*2 
      log('Moved 1 block right') 



     ##Other Keybindings 

     if keyinput[pygame.K_RETURN]: 
      if player.rect.colliderect(textures.carrotfieldrect) and gamevariables['blitcarrotfield']: 
       gamevariables['blitcarrotfield'] = False 
       player.inventory['Carrots'] = 20 

     if keyinput[pygame.K_i]: 
      log('Inventory') 
      printtext(gameScreen, str(player.inventory).replace('{','').replace('}','').replace(',',';').replace('"',''), textures,   gamevariables['blitcarrotfield'], player) 
      # pygame.mixer.music.play(fx.openinv) 

     if keyinput[pygame.K_F11]: 
      pygame.display.toggle_fullscreen() 

     if returntext != False: 
      return returntext 



    ##Blitting 


    def blit(displayScreen, vardict, Textures, Player): 
     if vardict['surroundings'] == 'Start': 
      displayScreen.blit(Textures.map,(0-int(camera.x),0 - gamevariables['cameray'])) 
      displayScreen.blit(Textures.smithy,(Textures.smithyrect.x - gamevariables['camerax'], Textures.smithyrect.y - gamevariables['cameray'])) 
      displayScreen.blit(Textures.faldorhouse,(Textures.faldorhouserect.x - gamevariables['camerax'], Textures.faldorhouserect.y - gamevariables['cameray'])) 
      if vardict['blitcarrotfield'] == True: 
       displayScreen.blit(Textures.carrotfield,(Textures.carrotfieldrect.x - gamevariables['camerax'], Textures.carrotfieldrect.y - gamevariables['cameray'])) 
      else: 
       displayScreen.blit(Textures.carrotfield_depleted,(Textures.carrotfieldrect.x - gamevariables['camerax'], Textures.carrotfieldrect.y - gamevariables['cameray'])) 
      displayScreen.blit(Textures.brillhouse,(Textures.brillhouserect.x - gamevariables['camerax'],Textures.brillhouserect.y - gamevariables['cameray'])) 
      displayScreen.blit(Textures.stable,(Textures.stablerect.x- gamevariables['camerax'], Textures.stablerect.y - gamevariables['cameray'])) 
      displayScreen.blit(Player.player,Player.rect) 
      pygame.display.flip() 
     if vardict['surroundings'] == 'End': 
      displayScreen.blit(Textures.map,(0,0)) 
      displayScreen.blit(Player.player,(Player.rect.x,player.rect.y)) 


    ##Setup 

    pack = "Default-Texture-Pack" #DEBUGGING PURPOSES 
    log('Using texturepack '+pack) 
    log('Texturepack is located in "'+'./rescources/textures/'+pack+'.zip'+'"') 
    resourcemanager.setpack(pack) #Load texturepack 
    pygame.key.set_repeat(1,10)  #Set holdingdown key to continous movemont 
    textures = resourcemanager.textures('FaldorFarm') #Copy 'texturepack' object 
    test = resourcemanager.testpack(textures, os.path.join(os.getcwd(),'resources','packs',pack+'.zip')) 
    result = test[0] 
    if result == True: 
     try: 
      F = open(os.path.join(os.getcwd(), 'resources', 'packs',pack+'.zip')) 
      F.close() 
      log('Valid texturepack, continuing with game setup...') 
     except: 
      log('Texturepack invalid, using default texturepack located in "'+'./rescources/default/Default-Texture-Pack.zip"') 
    if result == False: 
     log('Texturepack invalid, using default texturepack located in "'+'./rescources/default/Default-Texture-Pack.zip"') 
     resourcemanager.setpack('Default-Texture-Pack') 
     textures = resourcemanager.textures() 
    player = player_._player(pack) #Create the player 
    player.inventory['Stuff'] = '' 
    sounds = resourcemanager.soundfx(pack) 
    log("Sound's loaded from ./rescources/sounds/") 

    log('Job "Textures & Objects" completed successfully') 

    #Game Variables 
    gamevariables = { 'blitcarrotfield':True, 'surroundings':'Start', 'endofgame':False, 'camerax': 320, 'cameray':240 } #Create the variable dictionary 

    #Storyline... 
    printtext(gameScreen, 'Once there was a storyline', textures, gamevariables['blitcarrotfield'], player, timeout=5) 

    log('Game Setup successfully completed\n') 




    ##Mainloop 
    while True: 

     for event in pygame.event.get(): #Event loop 
      events = eventhandler(player, textures, gamevariables, event, pygame.key.get_pressed(), gameScreen, sounds) #Handle events 
      if events == 'End': 
       gamevariables['endofgame'] = True 
     blit(gameScreen, gamevariables, textures, player) #Blit objects 
     gamevariables['camerax'] = camera.x 
     gamevariables['cameray'] = camera.y 

     player.rect.clamp_ip(boundaryRect) 
     player.rect.clamp_ip(screenRect) 
     player.rect.clamp_ip(camera) 
     camera.clamp_ip(boundaryRect) 
     pygame.display.flip() 

     if gamevariables['endofgame']: #If the game has ended: 
      pygame.quit() 
      log('----- Game end -----') 
      stoplog = True 
      logfile.close() 
      sys.exit(0) 

在此先感謝!

+0

請在這裏發佈相關代碼,讓人們閱讀這種方式稍微容易一些,而不是打開github鏈接。 – SuperBiasedMan

+0

SuperBiasedMan,我無法弄清楚縮進... – Restioson

+0

沒關係......找到它了! – Restioson

回答

0

我用一個矩形將相機可視化,它不在屏幕上。