2013-10-03 112 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