Advertisement
Guest User

pygame CheckCollision

a guest
Nov 2nd, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.48 KB | None | 0 0
  1. import pygame, sys
  2.  
  3.  
  4. #        00000000001111111111222222222233333333334
  5. #        01234567890123456789012345678901234567890
  6. Stage =("        ################################################################",#0
  7.         "#                                                              #",#1
  8.         "#   ####       #      ##   ## #####  #  #  #  #  #  #  #  #    #",#2
  9.         "#  #          # #     # # # # #      #  #  #  #  #  #  #  #    #",#3
  10.         "#  #   ##    #####    #  #  # ##     #  #  #  #  #  #  #  #    #",#4
  11.         "#  #    #   #     #   #     # #                                #",#5
  12.         "#   ####   #       #  #     # #####  #  #  #  #  #  #  #  #    #",#6
  13.         "#                                                              #",#7
  14.         "################################################################",#8
  15.         "#                                                              #",#9
  16.         "#                                                              #",#0
  17.         "#                                                              #",#1
  18.         "#                                                              #",#2
  19.         "#                                                              #",#3
  20.         "#                                                              #",#4
  21.         "#                                                              #",#5
  22.         "#                                                              #",#6
  23.         "#                                                              #",#7
  24.         "#                                                              #",#8
  25.         "#                                                              #",#9
  26.         "#                                                              #",#0
  27.         "#                                                              #",#1
  28.         "#                                                              #",#2
  29.         "#                                                              #",#3
  30.         "#                                                              #",#4
  31.         "#                                                              #",#5
  32.         "#                                                              #",#6
  33.         "#                                                              #",#7
  34.         "#                                                              #",#8
  35.         "#                                                              #",#9
  36.         "#                                                              #",#0
  37.         "#                                                              #",#1
  38.         "#                                                              #",#2
  39.         "#                                                              #",#3
  40.         "#                                                              #",#4
  41.         "#                                                              #",#5
  42.         "#                                                              #",#6
  43.         "#                                                              #",#7
  44.         "#                                                              #",#8
  45.         "#                                                              #",#9
  46.         "#                                                              #",#0
  47.         "#                                                              #",#1
  48.         "################################################################",#2
  49.         "#                                                              #",#3
  50.         "########                                                       #",#4
  51.         "#                                                              #",#5
  52.         "#                                                              #",#6
  53.         "################################################################")#7
  54.        
  55.  
  56. #globaler:     
  57. BlockSize = 16
  58. PlayerSize = 12
  59. ScreenSize = (1024, 768)
  60. FPS = 60
  61. Gravity = 0.20
  62. BackgroundColour = (255, 255, 255)#(red, green, blue) range: 0-255
  63. BlockColor = (0, 0, 0)
  64.  
  65. print "Defining helperfunctions...."
  66. def CheckCollision(x, y):
  67.     global BlockSize, PlayerSize, Stage
  68.    
  69.     for x2, y2 in ((1,0), (0,1), (0,1), (1,0)):#Each corner of player:
  70.         #if corner of player is within a solid in Stage[y][x]:
  71.         if Stage[int(y - 1 + PlayerSize*y2) // BlockSize][int(x - 1 + PlayerSize*x2) // BlockSize] == "#":
  72.             return True
  73.         if Stage[int(y + 2 + PlayerSize*y2) // BlockSize][int(x + 2 + PlayerSize*x2) // BlockSize] == "#":
  74.             return True
  75.         if Stage[int(y + PlayerSize*y2) // BlockSize][int(x + 2 + PlayerSize*x2) // BlockSize] == "#":
  76.             return True
  77.     return False
  78.    
  79. print "Setting up playerobject..."
  80. class Player:
  81.     def __init__(self, x, y):
  82.         self.x, self.y = float(x), float(y)
  83.         self.Color = (255, 0, 127)#(red, green, blue) range: 0-255
  84.         self.vspeed = 0.0
  85.     def Step(self):
  86.         global Gravity
  87.         #Apply gravitation pull:
  88.         if not CheckCollision(self.x, self.y+1):#if there is no ground beneath the player's feet:
  89.             self.vspeed += Gravity
  90.         else:
  91.             self.vspeed = 0.0
  92.        
  93.         #Walk:
  94.         Keys = pygame.key.get_pressed()
  95.         if Keys[pygame.K_d] in (1,2):#Right D
  96.             if not CheckCollision(self.x+1, self.y):
  97.                 self.x += 5
  98.         if Keys[pygame.K_a] in (1,2):#Left A
  99.             if not CheckCollision(self.x-1, self.y):
  100.                 self.x -= 5
  101.        
  102.         #jump:
  103.         if Keys[pygame.K_w] in (1,2):#Up W
  104.             #if ground beneath feet:
  105.             if CheckCollision(self.x, self.y+1):
  106.                 self.vspeed = -6
  107.        
  108.         #add gravity and jump to self.y:
  109.         if self.vspeed <> 0.0:
  110.             #check if the fall will collide
  111.             if CheckCollision(self.x, self.y+self.vspeed):
  112.                 #if so, step pixel by pixel until hitting the wall:
  113.                 for i in xrange(int(self.y), int(self.y+self.vspeed), 1 if self.vspeed > 0 else -1):
  114.                     if CheckCollision(self.x, i):
  115.                         self.y = float(i-1) if self.vspeed > 0 else float(i+1)
  116.                         break
  117.             else:
  118.                 self.y += self.vspeed
  119.        
  120.         print self.vspeed
  121.     def Draw(self, Parent):
  122.         global PlayerSize
  123.         pygame.draw.rect(Parent, self.Color, (int(self.x), int(self.y), PlayerSize, PlayerSize))#(Surface, color, (x, y, width, height))
  124. you = Player(1*BlockSize, 46*BlockSize)
  125.  
  126. print "Initalizing pygame..."
  127. pygame.init()
  128. Screen = pygame.display.set_mode(ScreenSize)
  129. Timer = pygame.time.Clock()
  130. #main loop:
  131. print "START GAME!"
  132. while 1:
  133.     #maintain FPS:
  134.     Timer.tick(FPS)
  135.    
  136.     #read input:
  137.     for i in pygame.event.get():
  138.         if i.type == pygame.QUIT:
  139.             sys.exit()
  140.    
  141.    
  142.     #step:
  143.     you.Step()
  144.    
  145.     #draw:
  146.     Screen.fill(BackgroundColour)#clear screen
  147.     for x in xrange(len(Stage[0])):
  148.         for y in xrange(len(Stage)):
  149.             if Stage[y][x] == "#":
  150.                 pygame.draw.rect(Screen, BlockColor, (x*BlockSize, y*BlockSize, BlockSize, BlockSize))#(Surface, color, (x, y, width, height))
  151.     you.Draw(Screen)
  152.  
  153.     #update changes done to Screen:
  154.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement