Advertisement
Ward_Programm3r

Main.py for SO

Nov 28th, 2013
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.32 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import time
  4. import random
  5.  
  6. pygame.init()
  7. pygame.font.init()
  8.  
  9. #Colors
  10. black = (0, 0, 0)
  11. white = (255, 255, 255)
  12. aqua =(  0, 255, 255)
  13. blue = (0, 0, 255)
  14. fuchsia = (255, 0, 255)
  15. gray = (128, 128, 128)
  16. green = (0, 128, 0)
  17. lime = (0, 255, 0)
  18. maroon = (128, 0, 0)
  19. navyBlue = (0, 0, 128)
  20. olive = (128, 128, 0)
  21. purple = (128, 0, 128)
  22. red = (255, 0, 0)
  23. silver = (192, 192, 192)
  24. teal = (0, 128, 128)
  25. deepBlue = (35, 68, 255)
  26. yellow = (255, 255, 0)
  27.  
  28. #Screen Definition
  29. screenX = 928
  30. screenY = 480
  31.  
  32. screenX_center = screenX/2
  33. screenY_center = screenY/2
  34. screen_center = (screenX_center, screenY_center)
  35.  
  36.  
  37. screenSize = screenX, screenY
  38.  
  39. screen = pygame.display.set_mode(screenSize)
  40. pygame.display.set_caption("Unnamed Ship Game - Xeo Games")
  41.  
  42. #Font/Text Declaration
  43.  
  44. fpsFont = pygame.font.Font(None, 15)
  45.  
  46. moneyFont = pygame.font.Font(None, 15)
  47. goodsFont = pygame.font.Font(None, 15)
  48.  
  49. #Images
  50. bkg = pygame.image.load("f3TAE.png").convert()
  51. bkgRect = bkg.get_rect()
  52. bkgRect.center = screen_center
  53.  
  54. splash = pygame.image.load("Splash.png").convert()
  55. splashRect = splash.get_rect()
  56. splashRect.center = screen_center
  57.  
  58. Clock = pygame.time.Clock()
  59.  
  60. #Variable Declaration
  61. goodsAvailable = 0
  62. moveAvailable = False
  63.  
  64.  
  65. #Definitions
  66. def wipeScreenWhite():
  67.     screen.fill(white)
  68. #def displaySplashscreen():
  69.     #screen.fill(teal)
  70.     #time.sleep(2)
  71.     #global Starting
  72.     #Starting = False
  73.  
  74.  
  75. #Object Declaration:
  76. # WIP Pirate Class
  77. class Pirate(object):
  78.  
  79.     def __init__(self):
  80.         self.rect = pygame.Rect(randx, randy, 32, 32)
  81.  
  82.  
  83. # Class for the orange dude
  84. class Player(object):
  85.    
  86.     def __init__(self):
  87.  
  88.         self.rect = pygame.Rect(224, 96, 32, 32)
  89.  
  90.         self.atPort = False
  91.         self.money = 0
  92.         self.moneyString = ""
  93.         self.goodsNumber = 0
  94.         self.goodsNumberString = ""
  95.  
  96.     def move(self, dx, dy):
  97.        
  98.         # Move each axis separately. Note that this checks for collisions both times.
  99.         if dx != 0:
  100.             self.move_single_axis(dx, 0)
  101.         if dy != 0:
  102.             self.move_single_axis(0, dy)
  103.    
  104.     def move_single_axis(self, dx, dy):
  105.  
  106.         # Move the rect
  107.         self.rect.x += dx
  108.         self.rect.y += dy
  109.  
  110.  
  111.         for port in ports:
  112.             if self.rect.colliderect(port.rect):
  113.                 if dx > 0: # Moving right; Hit the left side of a port
  114.                     self.rect.right = port.rect.left
  115.                     self.atPort = True
  116.                 if dx < 0: # Moving left; Hit the right side of a port
  117.                     self.rect.left = port.rect.right
  118.                     self.atPort = True
  119.                 if dy > 0: # Moving down; Hit the top side of a port
  120.                     self.rect.bottom = port.rect.top
  121.                     self.atPort = True
  122.                 if dy < 0: # Moving up; Hit the bottom side of a port
  123.                     self.rect.top = port.rect.bottom
  124.                     self.atPort = True
  125.  
  126.         # If you collide with a block, move out based on velocity
  127.         for block in blocks:
  128.             if self.rect.colliderect(block.rect):
  129.                 if dx > 0: # Moving right; Hit the left side of the block
  130.                     self.rect.right = block.rect.left
  131.                 if dx < 0: # Moving left; Hit the right side of the block
  132.                     self.rect.left = block.rect.right
  133.                 if dy > 0: # Moving down; Hit the top side of the block
  134.                     self.rect.bottom = block.rect.top
  135.                 if dy < 0: # Moving up; Hit the bottom side of the block
  136.                     self.rect.top = block.rect.bottom
  137.  
  138. class Block(object):
  139.    
  140.     def __init__(self, pos):
  141.         blocks.append(self)
  142.         self.rect = pygame.Rect(pos[0], pos[1], 32, 32)
  143.  
  144.  
  145. class Port(Block):
  146.  
  147.     def __init__(self, pos):
  148.         ports.append(self)
  149.         self.rect = pygame.Rect(pos[0], pos[1], 32, 32)
  150.  
  151.  
  152. blocks = [] # List to hold the blocks
  153. ports = [] # List to hold the ports
  154. player = Player() # Create the player
  155.  
  156. # Holds the level layout in a list of strings.
  157. level = [
  158. "WWWWWWWWWWWWWWWWWWWW",
  159. "W                  W",
  160. "W                  W",
  161. "W             P    W",
  162. "W                  W",
  163. "W                  W",
  164. "W                  W",
  165. "W                  W",
  166. "W                  W",
  167. "W                  W",
  168. "W                  W",
  169. "W                  W",
  170. "W   P              W",
  171. "W                  W",
  172. "WWWWWWWWWWWWWWWWWWWW",
  173. ]
  174.  
  175. # Parse the level string above. W = wall, E = exit
  176. x = 144
  177. y = 0
  178. for row in level:
  179.     for col in row:
  180.         if col == "W":
  181.             Block((x, y))
  182.         if col == "P":
  183.             Port((x, y,))
  184.         x += 32
  185.     y += 32
  186.     x = 144
  187.  
  188. #Program Loops
  189. timeVar = 0 # Amount of time that has passed
  190. Starting = True # Whether or not to show the splashscreen
  191. Running = True
  192.  
  193.  
  194. while Running:
  195.     while Starting == True:
  196.         timeVar = timeVar + 1
  197.         for event in pygame.event.get():
  198.             if event.type == pygame.QUIT:
  199.                 Starting == False
  200.                 Running = False
  201.         if timeVar == 150:
  202.             timeVar = 0
  203.             Starting = False
  204.            
  205.         #FPS LABEL
  206.         fps = Clock.get_fps()
  207.         fps = round(fps, 2)
  208.         fpsString = str(fps)
  209.         fpsLabel = fpsFont.render(fpsString, 20, black)    
  210.        
  211.         #Drawing to screen
  212.         wipeScreenWhite()
  213.         screen.blit(splash, splashRect)
  214.         screen.blit(fpsLabel, (10, 10))
  215.         pygame.display.flip()
  216.         Clock.tick(50)
  217.  
  218.     #Main Loop, if Starting is False
  219.  
  220.     timeVar = timeVar + 1
  221.     for event in pygame.event.get():
  222.         if event.type == pygame.QUIT:
  223.             Running = False
  224.         elif event.type == pygame.KEYDOWN:
  225.             if event.key == pygame.K_ESCAPE:
  226.                 Running = False
  227.  
  228.  
  229.     #Capturing Mouse Position
  230.     mousePos = pygame.mouse.get_pos()
  231.     mouseX = mousePos[0]
  232.     mouseY = mousePos[1]
  233.  
  234.     #Capturing and Responding to Keys
  235.     key = pygame.key.get_pressed()
  236.     if key[pygame.K_LEFT]:
  237.         player.move(-32, 0)
  238.     if key[pygame.K_RIGHT]:
  239.         player.move(32, 0)
  240.     if key[pygame.K_UP]:
  241.         player.move(0, -32)
  242.     if key[pygame.K_DOWN]:
  243.         player.move(0, 32)
  244.    
  245.     #FPS LABEL
  246.     fps = Clock.get_fps()
  247.     fps = round(fps, 2)
  248.     fpsString = str(fps)
  249.     fpsLabel = fpsFont.render(fpsString, 20, black)
  250.  
  251.     if player.atPort == True:
  252.         player.money = player.money + (10*player.goodsNumber)
  253.         player.moneyString =  str(player.money)
  254.         player.goodsNumber = 0
  255.  
  256.         goodsAvailable = random.randrange(10, 20)
  257.         print("How many goods would you like to take? The max is: ", goodsAvailable)
  258.         player.goodsNumber = int(input(">>>"))
  259.         if player.goodsNumber > goodsAvailable:
  260.             print("You can only take up to: ", goodsAvailable, ". Pick another number.")
  261.             player.goodsNumber = int(input(">>>"))
  262.             print("You have brought ", player.goodsNumber," goods on board.")
  263.             player.goodsNumberString = str(player.goodsNumber)
  264.             player.atPort = False
  265.         else:
  266.             print("You have brought ", player.goodsNumber," goods on board.")
  267.             player.goodsNumberString = str(player.goodsNumber)
  268.             player.atPort = False
  269.  
  270.     #Goods and Money Labels
  271.     goodsLabel = goodsFont.render("Goods Carrying:", 20, black)
  272.     goodsLabelLineTwo = goodsFont.render(player.goodsNumberString, 20, black)
  273.     moneyLabel = moneyFont.render("Money:", 20, black)
  274.     moneyLabelLineTwo = moneyFont.render(player.moneyString, 20, black)
  275.  
  276.  
  277.     #Drawing to Screen
  278.     wipeScreenWhite()
  279.     screen.blit(bkg, bkgRect)
  280.     for block in blocks:
  281.         pygame.draw.rect(screen, (0, 0, 0), block.rect)
  282.     for port in ports:
  283.         pygame.draw.rect(screen, deepBlue, port.rect)
  284.     # WIP Pirates
  285.     #for pirate in pirates:
  286.     #   pygame.draw.rect(screen, red, pirate.rect)
  287.     pygame.draw.rect(screen, (255, 200, 0), player.rect)
  288.     screen.blit(goodsLabel, (10, 50))
  289.     screen.blit(goodsLabelLineTwo, (10, 60))
  290.     screen.blit(moneyLabel, (10, 30))
  291.     screen.blit(moneyLabelLineTwo, (10, 40))
  292.     screen.blit(fpsLabel, (10, 10))
  293.     pygame.display.flip()
  294.     Clock.tick(30)
  295.  
  296. pygame.font.quit()
  297. pygame.quit()
  298. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement