[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] Newbie help : Platforms and collisions



 
Hi,
 
I am a Pygame newbie.
I have modified the "movit" tutorial trying to develop a very basic platform game.
 
I have troubles in making my hero standing on the platforms; it "sinks" in the platforms more or less deeply according to the hero and to the platform positions.
 
I guess the mistake is in the following code I use to detect the platform "support" for my hero.
 
 
# check if hero is on a platform
   flag = 0
   for t in pl_list :
      #print t.pos,hero.pos,t.pos.colliderect(hero.pos)
      if t.pos.colliderect(hero.pos) == 1 :        
         hero.support = 1
         flag = 1
         if hero.s > 3 :
         # if the jump is alraedy in the
         #descending part then it
         # stops jumping
            hero.s = 0
            j = 0
         
       
   if flag == 0 :  
   # hero is not on a platform 
      hero.support = 0
 
   if hero.support == 0 and j == 0:
   # hero has support and is not jumping
      hero.fall()
      hero.move()
   elif j == 1 :
   #hero is jumping
      hero.jump(dir)
   else :
      hero.move()
 
 
However I enclose the full code (see below), since the problem might be somewhere else.
 
Thanks in advance for your help.
 
 
Fabrizio .
 
 
      
 
------------------------------------------
 
 
 
 
import pygame, sys, os
from pygame.locals import *
 
j = 0
dir =1
 

class GameObject:
  
   def __init__(self, image, posx, posy, patternX, patternY, support=1):     
      self.image = image
      self.pos = image.get_rect().move(posx, posy)
      self.patternX = patternX
      self.patternY = patternY
      self.support = support
      self.s = 0
     
   def move(self):
      #print 'self.s',self.s
      #print 'patternX',self.patternX
      #print 'patternY',self.patternY
 
      self.pos = self.pos.move(self.patternX[self.s], self.patternY[self.s])
      #print self.patternX[self.s], self.patternY[self.s]
      if self.pos.right > 640:
         self.pos.left = 0
 
      self.s = self.s + 1
      # at the end of the pattern starts from the beginning
      if self.s > len(self.patternX)-1:
         self.s = 0        
        
      return
 
  
   def jump(self,dir):
     
      global j
     
      if j == 1 :
         #self.patternX = [dir*10,dir*10,dir*10,dir*10,dir*10,dir*10]
         self.patternX = [dir*10,dir*20,dir*20,dir*20,dir*20,dir*20,dir*20,dir*10]
         self.patternY = [-60,-50,-20,-10,10,20,50,60]
            
      self.pos = self.pos.move(self.patternX[self.s], self.patternY[self.s])
      #print self.patternX[self.s], self.patternY[self.s]
      if self.pos.right > 640:
         self.pos.left = 0
 
      self.s = self.s + 1
      # at the end of the pattern
      if self.s > len(self.patternX)-1:
         self.s = 0
         j = 0
         self.patternX=[5]
         self.patternY=[0]
        
      return
  
   def fall(self):
 
      self.patternX=[0]
      self.patternY=[10]
 
      return
 

     
   def cancel(self):             
      screen.blit(background, self.pos, self.pos)
 
   def calculate(self) :
      screen.blit(self.image, self.pos)
 

def update_all (dir, ob_list):
   global j
   pl_list = [ob_list[2], ob_list[3],ob_list[4],ob_list[5]]
   # it's the list of the objects "platform"
  
   hero.cancel()
   villain.cancel()
 
   # check if hero is on a platform
   flag = 0
   for t in pl_list :
      #print t.pos,hero.pos,t.pos.colliderect(hero.pos)
      if t.pos.colliderect(hero.pos) == 1 :        
         hero.support = 1
         flag = 1
         if hero.s > 3 :
         # if the jump is alraedy in the
         #descending part then it
         # stops jumping
            hero.s = 0
            j = 0
        
      
   if flag == 0 :  
   # hero is not on a platform 
      hero.support = 0
 
   if hero.support == 0 and j == 0:
   # hero has support and is not jumping
      hero.fall()
      hero.move()
   elif j == 1 :
   #hero is jumping
      hero.jump(dir)
   else :
      hero.move()
 
  
   if hero.pos.colliderect(villain.pos) == 1:
   # collision with a villain
      #print "Collision !"
      pass
     
        
   villain.move()
 

   for t in ob_list:  
      t.calculate()
  
     
   pygame.display.update()
   pygame.time.delay(80)
  
   hero.patternX=[0]
   hero.patternY=[0]
  
   return
 

#main
 
screen = pygame.display.set_mode((640, 480))
heroimg = pygame.image.load('data/hero5.bmp').convert()
heroimg.set_colorkey((0,0,0),RLEACCEL)
# set black as the transparent color
#so that it will not be detected
#during collision ? => no; collision is still
# detected => how to  do ?
 
villimg = pygame.image.load('data/villain5.bmp').convert()
villimg.set_colorkey((0,0,0),RLEACCEL)
# set black as the transparent color
 
platimg =pygame.image.load('data/platform3.bmp').convert()
background = "">
 
screen.blit(background, (0, 0))
 
hero = GameObject(heroimg, 1, 150, [0],[0])
villain = GameObject(villimg, 150, 150, [10,10,10,10,10,10,-10,-10,-10,-10,-10,-10],[0,0,0,0,0,0,-10,-10,-10,10,10,10])
platform = GameObject(platimg, 1, 210, [0],[0])
platform2 = GameObject(platimg, 350, 213, [0],[0])
platform3 = GameObject(platimg, 150, 320, [0],[0])
platform4 = GameObject(platimg, 10, 400, [0],[0])
ob_list = [hero,villain, platform, platform2, platform3, platform4]
 
update_all(dir, ob_list)
 

pygame.key.set_repeat()
 
while 1:
##   hero.patternX=[0]
##   hero.patternY=[0]
  
   event = pygame.event.poll()
   keystate = pygame.key.get_pressed()
        
   if event.type == QUIT:
      sys.exit()
 
   if keystate[K_SPACE] and hero.support == 1:
      # jump has been pushed and hero is not falling
      j = 1
      if keystate[K_RIGHT]:
         dir = 1  # is the direction flag : 1 = right           
      elif keystate[K_LEFT]:
         dir = -1  # is the direction flag : -1 = left
      else:
         dir = 0  # is the direction flag : 0 = no direction
 
                       
   elif keystate[K_RIGHT]:
      hero.patternX=[5]
           
   elif keystate[K_LEFT]:
      hero.patternX=[-5]
           
   update_all(dir,ob_list)
     
     
------------------------------------------------------------