def __init__(self, Screen, stack, Ship=None): self.Class = "Battleship" if (Ship): self.Ship = Ships.Battleship.Load(Ship + ".txt") else: self.Ship = Ships.Battleship() General.__init__(self, Screen, stack) self.Ship.OnBuild = (self.UpdateInfo, ()) self.Show()
def __init__(self, Screen, stack, Ship=None): self.Class = "Cruiser" if (Ship): self.Ship = Ships.Cruiser.Load(Ship + ".txt") else: self.Ship = Ships.Cruiser() General.__init__(self, Screen, stack) self.Ship.OnBuild = (self.UpdateInfo, ()) self.Show()
def spawn_enemy(self, ship_position): world_size = self.world_size stop_spawn_enemy_timer() start_spawn_enemy_timer(4000) group = pygame.sprite.Group() for i in xrange(world_size.width / self.enemy_rect.width): x = i * self.enemy_rect.width + world_size.left group.add(Ships.FloaterEnemy(x, world_size.top, FloaterMovement.DOWN, speed=2)) group.add(Ships.FloaterEnemy(x, world_size.bottom - self.enemy_rect.height, FloaterMovement.UP, speed=2)) for i in xrange(world_size.height / self.enemy_rect.height): y = i * self.enemy_rect.height + world_size.top group.add(Ships.FloaterEnemy(world_size.left, y, FloaterMovement.RIGHT, speed=3)) group.add(Ships.FloaterEnemy(world_size.right - self.enemy_rect.width, y, FloaterMovement.LEFT, speed=3)) return group
def test_isSunk(self): #make ship start = PointT(ord("A") - 65, int(1) - 1) end = PointT(ord("B") - 65, int(1) - 1) ship = Ships(2, start, end) ship.isHit(start) #shoot the ship twice and sink it ship.isHit(end) self.assertTrue(ship.isSunk()) print("test_isSunk() 1 passed.\n...")
def processCommand(cmd): if cmd == "new ship": name = input("What will this ship be called?\n") crew = input("How big will the crew of " + name + " be?\n") s1 = Ships.Ship(name, crew) return s1 elif cmd == "combat": print("This function still needs to be added\n") elif cmd == "help": print("To create a ship, type 'new ship'\n") return 1 elif cmd == "exit": input("Thanks for playing! Press Enter to quit\n") return 0 else: print("Sorry, invalid command\n") return None
if (event.type == pygame.MOUSEBUTTONUP): #print("Click") if (self.TxtBox): if (self.M[pygame.mouse.get_pos()[0]][ pygame.mouse.get_pos()[1]] != self.TxtBox): self.TxtBox.Unfocus() self.M[pygame.mouse.get_pos()[0]][pygame.mouse.get_pos() [1]].Click() if (__name__ == "__main__"): pygame.init() S = Screen(Constants.Window_With, Constants.Window_Height) S.draw() if (False): s = Ships.Corvete() s.Core.Equip(Modules.Corvete.Core.Interceptor) s.Core.Thing.Weapon1.Equip(Weapons.Small.Lasers.Laser_T1) s.Core.Thing.Utility1.Equip(Utilitys.Small.Shields.Shield_T1) s.SetName("Party") s.Save() l = Ships.Corvete.Load("Party.txt") l.SetName("P2") l.Save() B = Buttons.Button(S.M, (50, 50), (50, 50)) S.stack = stack = [] #Main Menu Interface.Menu.Active(S, stack) #Ship Design
def __init__(self, world_size): self.interval = 100 self.world_size = world_size self.enemy_rect = Ships.FloaterEnemy().rect
import Ships import DataAnalysis as da if __name__ == "__main__": computer1 = Ships.Computer('Computer1', 'easy') computer2 = Ships.Computer('Computer2', 'master') computer1.place_ships() computer2.place_ships() computer1.copy_guess(computer2) computer2.copy_guess(computer1) turn_counter = 1 while True: computer1.play(computer2) print("computer 1") computer1.print_board(computer1.guessing_map) print(computer1.guesses) computer2.play(computer1) print("computer 2") computer2.print_board(computer2.guessing_map) print(computer2.guesses) turn_counter += 1 a = Ships.end_game(computer1, computer2) if a: break print("FINISHED GAME")
def generateShip(self): material = Util.randomDict(self.materials) enclosure = Util.randomDict(self.enclosure) symmetry = Util.randomDict(self.symmetry) accel = random.uniform(self.accel[ACCEL_MIN], self.accel[ACCEL_MAX]) if (not Parts.thrusters[self.size]): ##### ## # warn about no thrusters of appropriate size ## ##### accel = 0 accelFactorFwd = random.uniform(self.accel[ACCEL_FWD], 1) accelFactorLat = random.uniform(self.accel[ACCEL_LAT], accelFactorFwd) turn = random.uniform(self.turn[TURN_MIN], self.turn[TURN_MAX]) if (not Parts.gyros[self.size]): ##### ## # warn about no gyros of appropriate size ## ##### turn = 0 power = Util.randomDict(self.power) if (not Parts.reactors[self.size]): ##### ## # warn about no reactors of appropriate size ## ##### power = None # select parts and rooms partCounts = {} for part in self.parts.keys(): n = Dists.dists[self.parts[part][PART_DISTRIBUTION]].getCount() if ((PART_MIN in self.parts[part]) and (n < self.parts[part][PART_MIN])): n = self.parts[part][PART_MIN] if ((PART_MAX in self.parts[part]) and (n > self.parts[part][PART_MAX])): n = self.parts[part][PART_MAX] if (n > 0): partCounts[part] = n roomCounts = {} for room in self.rooms.keys(): n = Dists.dists[self.rooms[room][ROOM_DISTRIBUTION]].getCount() if ((ROOM_MIN in self.rooms[room]) and (n < self.rooms[room][ROOM_MIN])): n = self.rooms[room][ROOM_MIN] if ((ROOM_MAX in self.rooms[room]) and (n > self.rooms[room][ROOM_MAX])): n = self.rooms[room][ROOM_MAX] if (n > 0): roomCounts[room] = n self.fixCounts(partCounts, roomCounts) roomCounts[Rooms.EXTERIOR] = 1 roomCounts[Rooms.INTERIOR] = 1 partsMass = 0 partsPower = 0 maxPartPower = 0 for part in partCounts.keys(): if (part not in Parts.parts[self.size]): continue partsMass += Parts.parts[self.size][part].mass * partCounts[part] partsPower += Parts.parts[self.size][part].power * partCounts[part] if (Parts.parts[self.size][part].power > maxPartPower): maxPartPower = Parts.parts[self.size][part].power # select number of doors if ((self.doors) and (self.doors[DOOR_DISTRIBUTION] in Dists.dists)): doors = Dists.dists[self.doors[DOOR_DISTRIBUTION]].getCount() if ((DOOR_MIN in self.doors) and (doors < self.doors[DOOR_MIN])): doors = self.doors[DOOR_MIN] if ((DOOR_MAX in self.doors) and (doors > self.doors[DOOR_MAX])): doors = self.doors[DOOR_MAX] else: doors = random.choice(([1] * 6) + ([2] * 5) + ([3] * 4) + ([4] * 3)) # assign parts to rooms rooms = {} enclosureIndex = ENCLOSURE_SCALE.index(enclosure) for room in roomCounts.keys(): if (roomCounts[room] <= 0): continue rooms[room] = [] freeRange = (Rooms.rooms[room].free[FREE_MIN], Rooms.rooms[room].free[FREE_MAX]) for i in xrange(roomCounts[room]): roomDict = {} for (part, partDict) in Rooms.rooms[room].parts.items(): roomDict[part] = partDict[PART_MIN] partCounts[part] -= roomDict[part] freeFactor = random.uniform(*freeRange) roomMaterial = Util.randomDict(Rooms.rooms[room].materials) roomEnclosure = Util.randomDict(Rooms.rooms[room].enclosure) if (ENCLOSURE_SCALE.index(roomEnclosure) < enclosureIndex): roomEnclosure = ENCLOSURE_SCALE[enclosureIndex] rooms[room].append((roomDict, freeFactor, roomMaterial, roomEnclosure)) Ships.assignPartsToRooms(rooms, self.size, partCounts) # generate layout and add thrusters, gyros, and reactors thrusters = {} thrustersMass = {} thrustersThrust = {} thrustersPower = {} gyros = {} gyrosMass = 0 gyrosTurn = 0 gyrosPower = 0 reactors = {} reactorsMass = 0 reactorsPower = 0 needsWork = True iterations = 0 while (needsWork): needsWork = False # generate layout ship = Ships.layoutShip(self.shipType, material, enclosure, symmetry, rooms, thrusters, gyros, reactors, doors) # after a few tries, accept that the peformance we want may not be possible with the parts we have if (iterations > COMPROMISE_THRESHOLD): if ((accel > self.accel[ACCEL_MIN]) or (turn > self.turn[TURN_MIN])): # reduce acceleration and turning if there's room to do so accel = max(accel * COMPROMISE_FACTOR, self.accel[ACCEL_MIN]) turn = max(turn * COMPROMISE_FACTOR, self.turn[TURN_MIN]) elif ((accelFactorFwd > self.accel[ACCEL_FWD]) or (accelFactorLat > self.accel[ACCEL_LAT])): # reduce lateral and forward-facing thrust factors if there's room to do so accelFactorLat = max(accelFactorLat * COMPROMISE_FACTOR, self.accel[ACCEL_LAT]) accelFactorFwd = max(accelFactorFwd * COMPROMISE_FACTOR, self.accel[ACCEL_FWD], accelFactorLat) else: # reduce power requirements if there's room p = power while (p in COMPROMISE_POWER): p = COMPROMISE_POWER[self.power] if (self.power.get(p, 0) > 0): power = p break if (p not in COMPROMISE_POWER): # no room to reduce power requirements; the ship's as good as it's going to get ##### ## #warn about underperforming ship ## ##### break iterations += 1 # determine mass mass = partsMass + ship.structureMass + sum(m for m in thrustersMass.values()) + gyrosMass + reactorsMass # add thrusters if necessary thrustReq = {ACCEL: mass * accel} thrustReq[ACCEL_FWD] = thrustReq[ACCEL] * accelFactorFwd thrustReq[ACCEL_LAT] = thrustReq[ACCEL] * accelFactorLat for d in thrustReq.keys(): if (thrustersThrust.get(d, 0) < thrustReq[d]): needsWork = True # remove existing thrusters to realize efficiency gains from larger thrusters thrusters[d] = {} thrustersMass[d] = 0 thrustersThrust[d] = 0 thrustersPower[d] = 0 # add necessary thrusters while (thrustReq[d] > 0): for thruster in Parts.thrusters.get(self.size, []): if (thruster.thrust > thrustReq[d]): break n = max(int(math.ceil(thrustReq[d] / thruster.thrust)), 1) thrusters[d][thruster] = thrusters[d].get(thruster, 0) + n thrustersMass[d] += n * thruster.mass * THRUST_DIRECTION_COUNTS[d] thrustersThrust[d] += n * thruster.thrust thrustersPower[d] += n * thruster.power thrustReq[d] -= n * thruster.thrust # add gyros if necessary turnReq = mass * turn if (gyrosTurn < turnReq): needsWork = True # remove existing gyros to realize efficiency gains from larger gyros gyros = {} gyrosMass = 0 gyrosTurn = 0 gyrosPower = 0 # add necessary gyros while (turnReq > 0): for gyro in Parts.gyros.get(self.size, []): if (gyro.turn > turnReq): break n = max(int(math.ceil(turnReq / gyro.turn)), 1) gyros[gyro] = gyros.get(gyro, 0) + n gyrosMass += n * gyro.mass gyrosTurn += n * gyro.turn gyrosPower += n * gyro.power turnReq -= n * gyro.turn # add reactors if necessary if (power == POWER_MIN): # largest of: 150% of aft-facing thrusters, all gyros, largest single part powerReq = max(1.5 * thrustersPower[ACCEL], gyrosPower, maxPartPower) elif (power == POWER_STD): # largest of: 150% of aft and single lateral thrusters, 150% of aft thrusters and all gyros, all parts powerReq = max(1.5 * (thrustersPower[ACCEL] + thrustersPower[ACCEL_LAT]), 1.5 * thrustersPower[ACCEL] + gyrosPower, partsPower) elif (power == POWER_HIGH): # largest of: 150% of aft and two lateral thrusters, 150% of aft and single lateral thrusters and all gyros, # 100% of aft-facing thrusters plus all gyros and all parts powerReq = max(1.5 * (thrustersPower[ACCEL] + 2 * thrustersPower[ACCEL_LAT]), 1.5 * (thrustersPower[ACCEL] + thrustersPower[ACCEL_LAT]) + gyrosPower, thrustersPower[ACCEL] + gyrosPower + partsPower) elif (power == POWER_MAX): # 150% of aft and two lateral thrusters plus all gyros and all parts powerReq = 1.5 * (thrustersPower[ACCEL] + 2 * thrustersPower[ACCEL_LAT]) + gyrosPower + partsPower else: # no power requirements, so zero powerReq = 0 if (reactorsPower < powerReq): needsWork = True # remove existing reactors to realize efficiency gains from larger reactors reactors = {} reactorsMass = 0 reactorsPower = 0 # add necessary reactors while (powerReq > 0): # note reversal of signs below, as reactor.power is negative for reactor in Parts.reactors.get(self.size, []): if (-reactor.power > powerReq): break n = max(int(math.ceil(powerReq / -reactor.power)), 1) reactors[reactor] = reactors.get(reactor, 0) + n reactorsMass += n * reactor.mass reactorsPower -= n * reactor.power powerReq += n * reactor.power ##### ## #... print "material: %s"%material print "enclosure: %s"%enclosure print "symmetry: %s"%symmetry print "accel: %s (%s, %s)"%(accel,accelFactorFwd,accelFactorLat) print "turn: %s"%turn print "power: %s"%power print "parts mass: %s"%partsMass print "part assignments: %s"%rooms print "thrusters: %s"%thrusters print "accel: %s (%s, %s)"%(thrustersThrust[ACCEL]/mass,thrustersThrust[ACCEL_FWD]/mass,thrustersThrust[ACCEL_LAT]/mass) print "gyros: %s (turn: %s)"%(gyros,gyrosTurn/mass) print "reactors: %s (power: %s)"%(reactors,reactorsPower) print "structure mass: %s"%ship.structureMass print "mass: %s"%mass if (ship.occupied): minCoords = [None,None,None] maxCoords = [None,None,None] for x in ship.occupied.keys(): if ((minCoords[0] is None) or (x<minCoords[0])): minCoords[0]=x if ((maxCoords[0] is None) or (x>maxCoords[0])): maxCoords[0]=x for y in ship.occupied[x].keys(): if ((minCoords[1] is None) or (y<minCoords[1])): minCoords[1]=y if ((maxCoords[1] is None) or (y>maxCoords[1])): maxCoords[1]=y for z in ship.occupied[x][y]: if ((minCoords[2] is None) or (z<minCoords[2])): minCoords[2]=z if ((maxCoords[2] is None) or (z>maxCoords[2])): maxCoords[2]=z partsByLoc = {} import itertools for (part, partPos, partFwd, partUp) in ship.parts.values(): for blockPos in itertools.product(*[xrange(s) for s in Parts.parts[ship.size][part].size]): partsByLoc[tuple(Ships.addList(blockPos, partPos))] = part[0].lower() print "ship slices: (x: %s..%s, y: %s..%s)" % (minCoords[0], maxCoords[0], minCoords[1], maxCoords[1]) for z in xrange(minCoords[2],maxCoords[2]+1): print "z=%s"%z for y in xrange(minCoords[1],maxCoords[1]+1): line = "" for x in xrange(minCoords[0],maxCoords[0]+1): if ((x,y,z) in ship.windows): line+="W" elif ((x,y,z) in ship.doorways): line+="D" elif ((x,y,z) in partsByLoc): line+=partsByLoc[(x,y,z)] else: line += ship.structure.get((x,y,z), (" ",))[0][0] print line else: print "no ship contents" ## ##### return ship