2012-12-18 40 views
1

所以我一直在讓精靈留在屏幕邊界的問題。我得到它與一個簡單的矩形(0,0,16,16),但我似乎無法讓它與一個精靈在屏幕上blit工作。爲了讓我的精靈在屏幕上保持不變,我需要改變什麼?我今天才剛剛開始使用類來編程代碼,所以任何輸入都會被讚賞和幫助。Pygame sprite clamp isse

import pygame 
from pygame.locals import * 
from pygame import Color 


class Game(): 
    """ Lets try to get this going by simple steps 
    One by one. First step, lets figure how to make a class 
    that can do the display stuff. Lord have mercy on my soul""" 

    def __init__(self, wi=256, hi=224, multii=3): 
     """Initialization""" 
     pygame.init() 
     self.runGame = True 
     self.width  = wi*multii 
     self.height  = hi*multii 
     self.spritesize = 16*multii 
     self.clock  = pygame.time.Clock() 
     self.fps  = self.clock.get_fps() 
     self.screen  = pygame.display.set_mode((self.width, self.height)) 
     self.kl   = [] 
     self.walk  = [0, 0] 
     self.speed  = multii*1.5 
     self.x,self.y = self.width/2, self.height/2 
     self.playerSpr = pygame.image.load('images/'+'link1.png').convert_alpha() 
     self.playerRec = Rect(self.playerSpr.get_rect()) 


    def mainLoop(self): 
     """Loop through the main game routines 
     1. Drawing 2. Input handling 3. Updating 
     Then loop through it until user quits""" 
     while self.runGame: 
      self.clock.tick(60) 
      self.events() 
      self.draw() 


    def events(self): 
     """Time to handle some events""" 
     for e in pygame.event.get(): 
      if (e.type == pygame.QUIT) or (e.type == KEYDOWN and e.key == K_ESCAPE): 
       self.runGame = False 
       break        
      if e.type==KEYDOWN:  
       if e.key==pygame.K_a: self.kl.append(1) 
       if e.key==pygame.K_d: self.kl.append(2) 
       if e.key==pygame.K_w: self.kl.append(3) 
       if e.key==pygame.K_s: self.kl.append(4)    
      if e.type==pygame.KEYUP: 
       if e.key==pygame.K_a: self.kl.remove(1)    
       if e.key==pygame.K_d: self.kl.remove(2) 
       if e.key==pygame.K_w: self.kl.remove(3)    
       if e.key==pygame.K_s: self.kl.remove(4) 

      if self.kl[-1:]==[1]: self.walk=[-self.speed, 0] 
      elif self.kl[-1:]==[2]: self.walk=[ self.speed, 0] 
      elif self.kl[-1:]==[3]: self.walk=[0,-self.speed] 
      elif self.kl[-1:]==[4]: self.walk=[0, self.speed] 
      else:     self.walk=[0, 0] 

     self.x+=self.walk[0] 
     self.y+=self.walk[1] 


    def draw(self): 
     """Draw and update the main screen""" 
     self.fps = self.clock.get_fps() 
     self.screen.fill(Color('purple')) 
     #print self.screen.get_rect() 
     #print player_rect 
     self.playerSpr.clamp_ip(self.screen.get_rect()) 
     #pygame.draw.rect(self.screen, (255, 255, 255), self.playerrect) 
     self.screen.blit(self.playerSpr, (self.x,self.y), self.playerRec) 
     pygame.display.set_caption('Grid2. FPS: '+str(self.fps)) 
     pygame.display.update() 



game = Game() 
game.mainLoop() 
+0

使用正常的blit。如果精靈在屏幕外,它會自動部分閃爍或不閃爍。 'kl'在做什麼?也許你想要keystate而不是你的運動類型的關鍵事件。 – ninMonkey

+0

嗯,我不真正遵循'正常blit'的意思。我希望精靈永遠不會離屏或隱藏。我希望它停在屏幕矩形的邊界上(即使我嘗試用移動鍵將它從屏幕上移開),但仍然顯示。 'kl'和移動代碼是一種特殊的方式,既可以在記住以前的鍵的同時優先使用最近的移動鍵,只要它們仍然處於按下狀態。 'pygame.key.get_pressed()'不會這樣做一貫afaik。 –

回答

1

爲什麼不使用playerRec跟蹤您的播放器的位置,而不是額外的xy的屬性?

我建議也使用move方法(或move_ip):

def events(self): 
    for e in pygame.event.get(): 
     ... 

    self.playerRec.move_ip(*self.walk) # instead of self.x+=self.walk[0]/self.y+=self.walk[1] 

def draw(self): 
    ... 

    # probably do this right after 'move_ip' 
    self.playerRec.clamp_ip(self.screen.get_rect()) 

    # note that 'blit' accepts a 'Rect' as second parameter 
    self.screen.blit(self.playerSpr, self.playerRec) 

一個側面說明:您應該考慮使用Sprite,因爲它基本上結合了ImageRect

+0

謝謝,我將進一步研究pygame中的精靈。我也會在那裏嘗試你的提示。欣賞這個例子。 –

+0

這是做到了。非常感謝你向我指出這一點。 –

1

兩件事情:

  1. 當談到關閉屏幕你是不是停止精靈的運動。 做一個移動功能,將會得到一個方向,並將決定它是否可以移動更多的一邊。這樣,當精靈的右側將成爲屏幕時,您將不會再向右移動。

  2. 既然你把你的方向鍵放在一個像棧一樣工作的列表中,你只能得到每個按鍵1個方向。如果你也想斜走要麼使兩個列表一個兩個方向或使用更簡單的方法,如這樣的:

    if KEYDOWN == K_LEFT: direction_x = -1 
    if KEYUP == K_LEFT AND direction_x == -1: direction_x = 0 
    

    每個鍵的做到這一點。

+0

我所擁有的方法採用了'pygame.key.get_pressed()'和'KEYUP'' KEYDOWN'的最好部分,因爲它們本身都沒有優先考慮。我其實不想移動對角線,所以這不是一個問題。我的問題是如何讓'clamp_ip'工作我的移動精靈。我可以讓它適用於精靈矩形,但是我只是讓矩形本身移動而不是精靈,所以當我移動時,矩形滑過精靈並且精靈不見了,否則我嘗試使用精靈本身到'clamp_ip',但它不夾住,仍然飛離屏幕。 –

+0

clamp_ip只適用於矩形,不適用於位圖 –

+0

哦,我明白了!所以我會以某種方式迫使精靈堅持正直,然後夾緊直腸的工作?我曾嘗試過#1,但沒有得到它的正常工作。仍然在尋找一種標準的方式,人們這樣做。謝謝您的幫助! –