Advertisement
Guest User

Paste

a guest
Feb 14th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.52 KB | None | 0 0
  1. import pygame, sys, time
  2. from pygame.locals import *
  3.  
  4. # Define defaults
  5. bg = (0,0,0) # background colour
  6. spl = (255,255,255) # splash colour
  7. ver = "INDEV v1.0" # current version
  8. img = "icn.png" # corner icon (UNUSED)
  9. cur = "cur.png" # cursor icon
  10. player = "player.png" # player
  11. logo = "pixl.png" # splash logo
  12. currtile_x = 0
  13. currtile_y = 0
  14.  
  15. # Player details
  16. px = 20
  17. py = 20
  18. speedy = 50
  19. up = False
  20. down = False
  21. left = False
  22. right = False
  23. singlerun = 1
  24.  
  25.  
  26. # Load Map
  27. with open('townhall.map', 'r') as f:
  28.     for line in f:
  29.             for character in line:
  30.                 if character == "\n":
  31.                     print "Newline"
  32.                 else:
  33.                     if character == "x":
  34.                         print "WALL"
  35.                     else:
  36.                         if character == "a":
  37.                             print "LAND"
  38.                
  39.  
  40.  
  41. # Other
  42. completed = 0
  43. clock = pygame.time.Clock()
  44. splashboot = 0
  45.  
  46. # Initialise screen
  47. pygame.init()
  48. pygame.mouse.set_visible(False)
  49. screen = pygame.display.set_mode((640, 420))
  50. pygame.display.set_caption('The Missing Piece ' + ver)
  51.  
  52. # Unused
  53. #icon = pygame.image.load(img).convert_alpha()
  54. #pygame.display.set_icon(icon)
  55.  
  56. # Initialise sprites
  57. try:
  58.     cursor = pygame.image.load(cur).convert_alpha()
  59.     logo = pygame.image.load(logo).convert_alpha()
  60.     player = pygame.image.load(player).convert_alpha()
  61. except:
  62.     print "Unexpected error loading sprites!"
  63.     raw_input("Press ENTER to exit")
  64.     pygame.quit()
  65.     raise
  66.  
  67. # Splash screen
  68. while splashboot != 25:
  69.     for event in pygame.event.get():
  70.         if event.type == QUIT:
  71.             pygame.quit()
  72.     splash = pygame.Surface(screen.get_size())
  73.     splash = splash.convert()
  74.     splash.fill(spl)
  75.     x, y = screen.get_size()
  76.     gx = x - 600
  77.     yx = (y / 2) - 25
  78.     font = pygame.font.Font(None, 20)
  79.     splashver = font.render(ver, 20, (0, 0, 0))
  80.     screen.blit(splash, (0, 0))
  81.     screen.blit(logo, (gx,yx))
  82.     screen.blit(splashver, (520, yx + 50))
  83.     pygame.display.update()
  84.     splashboot += 1
  85.     time.sleep(0.1)
  86.     print splashboot
  87.  
  88.  
  89. # Fill background
  90. background = pygame.Surface(screen.get_size())
  91. background = background.convert()
  92. background.fill(bg)
  93.  
  94. # Define instructions
  95. font = pygame.font.Font(None, 20)
  96. ver = font.render("The Missing Piece " + ver, 20, (0, 0, 0))
  97. conl1 = font.render("W = UP", 20, (0, 0, 0))
  98. conl2 = font.render("A = LEFT", 20, (0, 0, 0))
  99. conl3 = font.render("S = DOWN", 20, (0, 0, 0))
  100. conl4 = font.render("D = RIGHT", 20, (0, 0, 0))
  101.  
  102. with open('townhall.map', 'r') as f:
  103.         for line in f:
  104.             for character in line:
  105.                 if character == "\n":
  106.                         currtile_y += 10
  107.                         currtile_x = 0
  108.                 else:
  109.                         if character == "x":
  110.                                 pygame.draw.rect(screen, (1,2,3), (currtile_x, currtile_y, 10, 10), 0)
  111.                                 currtile_x += 10
  112.                         else:
  113.                                 if character == "a":
  114.                                         pygame.draw.rect(screen, (0,255,255), (currtile_x, currtile_y, 10, 10), 0)
  115.                                         currtile_x += 10
  116.  
  117.  
  118. # Event loop
  119. while completed != 1:
  120.     dt = clock.tick(60)
  121.     speed = speedy / float(dt)
  122.     for event in pygame.event.get():
  123.         if event.type == QUIT:
  124.             completed = 1
  125.             pygame.quit()
  126.         if event.type == KEYDOWN:
  127.             if event.key == K_LEFT:
  128.                 left = True
  129.             if event.key == K_RIGHT:
  130.                 right = True  
  131.             if event.key == K_UP:
  132.                 up = True
  133.             if event.key == K_DOWN:
  134.                 down = True
  135.             if event.key == K_a:
  136.                 left = True
  137.             if event.key == K_d:
  138.                 right = True  
  139.             if event.key == K_w:
  140.                 up = True
  141.             if event.key == K_s:
  142.                 down = True
  143.         if event.type == KEYUP:
  144.             if event.key == K_LEFT:
  145.                 left = False
  146.             if event.key == K_RIGHT:
  147.                 right = False  
  148.             if event.key == K_UP:
  149.                 up = False
  150.             if event.key == K_DOWN:
  151.                 down = False
  152.             if event.key == K_a:
  153.                 left = False
  154.             if event.key == K_d:
  155.                 right = False  
  156.             if event.key == K_w:
  157.                 up = False
  158.             if event.key == K_s:
  159.                 down = False
  160.  
  161.     # direction scripts
  162.  
  163.     if up:
  164.         py -= speed
  165.     if down:
  166.         py += speed
  167.     if left:
  168.         px -= speed
  169.     if right:
  170.         px += speed
  171.        
  172.     # cursor settings
  173.     mousex,mousey = pygame.mouse.get_pos()
  174.     mousex -= cursor.get_width()/2
  175.     mousey -= cursor.get_height()/2
  176.     x, y = screen.get_size()
  177.     gx = x - 200
  178.  
  179.     # draw background
  180.     screen.blit(background, (0, 0))
  181.    
  182.     #pygame.draw.rect(screen, (255,255,255), (10,10,200,200), 0) # testbox
  183.     pygame.draw.rect(screen, (255,255,255), (gx,0,200,y), 0) # sidebar
  184.  
  185.     # draw player
  186.     screen.blit(player, (px, py))
  187.  
  188.     # draw version
  189.     screen.blit(ver, (gx,1))
  190.     pygame.draw.rect(screen, (0,255,255), (10, 30, 10, 10), 0)
  191.  
  192.     # draw instructions
  193.     screen.blit(conl1, (gx+2,20))
  194.     screen.blit(conl2, (gx+2,32))
  195.     screen.blit(conl3, (gx+2,44))
  196.     screen.blit(conl4, (gx+2,56))
  197.  
  198.     # draw cursor
  199.     screen.blit(cursor, (mousex, mousey))
  200.    
  201.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement