Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import wfea
- import pygame
- import pygame.font
- import sys
- import math
- from random import *
- global GRASS_BLOCK
- global WALL_BLOCK
- GRASS_BLOCK = 0
- WALL_BLOCK = 101
- # Game parameters
- SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480
- BG_COLOR = 150, 170, 120
- WALL_COLOR = 40, 40, 40
- SQUARE_COLOR = 255, 255, 255
- class MouseFollowingSquare(wfea.WFEAObject):
- def __init__(self, x, y):
- super(MouseFollowingSquare, self).__init__()
- self.x = x
- self.y = y
- self.xVelocity = 0
- self.yVelocity = 0
- def notPassable(self, grid, x, y):
- # First get all the blocks that are being touched
- all_connected = [ [int(x / 32), int(y / 32)] ]
- if int(x / 32) != int((x + 16) / 32):
- all_connected.append([all_connected[0][0] + 1, all_connected[0][1]])
- if int(y / 32) != int((y + 16) / 32):
- all_connected.append([all_connected[0][0] + 1, all_connected[0][1] + 1])
- if int(y / 32) != int((y + 16) / 32):
- all_connected.append([all_connected[0][0], all_connected[0][1] + 1])
- for i in range(len(all_connected)):
- if grid.getPassingValue(all_connected[i][0], all_connected[i][1]) < 0:
- return True
- return False
- def update(self, magic, mouse, grid, counter):
- self.xVelocity, self.yVelocity = magic.getNextMovementVector(mouse, self, counter)
- self.xVelocity = self.xVelocity * 3
- self.yVelocity = self.yVelocity * 3
- nextX = self.x + self.xVelocity
- if self.notPassable(grid, nextX, self.y):
- self.yVelocity = self.yVelocity + 0.5
- self.xVelocity = 0
- nextY = self.y + self.yVelocity
- if self.notPassable(grid, self.x, nextY):
- self.yVelocity = 0
- self.xVelocity = self.xVelocity + 0.5
- nextX = self.x + self.xVelocity
- nextY = self.y + self.yVelocity
- if not self.notPassable(grid, nextX, nextY):
- self.x = nextX
- self.y = nextY
- pygame.init()
- screen = pygame.display.set_mode(
- (SCREEN_WIDTH, SCREEN_HEIGHT), pygame.DOUBLEBUF, 32)
- clock = pygame.time.Clock()
- def exit_game():
- pygame.quit()
- sys.exit(0)
- myfont = pygame.font.SysFont("monospace", 11)
- def spawnSquare(grid):
- xLoc = -1
- yLoc = -1
- while xLoc < 0 or yLoc < 0 or grid.getPassingValue(xLoc, yLoc) < 0:
- xLoc = randrange(1, 19)
- yLoc = randrange(1, 14)
- return MouseFollowingSquare(xLoc * 32 + 2, yLoc * 32 + 2)
- def spawnSquares(num, grid):
- res = []
- for i in range(num):
- res.append(spawnSquare(grid))
- return res
- def drawGrid(obj, grid, counter):
- for x in range(grid.width):
- for y in range(grid.height):
- if(grid.getType(x, y) == GRASS_BLOCK):
- pass
- elif(grid.getType(x, y) == WALL_BLOCK):
- pygame.draw.rect(screen, WALL_COLOR,
- pygame.Rect(x * 32, y * 32, 32, 32))
- for x in range(grid.width):
- for y in range(grid.height):
- if grid.isUpdated(obj, x, y, counter) and grid.getDistanceValue(obj, x, y) >= 0:
- opacity = int(255 - getHeat(grid.getDistanceValue(obj, x, y)))
- if opacity > 255:
- opacity = 255
- elif opacity < 0:
- opacity = 0
- drawTransparentRect(x * 32, y * 32, 255, 0, 0, opacity)
- label = myfont.render(str(int(grid.getDistanceValue(obj, x, y))), 1, (255,255,0))
- screen.blit(label, (x * 32, y * 32))
- def getHeat(resistance):
- return (50 * math.log(resistance + 5) - 50)
- transSurface = pygame.Surface((32, 32))
- def drawTransparentRect(xLoc, yLoc, red, green, blue, opacity): # Draws a 32x32 square of the specified color and opacity
- transSurface.set_alpha(opacity)
- transSurface.fill((red, green, blue))
- screen.blit(transSurface, (xLoc, yLoc))
- mGrid = wfea.Grid(20, 15)
- mouse = wfea.Objective(30)
- mouse.x = 10
- mouse.y = 7
- pathFind = True
- magic = wfea.WFEASearch(mGrid, [mouse])
- stalkerSquares = spawnSquares(50, mGrid)
- mGrid.addObjective(mouse)
- counter = 0
- while True:
- if not wfea.DEBUG:
- time_passed = clock.tick(30)
- else:
- time_passed = clock.tick(1)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- exit_game()
- elif event.type == 5: # constant for mouse press I got from testing
- # 1 frame outdated but should be fine
- currentType = mGrid.getType(mouse.x, mouse.y)
- if currentType == wfea.GRASS_BLOCK:
- mGrid.setType(mouse.x, mouse.y, wfea.WALL_BLOCK)
- elif currentType == wfea.WALL_BLOCK:
- mGrid.setType(mouse.x, mouse.y, wfea.GRASS_BLOCK)
- else:
- print("Odd type found when mouse pressed")
- elif event.type == 2:
- print(event.key)
- if event.key == 114: # R
- wallChance = randrange(1, 16) # 1 - 15% chance of a wall
- for xind in range(mGrid.width):
- for yind in range(mGrid.height):
- if randrange(0, 101) < wallChance:
- mGrid.setType(xind, yind, wfea.WALL_BLOCK)
- else:
- mGrid.setType(xind, yind, wfea.GRASS_BLOCK)
- stalkerSquares = spawnSquares(len(stalkerSquares), mGrid)
- elif event.key == 99: # C
- for xind in range(mGrid.width):
- for yind in range(mGrid.height):
- mGrid.setType(xind, yind, wfea.GRASS_BLOCK)
- elif event.key == 100: # D
- wfea.DEBUG = not wfea.DEBUG
- else:
- pathFind = not pathFind
- print("Pathfinding toggled")
- screen.fill(BG_COLOR)
- temp = pygame.mouse.get_pos()
- mouse.x = int(temp[0] / 32)
- mouse.y = int(temp[1] / 32)
- magic.updateObjectivesPF(counter)
- drawGrid(mouse, mGrid, counter)
- toResp = []
- if pathFind:
- for sqInd in range(len(stalkerSquares)):
- square = stalkerSquares[sqInd]
- square.update(magic, mouse, mGrid, counter)
- pygame.draw.rect(screen, SQUARE_COLOR, pygame.Rect(square.x, square.y, 16, 16))
- xBlock = int(square.x / 32)
- yBlock = int(square.y / 32)
- if mGrid.getPassingValue(xBlock, yBlock) < 0 or (xBlock == mouse.x and yBlock == mouse.y):
- toResp.append(sqInd)
- continue
- for sqInd in toResp:
- stalkerSquares[sqInd] = spawnSquare(mGrid)
- pygame.display.flip()
- counter = counter + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement