def __init__(self, direction, position, health, mode, velocity, friction, maxspeed, points): self.direction = direction self.position = position self.health = health self.mode = mode self.velocity = velocity self.friction = friction self.maxspeed = maxspeed pnts_list = [] for i in points: pnts_list.append( pnt_proj(self.position, self.direction + i[0], i[1])) self.body = Polygon(pnts_list)
def line_gen(position, length, move = 0, para = True): pnt_0 = (position[0] + move[0], position[1] + move[1]) if para: pnt_1 = (position[0] + move[0], position[1] + length + move[1]) else: pnt_1 = (position[0] + length + move[0], position[1] + move[1]) return Polygon([pnt_0, pnt_1])
def poly_gen(position, points, move = (0,0)): newPoints = [] for point in points: x = point[0] + position[0] + move[0] y = point[1] + position[1] + move[1] newPoints.append((x,y)) return Polygon(newPoints)
def __init__(self, direction, position, health, mode, velocity, friction, maxspeed, points): self.direction = direction self.position = position self.health = health self.mode = mode self.velocity = velocity self.friction = friction self.maxspeed = maxspeed ship_nose = pnt_proj(self.position, self.direction, 10) ship_wing_r = pnt_proj(self.position, self.direction + 7.0 / 9 * math.pi, 10) ship_wing_l = pnt_proj(self.position, self.direction - 7.0 / 9 * math.pi, 10) ship_tail = pnt_proj(self.position, self.direction, 5.5) self.body = Polygon([ship_nose, ship_wing_l, ship_wing_r]) self.line = Polygon([ship_nose, ship_tail])
def rotate_body(body, pnt, theta): new_body = [] for poly in body: new_poly = [] for p in poly.P: dx = p[0] - pnt[0] dy = p[1] - pnt[1] mag = magnitude([dx, dy]) angle = atan2(dy, dx) new_x = pnt[0] + mag * cos(theta + angle) new_y = pnt[1] + mag * sin(theta + angle) new_p = (new_x, new_y) new_poly.append(new_p) new_body.append(Polygon(new_poly)) return new_body
def bodyrot(body, pnt, theta): new_body = [] for poly in body: new_poly = [] for p in poly.P: dx = p[0] - pnt[0] dy = p[1] - pnt[1] mag = magnitude(dx, dy) angle = math.atan2(dy,dx) new_x = pnt[0] + mag*math.cos(theta + angle) new_y = pnt[1] + mag*math.sin(theta + angle) new_p = (new_x, new_y) new_poly.append(new_p) new_body.append(Polygon(new_poly)) return new_body
def rotate(self, theta): new_body = [] for poly in self.poly_list: new_poly = [] for pnt in poly.P: dx = pnt[0] - self.position[0] dy = pnt[1] - self.position[1] mag = magnitude([dx, dy]) angle = atan2(dy, dx) new_x = self.position[0] + mag * cos(theta + angle) new_y = self.position[1] + mag * sin(theta + angle) new_pnt = (new_x, new_y) new_poly.append(new_pnt) new_body.append(Polygon(new_poly)) self.direction = self.direction + theta self.poly_list = new_body
def rect_gen(position, width, height, move = (0,0)): pnt_0 = (position[0] - width + move[0], position[1] - height + move[1]) pnt_1 = (position[0] - width + move[0], position[1] + height + move[1]) pnt_2 = (position[0] + width + move[0], position[1]- height + move[1]) pnt_3 = (position[0] + width + move[0], position[1] + height + move[1]) return Polygon([pnt_0, pnt_1, pnt_2, pnt_3])
screen.fill((0,0,0)) # meteorite if generate: met_poly_list=[] for i in met_positions: met_radius = random.randrange(10,15) met_edgenum = random.randrange(5,10) met_point_angles = [] for j in range(met_edgenum): met_point_angles.append(2 * math.pi/ float(met_edgenum) * j + math.radians(random.randrange(15))) met_point_list = [] for j in met_point_angles: met_point_list.append([i[0] + math.cos(j) * met_radius, i[1] + math.sin(j) * met_radius]) meteor = Polygon(met_point_list) met_poly_list.append(meteor) generate = False for i in met_poly_list: pygame.draw.lines(screen, (250,250,100), True, i.P) i.rotate_ip(math.pi/180) clock.tick(60) pygame.display.update() pygame.quit
class Entity: def __init__(self, direction, position, health, mode, velocity, friction, maxspeed, points): self.direction = direction self.position = position self.health = health self.mode = mode self.velocity = velocity self.friction = friction self.maxspeed = maxspeed self.vposition = (position.x - viewerPosition[0], position.y - viewerPosition[1]) pnts_list = [] for i in points: pnts_list.append( pnt_proj(self.position, self.direction + i[0], i[1])) self.body = Polygon(pnts_list) def __str__(self): return str(self.direction) + str(self.position) + str(self.velocity) def _rect(self): return self.body.get_rect() def update(self, delta_t): self.body.move_ip(self.velocity.x * (delta_t), self.velocity.y * (delta_t)) self.position = self.position.add(self.velocity.mult(delta_t)) self.velocity = self.velocity.mult(1 - self.friction * delta_t) if abs(self.velocity.x) < 0.2: self.velocity.x = 0 if abs(self.velocity.y) < 0.2: self.velocity.y = 0 self.body.C = (self.position.x, self.position.y) def turn(self, angle, delta_t): self.direction = self.direction + angle * delta_t self.body.rotate_ip(angle * delta_t) if self.direction > 2 * math.pi: self.direction = self.direction - 2 * math.pi if self.direction < 0: self.direction = self.direction + 2 * math.pi def move(self, speed): delta_v = Vector( math.cos(self.direction) * speed, math.sin(self.direction) * speed) self.velocity = self.velocity.add(delta_v) if self.velocity.magnitude > self.maxspeed: self.velocity.x = self.maxspeed * math.cos(self.direction) self.velocity.y = self.maxspeed * math.sin(self.direction) def check_bounds(self): rect = self.body.get_rect() if rect.right >= 400 and self.velocity.x > -1: self.position.x = 400 - abs(rect.right - self.position.x) self.velocity.x = 0 if rect.left <= 0 and self.velocity.x < 1: self.position.x = 0 + abs(rect.left - self.position.x) self.velocity.x = 0 if rect.top <= 0 and self.velocity.y < 1: self.position.y = 0 + abs(rect.top - self.position.y) self.velocity.y = 0 if rect.bottom >= 400 and self.velocity.y > -1: self.position.y = 400 - abs(rect.bottom - self.position.y) self.velocity.y = 0 def shoot(self): if self.mode == 0: laser = Projectile( Vector(self.position.x, self.position.y), Vector( math.cos(self.direction) * 500, (math.sin(self.direction) * 500)), 5.0, (255, 155, 155)) return laser elif self.mode == 1: laser_1 = Projectile( Vector((self.position.x + 3 * (math.cos(ship.direction + 90))), self.position.y + 3 * (math.sin(ship.direction + 90))), Vector( math.cos(self.direction) * 500, (math.sin(self.direction) * 500)), 10.0, (0, 255, 255)) laser_2 = Projectile((Vector( self.position.x + 3 * (math.cos((ship.direction - 90))), self.position.y + 3 * math.sin(ship.direction - 90))), Vector( math.cos(self.direction) * 500, math.sin(self.direction) * 500), 10.0, (0, 255, 255)) return [laser_1, laser_2]
from Vector import * from Projectile import * from Entity import * from Ship import Ship import pygame import math from pylygon import Polygon import numpy wall_l = Polygon([(0,0), (0,400)]) wall_t = Polygon([(0,0), (400,0)]) wall_b = Polygon([(0,400), (400,400)]) wall_r = Polygon([(400,0), (400,400)]) pygame.init screen = pygame.display.set_mode((400,400)) end = False ship_laser_list = [] # ship object ship = Ship((3*math.pi/2), Vector(200,300)) movement = False turnright = False turnleft = False holdingFire = False firingDelay = 0
class ShipEntity: def __init__(self, direction, position, health, mode, velocity, friction, maxspeed, points): self.direction = direction self.position = position self.health = health self.mode = mode self.velocity = velocity self.friction = friction self.maxspeed = maxspeed ship_nose = pnt_proj(self.position, self.direction, 10) ship_wing_r = pnt_proj(self.position, self.direction + 7.0 / 9 * math.pi, 10) ship_wing_l = pnt_proj(self.position, self.direction - 7.0 / 9 * math.pi, 10) ship_tail = pnt_proj(self.position, self.direction, 5.5) self.body = Polygon([ship_nose, ship_wing_l, ship_wing_r]) self.line = Polygon([ship_nose, ship_tail]) def __str__(self): return str(self.direction) + str(self.position) + str(self.velocity) def _rect(self): return self.body.get_rect() def update(self, delta_t): self.body.move_ip(self.velocity[0] * (delta_t), self.velocity[1] * (delta_t)) self.position = self.position + (self.velocity * (delta_t)) self.velocity = self.velocity * (1 - self.friction * delta_t) if abs(self.velocity[0]) < 0.2: self.velocity[0] = 0 if abs(self.velocity[1]) < 0.2: self.velocity[1] = 0 self.body.C = (self.position[0], self.position[1]) def turn(self, angle, delta_t): self.direction = self.direction + angle * delta_t self.body.rotate_ip(angle * delta_t) if self.direction > 2 * math.pi: self.direction = self.direction - 2 * math.pi if self.direction < 0: self.direction = self.direction + 2 * math.pi def move(self, speed): delta_v = array([ math.cos(self.direction) * speed, math.sin(self.direction) * speed ]) self.velocity = self.velocity + (delta_v) if magnitude(self.velocity) > self.maxspeed: self.velocity[0] = self.maxspeed * math.cos(self.direction) self.velocity[1] = self.maxspeed * math.sin(self.direction) def check_bounds(self): rect = self.body.get_rect() if rect.right >= 400 and self.velocity[0] > -1: self.position[0] = 400 - abs(rect.right - self.position[0]) self.velocity[0] = 0 if rect.left <= 0 and self.velocity[0] < 1: self.position[0] = 0 + abs(rect.left - self.position[0]) self.velocity[0] = 0 if rect.top <= 0 and self.velocity[1] < 1: self.position[1] = 0 + abs(rect.top - self.position[1]) self.velocity[1] = 0 if rect.bottom >= 400 and self.velocity[1] > -1: self.position[1] = 400 - abs(rect.bottom - self.position[1]) self.velocity[1] = 0 def shoot(self): if self.mode == 0: laser = Projectile( array([self.position[0], self.position[1]]), array([ math.cos(self.direction) * 500, math.sin(self.direction) * 500 ]), 5.0, (255, 155, 155)) return laser elif self.mode == 1: laser_1 = Projectile( array([ (self.position[0] + 3 * (math.cos(ship.direction + 90))), self.position[1] + 3 * (math.sin(ship.direction + 90)) ]), array([ math.cos(self.direction) * 500, (math.sin(self.direction) * 500) ]), 10.0, (0, 255, 255)) laser_2 = Projectile((array([ self.position[0] + 3 * (math.cos((ship.direction - 90))), self.position[1] + 3 * math.sin(ship.direction - 90) ])), array([ math.cos(self.direction) * 500, math.sin(self.direction) * 500 ]), 10.0, (0, 255, 255)) return [laser_1, laser_2]
import pygame import math from pylygon import Polygon from numpy import array wall_l = Polygon([(0, 0), (0, 400)]) wall_t = Polygon([(0, 0), (400, 0)]) wall_b = Polygon([(0, 400), (400, 400)]) wall_r = Polygon([(400, 0), (400, 400)]) pygame.init icon = pygame.image.load( "C:\Users\Evan\Desktop\Code Projects\ShuttleDefender\SD - Sprites\Shuttle.PNG" ) pygame.display.set_icon(icon) screen = pygame.display.set_mode((400, 400)) end = False ship_laser_list = [] # Creates a polygon given a position a difference in angle, and a magnitude. This difference in angle # to be a difference between a 'forward' direction and where this point should be relatively. def pnt_proj(position, angle, magnitude): pnt_x = position[0] + magnitude * math.cos(angle) pnt_y = position[1] + magnitude * math.sin(angle) return (pnt_x, pnt_y) # Similar to pnt_proj, but instead of an angle and magnitude, takes a difference in the x-axis and y-axis.
class Entity: def __init__(self, direction, position, health, mode, velocity, friction, maxspeed, points): self.direction = direction self.position = position self.health = health self.mode = mode self.velocity = velocity self.friction = friction self.maxspeed = maxspeed pnts_list = [] for i in points: pnts_list.append( pnt_proj(self.position, self.direction + i[0], i[1])) self.body = Polygon(pnts_list) def __str__(self): return str(self.direction) + str(self.position) + str(self.velocity) def _rect(self): return self.body.get_rect() def update(self, delta_t): #self.body.move_ip(self.velocity.x*(delta_t), self.velocity.y*(delta_t)) self.position = self.position.add(self.velocity.mult(delta_t)) self.velocity = self.velocity.mult(1 - self.friction * delta_t) if abs(self.velocity.x) < 0.8: self.velocity.set_x(0) if abs(self.velocity.y) < 0.8: self.velocity.set_y(0) self.body.C = (self.position.x, self.position.y) def turn(self, angle, delta_t): self.direction = self.direction + angle * delta_t self.body.rotate_ip(angle * delta_t) if self.direction > 2 * math.pi: self.direction = self.direction - 2 * math.pi if self.direction < 0: self.direction = self.direction + 2 * math.pi def move(self, speed): delta_v = Vector( math.cos(self.direction) * speed, math.sin(self.direction) * speed) self.velocity = self.velocity.add(delta_v) if self.velocity.magnitude > self.maxspeed: self.velocity.set_x(self.maxspeed * math.cos(self.direction)) self.velocity.set_y(self.maxspeed * math.sin(self.direction)) def check_bounds(self): rect = self.body.get_rect() #checks if ship is leaving the screen and stops the ship from leaving the screen #Right, Left, Top, and then Bottom if rect.right >= 400 and self.velocity.x > -1: self.position.set_x(400 - abs(rect.right - self.position.x)) self.velocity.set_x(0) if rect.left <= 0 and self.velocity.x < 1: self.position.set_x(0 + abs(rect.left - self.position.x)) self.velocity.set_x(0) if rect.top <= 0 and self.velocity.y < 1: self.position.set_y(0 + abs(rect.top - self.position.y)) self.velocity.set_y(0) if rect.bottom >= 400 and self.velocity.y > -1: self.position.set_y(400 - abs(rect.bottom - self.position.y)) self.velocity.set_y(0)
import pygame from pylygon import Polygon pygame.display.init() SCREEN_W = w = pygame.display.Info().current_w SCREEN_H = h = pygame.display.Info().current_h SCREEN_RECT = Polygon([(0, 0), (w, 0), (w, h), (0, h)])
from numpy import seterr seterr(divide='raise') _prod = lambda X: reduce(mul, X) # product if __name__ == '__main__': pygame.init() SCREEN_SIZE = (800, 600) # initialize screen size SCREEN = display.set_mode(SCREEN_SIZE) # load screen triangle = Polygon([(0, 70), (110, 0), (110, 70)]) rhombus = Polygon([(0, 80), (20, 0), (80, 0), (60, 80)]) triangle.move_ip(200, 200) rhombus.move_ip(300, 300) grab, other, theta = None, None, 0 while 1: SCREEN.fill((0, 0, 0)) draw.polygon(SCREEN, (255, 0, 0), triangle.P, 1) draw.polygon(SCREEN, (0, 0, 255), rhombus.P, 1) mouse_pos = array(mouse.get_pos()) for ev in event.get(): if ev.type == KEYDOWN: if ev.key == K_q: exit() if ev.key == K_LEFT: theta = -0.01
def main(): triangle = Polygon([(0, 70), (110, 0), (110, 70)]) rhombus = Polygon([(0, 80), (20, 0), (80, 0), (60, 80)]) triangle.move_ip(200, 200) rhombus.move_ip(300, 300) r = array([-25, -40]) # no collision for i in xrange(1000): triangle.collidepoly(rhombus) triangle.distance(rhombus, -r) triangle.raycast(rhombus, -r) # raycast collision r = array([25, 40]) for i in xrange(1000): triangle.collidepoly(rhombus) triangle.distance(rhombus, -r) triangle.raycast(rhombus, -r) # collision triangle.move_ip(*r) for i in xrange(1000): triangle.collidepoly(rhombus) triangle.distance(rhombus, -r) triangle.raycast(rhombus, -r)
import pygame import math from pylygon import Polygon import numpy wall_l = Polygon([(0, 0), (0, 400)]) wall_t = Polygon([(0, 0), (400, 0)]) wall_b = Polygon([(0, 400), (400, 400)]) wall_r = Polygon([(400, 0), (400, 400)]) pygame.init icon = pygame.image.load( "C:\Users\Evan\Desktop\Code Projects\ShuttleDefender\SD - Sprites\Shuttle.PNG" ) pygame.display.set_icon(icon) screen = pygame.display.set_mode((400, 400)) end = False ship_laser_list = [] viewerPosition = [0, 0] def pnt_proj(position, angle, magnitude): pnt_x = position.x + magnitude * math.cos(angle) pnt_y = position.y + magnitude * math.sin(angle) return (pnt_x, pnt_y) def pnt_move(position, x_d, y_d): pnt_x = position.x + x_d
from pylygon import Polygon import numpy pygame.init icon = pygame.image.load( "C:\Users\Evan\Desktop\Code Projects\ShuttleDefender\SD - Sprites\Shuttle.PNG" ) pygame.display.set_icon(icon) screen = pygame.display.set_mode((160, 160)) end = False #Automatic Fire A_position = [10, 10] A_shape = Polygon([(A_position[0] - 3, A_position[1] - 6), (A_position[0] + 3, A_position[1] - 6), (A_position[0] - 3, A_position[1] + 6), (A_position[0] + 3, A_position[1] + 6)]) A_shape2 = Polygon([(A_position[0] - 3, A_position[1] - 2), (A_position[0] + 3, A_position[1] - 2), (A_position[0] + 3, A_position[1] + 2), (A_position[0] - 3, A_position[1] + 2)]) clock = pygame.time.Clock() while not end: #events, keys, etc. tick = clock.get_time() / 1000.0 for event in pygame.event.get(): if event.type == pygame.QUIT: end = True
from operator import mul from numpy import array from pylygon import Polygon import pygame from pygame import display, draw, event, mouse, Surface from pygame.font import Font from pygame.locals import * if __name__ == '__main__': pygame.init() SCREEN_SIZE = (800, 600) # initialize screen size SCREEN = display.set_mode(SCREEN_SIZE) # load screen triangle = Polygon([(0, 70), (110, 0), (110, 70)]) rhombus = Polygon([(0, 80), (20, 0), (80, 0), (60, 80)]) triangle.move_ip(200, 200) rhombus.move_ip(300, 300) font = Font(None, 24) r = mouse.get_rel() grab, other = None, None while 1: SCREEN.fill((0, 0, 0)) draw.polygon(SCREEN, (255, 0, 0), triangle.P, 1) draw.polygon(SCREEN, (0, 0, 255), rhombus.P, 1) mouse_pos = mouse.get_pos() for ev in event.get():