Advertisement
Guest User

WFEA Testing in pygame

a guest
Jan 18th, 2013
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.71 KB | None | 0 0
  1. import wfea
  2. import pygame
  3. import pygame.font
  4. import sys
  5. import math
  6. from random import *
  7.  
  8. global GRASS_BLOCK
  9. global WALL_BLOCK
  10.  
  11. GRASS_BLOCK = 0
  12. WALL_BLOCK = 101
  13.  
  14.  
  15. # Game parameters
  16. SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480
  17. BG_COLOR = 150, 170, 120
  18. WALL_COLOR = 40, 40, 40
  19. SQUARE_COLOR = 255, 255, 255
  20.  
  21. class MouseFollowingSquare(wfea.WFEAObject):
  22.     def __init__(self, x, y):
  23.         super(MouseFollowingSquare, self).__init__()
  24.         self.x = x
  25.         self.y = y
  26.         self.xVelocity = 0
  27.         self.yVelocity = 0
  28.  
  29.     def notPassable(self, grid, x, y):
  30.         # First get all the blocks that are being touched
  31.         all_connected = [ [int(x / 32), int(y / 32)] ]
  32.  
  33.         if int(x / 32) != int((x + 16) / 32):
  34.             all_connected.append([all_connected[0][0] + 1, all_connected[0][1]])
  35.             if int(y / 32) != int((y + 16) / 32):
  36.                 all_connected.append([all_connected[0][0] + 1, all_connected[0][1] + 1])
  37.  
  38.         if int(y / 32) != int((y + 16) / 32):
  39.             all_connected.append([all_connected[0][0], all_connected[0][1] + 1])
  40.  
  41.         for i in range(len(all_connected)):
  42.             if grid.getPassingValue(all_connected[i][0], all_connected[i][1]) < 0:
  43.                 return True
  44.         return False
  45.  
  46.     def update(self, magic, mouse, grid, counter):
  47.         self.xVelocity, self.yVelocity = magic.getNextMovementVector(mouse, self, counter)
  48.         self.xVelocity = self.xVelocity * 3
  49.         self.yVelocity = self.yVelocity * 3
  50.         nextX = self.x + self.xVelocity
  51.         if self.notPassable(grid, nextX, self.y):
  52.             self.yVelocity = self.yVelocity + 0.5
  53.             self.xVelocity = 0
  54.  
  55.         nextY = self.y + self.yVelocity
  56.         if self.notPassable(grid, self.x, nextY):
  57.             self.yVelocity = 0
  58.             self.xVelocity = self.xVelocity + 0.5
  59.  
  60.         nextX = self.x + self.xVelocity
  61.         nextY = self.y + self.yVelocity
  62.         if not self.notPassable(grid, nextX, nextY):
  63.             self.x = nextX
  64.             self.y = nextY
  65.    
  66.  
  67. pygame.init()
  68. screen = pygame.display.set_mode(
  69.             (SCREEN_WIDTH, SCREEN_HEIGHT), pygame.DOUBLEBUF, 32)
  70. clock = pygame.time.Clock()
  71.  
  72. def exit_game():
  73.     pygame.quit()
  74.     sys.exit(0)
  75.  
  76. myfont = pygame.font.SysFont("monospace", 11)
  77.  
  78. def spawnSquare(grid):
  79.     xLoc = -1
  80.     yLoc = -1
  81.     while xLoc < 0 or yLoc < 0 or grid.getPassingValue(xLoc, yLoc) < 0:
  82.         xLoc = randrange(1, 19)
  83.         yLoc = randrange(1, 14)
  84.  
  85.     return MouseFollowingSquare(xLoc * 32 + 2, yLoc * 32 + 2)
  86. def spawnSquares(num, grid):
  87.     res = []
  88.     for i in range(num):
  89.         res.append(spawnSquare(grid))
  90.     return res
  91.  
  92. def drawGrid(obj, grid, counter):
  93.     for x in range(grid.width):
  94.         for y in range(grid.height):
  95.             if(grid.getType(x, y) == GRASS_BLOCK):
  96.                 pass
  97.             elif(grid.getType(x, y) == WALL_BLOCK):
  98.                 pygame.draw.rect(screen, WALL_COLOR,
  99.                         pygame.Rect(x * 32, y * 32, 32, 32))
  100.     for x in range(grid.width):
  101.         for y in range(grid.height):
  102.             if grid.isUpdated(obj, x, y, counter) and grid.getDistanceValue(obj, x, y) >= 0:
  103.                 opacity = int(255 - getHeat(grid.getDistanceValue(obj, x, y)))
  104.                 if opacity > 255:
  105.                     opacity = 255
  106.                 elif opacity < 0:
  107.                     opacity = 0
  108.                 drawTransparentRect(x * 32, y * 32, 255, 0, 0, opacity)
  109.                 label = myfont.render(str(int(grid.getDistanceValue(obj, x, y))), 1, (255,255,0))
  110.                 screen.blit(label, (x * 32, y * 32))
  111. def getHeat(resistance):
  112.     return (50 * math.log(resistance + 5) - 50)
  113.  
  114. transSurface = pygame.Surface((32, 32))
  115. def drawTransparentRect(xLoc, yLoc, red, green, blue, opacity): # Draws a 32x32 square of the specified color and opacity
  116.     transSurface.set_alpha(opacity)
  117.     transSurface.fill((red, green, blue))
  118.     screen.blit(transSurface, (xLoc, yLoc))
  119. mGrid = wfea.Grid(20, 15)
  120. mouse = wfea.Objective(30)
  121. mouse.x = 10
  122. mouse.y = 7
  123.  
  124. pathFind = True
  125.  
  126.  
  127.  
  128. magic = wfea.WFEASearch(mGrid, [mouse])
  129. stalkerSquares = spawnSquares(50, mGrid)
  130. mGrid.addObjective(mouse)
  131. counter = 0
  132. while True:
  133.     if not wfea.DEBUG:
  134.         time_passed = clock.tick(30)
  135.     else:
  136.         time_passed = clock.tick(1)
  137.     for event in pygame.event.get():
  138.         if event.type == pygame.QUIT:
  139.             exit_game()
  140.         elif event.type == 5: # constant for mouse press I got from testing
  141.             # 1 frame outdated but should be fine
  142.             currentType = mGrid.getType(mouse.x, mouse.y)
  143.             if currentType == wfea.GRASS_BLOCK:
  144.                 mGrid.setType(mouse.x, mouse.y, wfea.WALL_BLOCK)
  145.             elif currentType == wfea.WALL_BLOCK:
  146.                 mGrid.setType(mouse.x, mouse.y, wfea.GRASS_BLOCK)
  147.             else:
  148.                 print("Odd type found when mouse pressed")
  149.         elif event.type == 2:
  150.             print(event.key)
  151.             if event.key == 114: # R
  152.                 wallChance = randrange(1, 16) # 1 - 15% chance of a wall
  153.                 for xind in range(mGrid.width):
  154.                     for yind in range(mGrid.height):
  155.                         if randrange(0, 101) < wallChance:
  156.                             mGrid.setType(xind, yind, wfea.WALL_BLOCK)
  157.                         else:
  158.                             mGrid.setType(xind, yind, wfea.GRASS_BLOCK)
  159.                 stalkerSquares = spawnSquares(len(stalkerSquares), mGrid)
  160.             elif event.key == 99: # C
  161.                 for xind in range(mGrid.width):
  162.                     for yind in range(mGrid.height):
  163.                         mGrid.setType(xind, yind, wfea.GRASS_BLOCK)
  164.             elif event.key == 100: # D
  165.                 wfea.DEBUG = not wfea.DEBUG
  166.             else:
  167.                 pathFind = not pathFind
  168.                 print("Pathfinding toggled")
  169.     screen.fill(BG_COLOR)
  170.  
  171.     temp = pygame.mouse.get_pos()
  172.     mouse.x = int(temp[0] / 32)
  173.     mouse.y = int(temp[1] / 32)
  174.     magic.updateObjectivesPF(counter)
  175.     drawGrid(mouse, mGrid, counter)
  176.     toResp = []
  177.     if pathFind:
  178.         for sqInd in range(len(stalkerSquares)):
  179.             square = stalkerSquares[sqInd]
  180.             square.update(magic, mouse, mGrid, counter)
  181.             pygame.draw.rect(screen, SQUARE_COLOR, pygame.Rect(square.x, square.y, 16, 16))
  182.             xBlock = int(square.x / 32)
  183.             yBlock = int(square.y / 32)
  184.             if mGrid.getPassingValue(xBlock, yBlock) < 0 or (xBlock == mouse.x and yBlock == mouse.y):
  185.                 toResp.append(sqInd)
  186.                 continue
  187.         for sqInd in toResp:
  188.             stalkerSquares[sqInd] = spawnSquare(mGrid)
  189.    
  190.     pygame.display.flip()
  191.     counter = counter + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement