我遇到了pygame的clamp_ip()方法的问题。我似乎无法弄清问题是什么。我的相机有一个矩形,并调用player.clamp_ip(camera)
,但它无法钳制。我的代码在github上。我也用Python 3.4运行最新的pygame。另一个令人担忧的事情是它没有错误。
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 :(得分:0)
我用一个矩形可视化相机,它不在屏幕上。