def __init__(self, zoom): self.vel = Vector() self.source = img self.centreSource = Vector(307, 307) self.dimSource = Vector(614, 614) self.moveP = Vector(307, 307) self.imgZoom = Vector(614 * zoom, 614 * zoom)
def update(self): if self.keyboard.left: self.tMap.vel.add(Vector(0.1, 0)) if self.keyboard.right: self.tMap.vel.add(Vector(-0.1, 0)) if self.keyboard.down: self.tMap.vel.add(Vector(0, -0.1)) if self.keyboard.up: self.tMap.vel.add(Vector(0, 0.1))
def update(self): if Vector(self.mapPos[0], self.mapPos[1]).copy().subtract( Vector(pygame.mouse.get_pos()[0], pygame.mouse.get_pos() [1])).length() >= self.lightRadius - 20: self.mousePos = pygame.mouse.get_pos() if self.isSmaTorch: self.createSmaTorch() if self.isMedTorch: self.createMedTorch() if self.isBigTorch: self.createBigTorch()
class Player: def __init__(self): self.pos = Vector(CANVASWIDTH / 2, CANVASHEIGHT / 2) self.vel = Vector() self.radius = 10 self.colour = 'Green' def draw(self, canvas): canvas.draw_circle(self.pos.get_pos(), self.radius, 1, self.colour, self.colour) def update(self): self.pos.add(self.vel) self.vel.multiply(0.85)
class spawn_point: def __init__(self, pos): self.vel = Vector() self.moveP = Vector(pos[0], pos[1]) self.startPos = Vector(0, 0) self.endPos = Vector(0, 0) def draw(self, canvas): canvas.draw_circle(self.moveP.get_pos(), 10, 5, 'Red', 'Red') def update(self): self.moveP.add(self.vel) self.vel.multiply(0.85) screenPos = self.moveP
class Obstacle: def __init__(self, x1, y1, x2, y2): self.startPos = imgCentre.copy().add(Vector(x1, y1).multiply(mapZoom)) self.endPos = imgCentre.copy().add(Vector(x2, y2).multiply(mapZoom)) self.vel = Vector() self.colour = 'Blue' def draw(self, canvas): canvas.draw_line(self.startPos.get_pos(), self.endPos.get_pos(), 7, self.colour) def update(self): self.startPos.add(self.vel) self.endPos.add(self.vel) self.vel.multiply(0.85)
def update(self): self.remove = [] self.clock += 1 for enemy in self.enemies: if enemy.is_fleeing(): offset = enemy.pos.copy().subtract( Vector(self.player.mouse_pos[0], self.player.mouse_pos[1])) enemy.aim = enemy.aim.copy().add(offset) else: enemy.aim = enemy.target.copy() if self.clock % 1.5 == 0: enemy.update() if enemy.target.copy().subtract(enemy.pos).length() < 3: self.player.take_damage(enemy.attack_str) if self.clock % 5 == 0: if enemy.pos.copy().subtract( Vector(self.player.mouse_pos[0], self.player.mouse_pos[1])).length( ) <= self.player.inven.torch.lightRadius: enemy.take_damage(self.player.inven.torch.damage) if enemy.pos.copy().subtract( Vector(self.player.mouse_pos[0], self.player.mouse_pos[1]) ).length() <= self.player.inven.torch.lightRadius * (3 / 4): enemy.flee = True if enemy.pos.copy().subtract( Vector(self.player.mouse_pos[0], self.player.mouse_pos[1]) ).length() >= self.player.inven.torch.lightRadius * (6 / 4): enemy.flee = False if not enemy.is_alive(): self.remove.append(enemy) for item in self.remove: self.enemies.remove(item) self.player.update_score(5) if self.clock % 30 == 0 and len(self.enemies) < self.max_enemy: for i in range(self.max_enemy - len(self.enemies)): self.enemies.append( Shuffler(random.choice(self.spawn_points), self.settings)) self.max_enemy = 10 + (self.player.score // 60)
def draw(self, canvas): if self.visible: if self.moving == 'up': self.row = 3 if self.moving == 'right': self.row = 2 if self.moving == 'down': self.row = 0 if self.moving == 'left': self.row = 1 canvas.draw_image( self.img, (self.frameWidth * self.column + self.frameCentreX, self.frameHeight * self.row + self.frameCentreY), # center_source (self.frameWidth, self.frameHeight), # width_height_source self.pos.get_pos(), # center_dest (20, 20)) canvas.draw_line( self.pos.copy().add(Vector(-10, -20)).get_pos(), self.pos.copy().add(Vector(-10 + self.health * (2 / 3), -20)).get_pos(), 3, 'Red')
def __init__(self, position, settings_args): Enemy.__init__(self, position, True, False, 0.1, 10, 30, 0) self.img = simplegui._load_local_image( os.path.join( os.path.dirname(__file__), "../../textures/sprite_sheets" "/enemies" "/ghosts.png")) self.zombie = SpriteSheet(self.img, 12, 8, 100, 100) self.frameWidth = (self.img.get_width() / 12) self.frameHeight = (self.img.get_height() / 8) self.frameCentreX = self.frameWidth / 2 self.frameCentreY = self.frameHeight / 2 self.settings = settings_args self.target = Vector( self.settings.get('width') / 2, self.settings.get('height') / 2) self.aim = self.target.copy() self.pos = position self.row = None self.moving = 'up' self.column = 0 self.path = None
def update_rot(self): if Vector(self.x, self.y).copy().subtract( Vector(pygame.mouse.get_pos()[0], pygame.mouse.get_pos() [1])).length() >= self.inven.torch.lightRadius - 20: self.mouse_pos = pygame.mouse.get_pos() if self.mouse_pos[0] > self.x and self.mouse_pos[1] > self.y: self.rot = -(math.pi + math.asin( (self.mouse_pos[0] - self.x) / math.sqrt( math.pow(self.mouse_pos[0] - self.x, 2) + math.pow(self.mouse_pos[1] - self.y, 2)))) if self.mouse_pos[0] > self.x and self.mouse_pos[1] < self.y: self.rot = math.asin((self.mouse_pos[0] - self.x) / math.sqrt( math.pow(self.mouse_pos[0] - self.x, 2) + math.pow(self.mouse_pos[1] - self.y, 2))) if self.mouse_pos[0] < self.x and self.mouse_pos[1] < self.y: self.rot = math.asin((self.mouse_pos[0] - self.x) / math.sqrt( math.pow(self.mouse_pos[0] - self.x, 2) + math.pow(self.mouse_pos[1] - self.y, 2))) if self.mouse_pos[0] < self.x and self.mouse_pos[1] > self.y: self.rot = -(math.pi + math.asin( (self.mouse_pos[0] - self.x) / math.sqrt( math.pow(self.mouse_pos[0] - self.x, 2) + math.pow(self.mouse_pos[1] - self.y, 2))))
class tMap: def __init__(self, zoom): self.vel = Vector() self.source = img self.centreSource = Vector(307, 307) self.dimSource = Vector(614, 614) self.moveP = Vector(307, 307) self.imgZoom = Vector(614 * zoom, 614 * zoom) self.startPos = Vector(0, 0) self.endPos = Vector(0, 0) def draw(self, canvas): canvas.draw_image(img, self.centreSource.get_pos(), self.dimSource.get_pos(), self.moveP.get_pos(), self.imgZoom.get_pos()) def update(self): self.moveP.add(self.vel) self.vel.multiply(0.85) screenPos = self.moveP
try: import simplegui except ImportError: import SimpleGUICS2Pygame.simpleguics2pygame as simplegui import math, random from lib.util.vector import Vector CANVASHEIGHT = 614 CANVASWIDTH = 614 MIN_RAD = 1 img = simplegui.load_image('https://raw.githubusercontent.com/Bill-Ferny/CS1830-LightsOut/master/gameMap.jpg') imgCentre = Vector(307, 307) mapZoom = 2 class Player: def __init__(self): self.pos = Vector(CANVASWIDTH / 2, CANVASHEIGHT / 2) self.vel = Vector() self.radius = 10 self.colour = 'Green' def draw(self, canvas): canvas.draw_circle(self.pos.get_pos(), self.radius, 1, self.colour, self.colour) def update(self): self.pos.add(self.vel) self.vel.multiply(0.85)
return 'up' else: return 'right' elif self.pos.get_pos()[0] > self.frameCentreX and self.pos.get_pos( )[1] <= self.frameCentreY: if self.pos.get_pos()[0] >= -self.pos.get_pos()[1]: return 'down' else: return 'right' elif self.pos.get_pos()[0] <= self.frameCentreX and self.pos.get_pos( )[1] < self.frameCentreY: if self.pos.get_pos()[0] <= self.pos.get_pos()[1]: return 'left' else: return 'down' elif self.pos.get_pos()[0] < self.frameCentreX and self.pos.get_pos( )[1] >= self.frameCentreY: if self.pos.get_pos()[0] <= -self.pos.get_pos()[1]: return 'left' else: return 'up' return self.moving if __name__ == '__main__': settings = {'width': 1000, 'height': 750, 'font': 'monospace', 'fps': 60} frame = simplegui.create_frame("LightsOut", 1000, 750) test = Shuffler(Vector(100, 310), settings) frame.set_draw_handler(test.draw) frame.start()
def __init__(self): self.pos = Vector(CANVASWIDTH / 2, CANVASHEIGHT / 2) self.vel = Vector() self.radius = 10 self.colour = 'Green'
def __init__(self, x1, y1, x2, y2): self.startPos = imgCentre.copy().add(Vector(x1, y1).multiply(mapZoom)) self.endPos = imgCentre.copy().add(Vector(x2, y2).multiply(mapZoom)) self.vel = Vector() self.colour = 'Blue'
def collide_check(self, wall, tMap, keyboard): if (((int(wall.startPos.x) == int(wall.endPos.x)) and ((CANVASWIDTH / 2) - 7 < int(wall.startPos.x) < (CANVASWIDTH / 2) + 7) and (int(wall.endPos.y) < CANVASHEIGHT / 2 < int(wall.startPos.y) or int(wall.startPos.y) < CANVASHEIGHT / 2 < int(wall.endPos.y)))): for wall in self.Map: if keyboard.left: wall.vel.add(Vector(-1, 0)) tMap.vel.add(Vector(-1, 0)) if keyboard.right: wall.vel.add(Vector(1, 0)) tMap.vel.add(Vector(1, 0)) if keyboard.down: wall.vel.add(Vector(0, 1)) tMap.vel.add(Vector(0, 1)) if keyboard.up: wall.vel.add(Vector(0, -1)) tMap.vel.add(Vector(0, -1)) #print("Collision handled") return True elif (((int(wall.startPos.y) == int(wall.endPos.y)) and ((CANVASHEIGHT / 2) - 7 < int(wall.startPos.y) < (CANVASHEIGHT / 2) + 7) and (int(wall.endPos.x) < CANVASWIDTH / 2 < int(wall.startPos.x) or int(wall.startPos.x) < CANVASWIDTH / 2 < int(wall.endPos.x)))): for wall in self.Map: if keyboard.left: wall.vel.add(Vector(-1, 0)) tMap.vel.add(Vector(-1, 0)) if keyboard.right: wall.vel.add(Vector(1, 0)) tMap.vel.add(Vector(1, 0)) if keyboard.down: wall.vel.add(Vector(0, 1)) tMap.vel.add(Vector(0, 1)) if keyboard.up: wall.vel.add(Vector(0, -1)) tMap.vel.add(Vector(0, -1)) #print("Collision handled") return True # print("False") return False
def __init__(self, pos): self.vel = Vector() self.moveP = Vector(pos[0], pos[1]) self.startPos = Vector(0, 0) self.endPos = Vector(0, 0)
class Shuffler(Enemy): def __init__(self, position, settings_args): Enemy.__init__(self, position, True, False, 0.1, 10, 30, 0) self.img = simplegui._load_local_image( os.path.join( os.path.dirname(__file__), "../../textures/sprite_sheets" "/enemies" "/ghosts.png")) self.zombie = SpriteSheet(self.img, 12, 8, 100, 100) self.frameWidth = (self.img.get_width() / 12) self.frameHeight = (self.img.get_height() / 8) self.frameCentreX = self.frameWidth / 2 self.frameCentreY = self.frameHeight / 2 self.settings = settings_args self.target = Vector( self.settings.get('width') / 2, self.settings.get('height') / 2) self.aim = self.target.copy() self.pos = position self.row = None self.moving = 'up' self.column = 0 self.path = None def draw(self, canvas): if self.visible: if self.moving == 'up': self.row = 3 if self.moving == 'right': self.row = 2 if self.moving == 'down': self.row = 0 if self.moving == 'left': self.row = 1 canvas.draw_image( self.img, (self.frameWidth * self.column + self.frameCentreX, self.frameHeight * self.row + self.frameCentreY), # center_source (self.frameWidth, self.frameHeight), # width_height_source self.pos.get_pos(), # center_dest (20, 20)) canvas.draw_line( self.pos.copy().add(Vector(-10, -20)).get_pos(), self.pos.copy().add(Vector(-10 + self.health * (2 / 3), -20)).get_pos(), 3, 'Red') def update(self): self.path = self.aim.copy().subtract(self.pos).normalize() * 3 self.column += 1 if self.column > 2: self.column = 0 self.moving = self.get_orientation() self.pos.add(self.path) def is_fleeing(self): return self.flee def get_orientation(self): if self.pos.get_pos()[0] >= self.frameCentreX and self.pos.get_pos( )[1] > self.frameCentreY: if self.pos.get_pos()[0] >= self.pos.get_pos()[1]: return 'up' else: return 'right' elif self.pos.get_pos()[0] > self.frameCentreX and self.pos.get_pos( )[1] <= self.frameCentreY: if self.pos.get_pos()[0] >= -self.pos.get_pos()[1]: return 'down' else: return 'right' elif self.pos.get_pos()[0] <= self.frameCentreX and self.pos.get_pos( )[1] < self.frameCentreY: if self.pos.get_pos()[0] <= self.pos.get_pos()[1]: return 'left' else: return 'down' elif self.pos.get_pos()[0] < self.frameCentreX and self.pos.get_pos( )[1] >= self.frameCentreY: if self.pos.get_pos()[0] <= -self.pos.get_pos()[1]: return 'left' else: return 'up' return self.moving
import simplegui except ImportError: import SimpleGUICS2Pygame.simpleguics2pygame as simplegui from lib.player.interactions.keyboard import Keyboard import random from lib.util.vector import Vector from lib.player.Player import Player from lib.enemies.shuffler import Shuffler CANVASHEIGHT = 750 CANVASWIDTH = 1000 MIN_RAD = 1 img = simplegui.load_image( 'https://raw.githubusercontent.com/Bill-Ferny/CS1830-LightsOut/master/textures/backgrounds/maps/gameMap.jpg' ) imgCentre = Vector(307, 307) mapZoom = 2 screenPos = Vector(307, 307) class Obstacle: def __init__(self, x1, y1, x2, y2): self.startPos = imgCentre.copy().add(Vector(x1, y1).multiply(mapZoom)) self.endPos = imgCentre.copy().add(Vector(x2, y2).multiply(mapZoom)) self.vel = Vector() self.colour = 'Blue' def draw(self, canvas): canvas.draw_line(self.startPos.get_pos(), self.endPos.get_pos(), 1, self.colour)