Advertisement
Ward_Programm3r

Main.py 11-27 4:00

Nov 27th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import time
  4.  
  5. pygame.init()
  6. pygame.font.init()
  7.  
  8. #Colors
  9. black = (0, 0, 0)
  10. white = (255, 255, 255)
  11. aqua =(  0, 255, 255)
  12. blue = (0, 0, 255)
  13. fuchsia = (255, 0, 255)
  14. gray = (128, 128, 128)
  15. green = (0, 128, 0)
  16. lime = (0, 255, 0)
  17. maroon = (128, 0, 0)
  18. navyBlue = (0, 0, 128)
  19. olive = (128, 128, 0)
  20. purple = (128, 0, 128)
  21. red = (255, 0, 0)
  22. silver = (192, 192, 192)
  23. teal = (0, 128, 128)
  24. yellow = (255, 255, 0)
  25.  
  26. #Screen Definition
  27. screenX = 950
  28. screenY = 500
  29.  
  30. screenX_center = screenX/2
  31. screenY_center = screenY/2
  32. screen_center = (screenX_center, screenY_center)
  33.  
  34.  
  35. screenSize = screenX, screenY
  36.  
  37. screen = pygame.display.set_mode(screenSize)
  38. pygame.display.set_caption("Unnamed Ship Game - Xeo Productions")
  39.  
  40. #Font/Text Declaration
  41.  
  42. fpsFont = pygame.font.Font(None, 15)
  43.  
  44. #Images
  45. bkg = pygame.image.load("f3TAE.png")
  46. bkgRect = bkg.get_rect()
  47. bkgRect.center = screen_center
  48.  
  49. splash = pygame.image.load("Splash.png")
  50. splashRect = splash.get_rect()
  51. splashRect.center = screen_center
  52.  
  53. Clock = pygame.time.Clock()
  54.  
  55.  
  56. #Definitions
  57. def wipeScreenWhite():
  58.     screen.fill(white)
  59. #def displaySplashscreen():
  60.     #screen.fill(teal)
  61.     #time.sleep(2)
  62.     #global Starting
  63.     #Starting = False
  64.  
  65.  
  66. #Object Declaration:
  67. # Class for the orange dude
  68. class Player(object):
  69.    
  70.     def __init__(self):
  71.         self.rect = pygame.Rect(32, 32, 16, 16)
  72.  
  73.     def move(self, dx, dy):
  74.        
  75.         # Move each axis separately. Note that this checks for collisions both times.
  76.         if dx != 0:
  77.             self.move_single_axis(dx, 0)
  78.         if dy != 0:
  79.             self.move_single_axis(0, dy)
  80.    
  81.     def move_single_axis(self, dx, dy):
  82.  
  83.         # Move the rect
  84.         self.rect.x += dx
  85.         self.rect.y += dy
  86.  
  87.         # If you collide with a block, move out based on velocity
  88.         for block in blocks
  89.             if self.rect.colliderect(block.rect):
  90.                 if dx > 0: # Moving right; Hit the left side of the block
  91.                     self.rect.right = block.rect.left
  92.                 if dx < 0: # Moving left; Hit the right side of the block
  93.                     self.rect.left = block.rect.right
  94.                 if dy > 0: # Moving down; Hit the top side of the block
  95.                     self.rect.bottom = block.rect.top
  96.                 if dy < 0: # Moving up; Hit the bottom side of the block
  97.                     self.rect.top = block.rect.bottom
  98.  
  99. class Block(object):
  100.    
  101.     def __init__(self, pos):
  102.         blocks.append(self)
  103.         self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
  104.  
  105.  
  106. #Program Loops
  107. timeVar = 0
  108. Starting = True
  109. Running = True
  110.  
  111. while Running:
  112.     while Starting == True:
  113.         timeVar = timeVar + 1
  114.         for event in pygame.event.get():
  115.             if event.type == pygame.QUIT:
  116.                 Starting == False
  117.                 Running = False
  118.         if timeVar == 500:
  119.             timeVar = 0
  120.             Starting = False
  121.            
  122.         #FPS LABEL
  123.         fps = Clock.get_fps()
  124.         fps = round(fps, 2)
  125.         fpsString = str(fps)
  126.         fpsLabel = fpsFont.render(fpsString, 20, black)    
  127.        
  128.         #Drawing to screen
  129.         wipeScreenWhite()
  130.         screen.blit(splash, splashRect)
  131.         screen.blit(fpsLabel, (10, 10))
  132.         pygame.display.flip()
  133.         Clock.tick(50)
  134.  
  135.     #Main Loop, if Starting is False
  136.  
  137.     timeVar = timeVar + 1
  138.     for event in pygame.event.get():
  139.         if event.type == pygame.QUIT:
  140.             Running = False
  141.         elif event.type == pygame.KEYDOWN:
  142.             if event.key == pygame.K_ESCAPE:
  143.                 Running = False
  144.  
  145.  
  146.     #Capturing Mouse Position
  147.     mousePos = pygame.mouse.get_pos()
  148.     mouseX = mousePos[0]
  149.     mouseY = mousePos[1]
  150.  
  151.     #Capturing and Responding to Keys
  152.     key = pygame.key.get_pressed()
  153.     if key[pygame.K_LEFT]:
  154.         player.move(-2, 0)
  155.     if key[pygame.K_RIGHT]:
  156.         player.move(2, 0)
  157.     if key[pygame.K_UP]:
  158.         player.move(0, -2)
  159.     if key[pygame.K_DOWN]:
  160.         player.move(0, 2)
  161.    
  162.     #FPS LABEL
  163.     fps = Clock.get_fps()
  164.     fps = round(fps, 2)
  165.     fpsString = str(fps)
  166.     fpsLabel = fpsFont.render(fpsString, 20, black)    
  167.  
  168.     wipeScreenWhite()
  169.     screen.blit(bkg, bkgRect)
  170.     screen.blit(fpsLabel, (10, 10))
  171.     pygame.display.flip()
  172.     Clock.tick(30)
  173.  
  174. pygame.font.quit()
  175. pygame.quit()
  176. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement