2017-02-04 41 views
0

我正在制作一款游戏,并且在该游戏中,只要玩家的x>>屏幕宽度,我希望玩家的x速度= 0。但是当我尝试时,它没有工作。碰撞代码对我来说看起来很好,我认为当我打电话给player.collision()时有一些问题。与墙碰撞并不会停止对象的速度

PS。对象的速度等于0时,对象x为< = 0,但不是当X是> = displayW速度不等于0

问题是下#collision to walls

PYTHON

# IMPORTS 
import pygame, random; 

# GLOBALS 
global screen, displayW, displayH; 
global clock, FPS; 
global end, food, player; 

# SETGLOBALVALUES 
def setGlobalValues(): 
    global screen, displayW, displayH; 
    global clock, FPS; 
    global end, food, player; 

    displayW = 800; 
    displayH = 600; 
    screen = pygame.display.set_mode((displayW, displayH)); 

    clock = pygame.time.Clock(); 
    FPS = 60; 

    end = False; 
    food = Food(); 
    player = Player(); 

# MAIN 
def main(): 
    pygame.init(); 

    setGlobalValues(); 
    setup(); 
    gameLoop(); 
    quitGame(); 

# GAMELOOP 
def gameLoop(): 
    global end, player; 

    while(not end): 
     for event in pygame.event.get(): 
      # ONCLICK QUIT 
      if(event.type == pygame.QUIT): 
       end = True; 

      # KEYDOWN 
      if(event.type == pygame.KEYDOWN): 
       if(event.key == pygame.K_LEFT): 
        player.velX -= 1; 
       if(event.key == pygame.K_RIGHT): 
        player.velX += 1; 

      # KEYUP 
      if(event.type == pygame.KEYUP): 
       if(event.key == pygame.K_LEFT): 
        player.velX = 0; 
       if(event.key == pygame.K_RIGHT): 
        player.velX = 0; 

     draw(); 
     animate(); 
     collision(); 


# DRAW 
def draw(): 
    global screen, food, player; 

    # fill background 
    screen.fill((255, 255, 255)); 

    player.draw(); 

    # update 
    pygame.display.update(); 

# ANIMATE 
def animate(): 
    global food, player; 

    food.animate(); 
    player.animate(); 

# COLLISION 
def collision(): 
    player.collision(); 

# CLASSES 
class Food(): 
    def __init__(self, x=0, y=0, w=0, h=0, velY=0, color=()): 
     global displayW; 

     self.x = random.randrange(0, displayW); 
     self.y = -100; 
     self.w = 20; 
     self.h = 20; 
     self.velY = 0.7; 
     self.color = (255, 0, 0); 

    def draw(self): 
     global screen; 

     pygame.draw.rect(screen, self.color, (self.x, self.y, self.w, self.h)); 

    def animate(self): 
     self.y += self.velY; 

    def collision(self): 
     global displayW, displayH; 

     pass; 

class Player(): 
    def __init__(self, x=0, y=0, velX=0, velY=0, w=0, h=0, color=()): 
     global displayW, displayH; 

     self.w = 20; 
     self.h = 20; 
     self.x = displayW/2 - self.w/2; 
     self.y = displayH - 100; 
     self.velX = 0; 
     self.velY = 0; 
     self.color = (0, 0, 0); 

    def draw(self): 
     global screen; 

     pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.w, self.h)); 

    def animate(self): 
     self.x += self.velX; 
     self.y += self.velY; 

    def collision(self): 
     global displayW; 

     # collision to walls 
     if(self.x <= 0): 
      self.velX = 0; 
     elif(self.x >= displayW): 
      self.velX = 0; 


# SETUP 
def setup(): 
    pygame.display.set_caption("Food Catcher"); 

# QUIT GAME 
def quitGame(): 
    pygame.quit(); 
    quit(); 

# CALL MAIN 
if(__name__ == "__main__"): 
    main(); 
+1

你会得到什么输出?你为什么认为这行不通? –

+0

它是Python:你不需要'''在行尾。 – jsbueno

回答

3

逻辑是正确的 - 但它允许玩家在两边缓慢“睡觉”。你认为它在左侧工作,玩家在右侧消失的原因是你的屏幕宽度与你的球员的角球坐标相比。当它为零时,播放器在屏幕上。但是,当player.x ==屏幕宽度时,播放器已经被拖出屏幕。

更改您的条件:

elif(self.x + self.h) >= displayW: 

的逻辑是正确的 - 但你必须在那里没有延迟,这意味着你GTE更新一样快,您的计算机可以重画屏幕 - 并且,新的计算poision(player.animate)发生你之前已经更新速度(velX)由于按键和后您检查是否有一个colision,并降低车速为0

因此,有效地不存在改变为玩家的速度,即使碰撞检测将其降至零 - 即可e下一帧将通过连续按键再次增加。我只是不确定为什么它不会在左侧消失。

无论如何,只需将呼叫更改为“碰撞”即可在调用动画之前进行。

更多pygame技巧:在while循环中包含几十微秒的延迟。这样,游戏不会吃掉你的CPU 100%。只需致电pygame.time.delay(20)即可。另外,您会注意到一次移动1或2像素可能会很慢 - 因此,请将速度调整为5或10像素的倍数。

而且,在“碰撞”方法上。不仅要改变速度,还要改变位置,让玩家在屏幕上。

更多Python的提示:不像其他语言的gazzilion,Python是不是C的语法时才直接modificationd:无需;在每行,并且无需()如果周围表达式(它们是由:分隔无论如何)。另外,如果您仅使用全局变量进行读取,或者调用全局变量中存储的对象中的方法,则无需声明它 - 事实上,您不需要这些global声明,而是需要setGlobalValues函数。