2013-10-03 55 views
1

我正在製作一個8位風格的平臺遊戲。由於僞重力,玩家會下降並獲得速度,但他會下降幾個像素到地面。在沒有重力的情況下,他會降落在地面上,但不會掉下來,但它會持續下降。當你在地上時,你可以上去,但是當你放鬆時他會掉下來。他不會下來,所以現在不是問題。任何幫助,將不勝感激。使用重力時Python-Pygame精靈彈跳

玩家類/文件。

import pygame,sys 
from pygame.locals import * 
class Player: 
    x=0 
    y=0 
    offset = 5 
    L=False 
    R=False 
    U=False 
    D=False 
    image = None 
    gravity = .25 
    velocity = offset 
    objectDict = None #this si the list of the current objects so that collision can be check with every 
    #object.. get updated every loop to keep a accurate check of locations 
    rect = None 
    grav = True #TODO use this to check if we are paying attention to the gravity 
    def __init__(self,x,y): 
     self.x = x 
     self.y = y 
     self.image = pygame.image.load('Resources/Pics/player.png') 



    def draw(self,DISPLAY): 
     #print('draw will go here') 
     imgRect = self.image.get_rect() 
     imgRect.midleft = (self.x,self.y) 
     self.rect = imgRect 
     DISPLAY.blit(self.image, imgRect) 
     #and now im here 

    def checkCollide(self,otherRect): 
     return self.rect.colliderect(otherRect) 

    def checkCollideAll(self): 
     if(self.objectDict != None): 
      # print(len(self.objectDict)) 
     #  for x in range(1,len(self.objectDict)): 
     #   newb = self.checkCollide(self.objectDict[x].getRect()) 
     #   print(self.objectDict[x].getRect()) 
     #  if(newb): 
     #   return True 
     # return False 
      collideNum = self.rect.collidelist(self.objectDict) 
      if(collideNum == -1): 
       return False 
      else: 
       return True 

    def willCollideBelow(self): 
     if(self.objectDict): 
      checkRect = (self.x,(self.y),self.image.get_size()) 
      collideNum = self.rect.collidelist(self.objectDict) 
      if collideNum == -1: 
       return False 
      else: 
       return True 


    def objUpdate(self,dict): 
     self.objectDict = dict 

    def getRect(self): 
     return self.rect 

    def update(self): 
     # while(self.checkCollideAll()): 
     #  print('while happened') 
     #  self.y -= self.offset 
     #  imgRect = self.image.get_rect() 
     #  imgRect.midleft = (self.x,self.y) 
     #  self.rect = imgRect 
     # print(self.willCollideBelow()) 
     if not self.willCollideBelow(): 
      self.D = True 
      # print('will fall') 
     else: 
      self.D = False 

     if self.U == True: 
      self.y -= self.offset 

     if self.D == True: 
       self.y += self.velocity 
       if not self.velocity >= 9.8: 
        self.velocity += self.gravity 
     else: 
      self.velocity = self.offset 
     if self.L == True: 
       self.x -= self.offset 

     if self.R == True: 
       self.x += self.offset 
+0

你的意思是說你的角色正在下降,以至於他與地面相交?如果是這樣的話,那是因爲在一個時間步中,他的距離比他高於地面的距離更遠,並且看起來你沒有任何代碼來解決這個交集。 – kevintodisco

回答

2

您沒有提供一個運行的例子,你的代碼是難以閱讀(帕斯卡的情況下,很多不必要的括號),但這裏是我的猜測:

在你willCollideBelow功能,你檢查你打在播放器下方的對象:

def willCollideBelow(self): 
     if(self.objectDict): 
      checkRect = (self.x,(self.y),self.image.get_size()) 
      collideNum = self.rect.collidelist(self.objectDict) 
      if collideNum == -1: 
       return False 
      else: 
       return True 

,而不是僅僅返回TrueFalse,返回對象(或對象的索引),你實際碰撞:

現在
def will_collide_below(self): 
     if(self.objectDict): 
      # using 'collidelistall' would be better, but that's another topic 
      return self.rect.collidelist(self.objectDict) 

,你知道哪個對象的球員碰撞時,可以調整播放器的垂直位置:

ground_i = self.will_collide_below() 
if ground_i: 
    ground = self.objectDict[ground_i] 
    self.velocity = 0 
    self.rect.bottom = ground.top # or self.y = ground.top 

你會明白我的意思。


一些更多的注意事項:

您可以使用不同的變量來存儲播放器的位置(我看到xyrectimgRect)。它將使你的代碼簡單了很多,如果你只使用一個Rect存儲位置:

class Player: 
    ... 
    def __init__(self,x,y): 
     self.image = pygame.image.load('Resources/Pics/player.png') 
     self.rect = self.image.get_rect(midleft=(x,y)) 

    def draw(self, display): 
     display.blit(self.image, self.rect) 

    def update(self): 
     ... 
     if self.L: # no need to check == True 
      self.rect.move_ip(-self.offset) 

     if self.R: # simply use move_ip to alter the position 
      self.rect.move_ip(self.offset) 

您還可以使用一堆類變量,你真的應該使用實例變量,像rectLRUD

+0

謝謝你,更有意義。另外,camelcase和括號是因爲我們在VB和Java方面有經驗,在移植到Python時徹底破壞了所有的邏輯格式。 – ddaniels