Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. import os
  2. import pygame
  3. import random
  4. pygame.init()
  5. white = (255,255,255)
  6. black = (0,0,0)
  7. red = (255,0,0)
  8. green = (0,155,0)
  9. brown = (205,133,63)
  10. blue = (135,206,250)
  11. FPS = 60
  12. #the dimension of the game
  13. display_width = 680
  14. display_height = 440
  15. # Class for the player
  16. class Player(object):
  17.     def __init__(self):
  18.         self.rect = pygame.Rect(40, 40, 30, 30)
  19.     def move(self, dx, dy):
  20.         # Move each axis separately. Note that this checks for collisions both times.
  21.         if dx != 0:
  22.             self.move_single_axis(dx, 0)
  23.         if dy != 0:
  24.             self.move_single_axis(0, dy)
  25.     def move_single_axis(self, dx, dy):
  26.         # Move the rect
  27.         self.rect.x += dx
  28.         self.rect.y += dy
  29.         # If you collide with a wall, move out based on velocity
  30.         for wall in walls:
  31.             if self.rect.colliderect(wall.rect):
  32.                 if dx > 0: # Moving right, Hit the left side of the wall
  33.                     self.rect.right = wall.rect.left
  34.                 if dx < 0: # Moving left, Hit the right side of the wall
  35.                     self.rect.left = wall.rect.right
  36.                 if dy > 0: # Moving down, Hit the top side of the wall
  37.                     self.rect.bottom = wall.rect.top
  38.                 if dy < 0: # Moving up, Hit the bottom side of the wall
  39.                     self.rect.top = wall.rect.bottom
  40. # Class to hold a wall rect
  41. class Wall(object):
  42.     def __init__(self, pos):
  43.         walls.append(self)
  44.         self.rect = pygame.Rect(pos[0], pos[1], 40, 40)
  45. font = pygame.font.SysFont(None, 50)
  46. def message_to_screen(msg,color):
  47.     screen_text = font.render(msg, True, color)
  48.     gameDisplay.blit(screen_text, [680/2, display_height/2])
  49. # Initialise pygame
  50. os.environ["Time to play"] = "1"
  51. # Set up the display
  52. pygame.display.set_caption("Wrath of the gods")
  53. gameDisplay = pygame.display.set_mode((display_width,display_height))
  54. clock = pygame.time.Clock()
  55. walls = [] # List to hold the walls
  56. player = None
  57. end_rect = None
  58. def reinit():
  59.     global player, end_rect
  60.     player = Player() # Create the player
  61.     # Holds the level layout in a list of strings.
  62.     level = [
  63.     "WWWWWWWWWWWWWWWWW",
  64.     "W    W    W     W",
  65.     "W WW W WW   WWW W",
  66.     "W W  W  W W W W W",
  67.     "W WWWWW WWW W W W",
  68.     "W  W  W   W   W W",
  69.     "WW W WW WWWWWWW W",
  70.     "W    W   W      W",
  71.     "W WWWW W WWWWW WW",
  72.     "W               W",
  73.     "WWWWWWWWWWWWWWWEW",
  74.     ]
  75.     # W = wall, E = exit
  76.     x = y = 0
  77.     for row in level:
  78.         for col in row:
  79.             if col == "W":
  80.                 Wall((x, y))
  81.             if col == "E":
  82.                 end_rect = pygame.Rect(x, y, 40, 40)
  83.             x += 40
  84.         y += 40
  85.         x = 0
  86. reinit()
  87. bigfont = pygame.font.Font(None, 80)
  88. smallfont = pygame.font.Font(None, 45)
  89. def play_again():
  90.     SCREEN_WIDTH = display_width
  91.     SCREEN_HEIGHT = display_height
  92.     screen = gameDisplay
  93.     text = bigfont.render('Play again?', 13, (0, 0, 0))
  94.     textx = SCREEN_WIDTH / 2 - text.get_width() / 2
  95.     texty = SCREEN_HEIGHT / 2 - text.get_height() / 2
  96.     textx_size = text.get_width()
  97.     texty_size = text.get_height()
  98.     pygame.draw.rect(screen, (255, 255, 255), ((textx - 5, texty - 5),
  99.                                                (textx_size + 10, texty_size +
  100.                                                 10)))
  101.     screen.blit(text, (SCREEN_WIDTH / 2 - text.get_width() / 2,
  102.                        SCREEN_HEIGHT / 2 - text.get_height() / 2))
  103.     #clock = pygame.time.Clock()
  104.     pygame.display.flip()
  105.     in_main_menu = True
  106.     while in_main_menu:
  107.         clock.tick(50)
  108.         for event in pygame.event.get():
  109.             if event.type == pygame.QUIT:
  110.                 in_main_menu = False
  111.                 return False
  112.             elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  113.                 x, y = event.pos
  114.                 if x >= textx - 5 and x <= textx + textx_size + 5:
  115.                     if y >= texty - 5 and y <= texty + texty_size + 5:
  116.                         in_main_menu = False
  117.                         return True
  118. running = True
  119. while running:
  120.     clock.tick(FPS)
  121.     for e in pygame.event.get():
  122.         if e.type == pygame.QUIT:
  123.             running = False
  124.         if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
  125.             running = False
  126.     # Move the player
  127.     key = pygame.key.get_pressed()
  128.     if key[pygame.K_LEFT]:
  129.         player.move(-2, 0)
  130.     if key[pygame.K_RIGHT]:
  131.         player.move(2, 0)
  132.     if key[pygame.K_UP]:
  133.         player.move(0, -2)
  134.     if key[pygame.K_DOWN]:
  135.         player.move(0, 2)
  136.     if player.rect.colliderect(end_rect):
  137.         again = play_again()
  138.         if again:
  139.             reinit()
  140.         else:
  141.             break
  142.         # Draw the scene
  143.     gameDisplay.fill((brown))
  144.     for wall in walls:
  145.         pygame.draw.rect(gameDisplay, green, wall.rect)
  146.     pygame.draw.rect(gameDisplay, red, end_rect)
  147.     pygame.draw.rect(gameDisplay, white, player.rect)
  148.     pygame.display.flip()
  149. pygame.quit()
  150. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement