def __init__(self): global taskMgr, base # Initialize the ShowBase class from which we inherit, which will # create a window and set up everything we need for rendering into it. ShowBase.__init__(self) lens = OrthographicLens() lens.setFilmSize(FIELD_SZ, FIELD_SZ) base.cam.node().setLens(lens) # This code puts the standard title and instruction text on screen # self.scoreBrd = genLabelText("Test", 0) self.scoreBrd = genTextNode("TEST") # Disable default mouse-based camera control. This is a method on the # ShowBase class from which we inherit. self.disableMouse() # point camera down onto x-y plane camera.setPos(LVector3(0, 0, 1)) camera.setP(-90) # Load the background starfield. self.setBackgroundColor((0, 0, 0, 1)) self.bg = loadObject("stars.jpg", scale=FIELD_SZ, transparency=False) # Load the ship and set its initial velocity. self.ship = loadObject("ship.png", scale=2*SHIP_RAD) self.setTag("velocity", self.ship, LVector3.zero()) self.accept("escape", sys.exit) # Escape quits self.accept("space", self.endGame, [0]) # Escape quits # Now we create the task. taskMgr is the task manager that actually # calls the function each frame. The add method creates a new task. # The first argument is the function to be called, and the second # argument is the name for the task. It returns a task object which # is passed to the function each frame. self.gameTask = taskMgr.add(self.gameLoop, "gameLoop") # Stores the time at which the next bullet may be fired. self.nextBullet = 0.0 # This list will stored fired bullets. self.bullets = [] self.shipAI = ShipAI(FIELD_SZ, AI_TIME, TURN_RATE, BULLET_SPEED, BULLET_REPEAT, BULLET_RAD, AST_SPEEDS, AST_DIAMS, NUM_FRAMES, PTS) self.currFrame = 0 self.currGame = 0 self.totalScore = 0 self.bullets = [] self.asteroids = [] self.ship.setColor(1,1,1) self.newGame() self.shieldActive = False self.shieldFrames = 0
def __init__(self): global taskMgr, base # Initialize the ShowBase class from which we inherit, which will # create a window and set up everything we need for rendering into it. ShowBase.__init__(self) lens = OrthographicLens() lens.setFilmSize(WINDOW_SZ, WINDOW_SZ) base.cam.node().setLens(lens) # Disable default mouse-based camera control. This is a method on the # ShowBase class from which we inherit. self.disableMouse() # point camera down onto x-y plane camera.setPos(LVector3(0, 0, 1)) camera.setP(-90) self.setBackgroundColor((0, 0, 0, 1)) self.bg = loadObject("stars.jpg", WINDOW_SZ, (0, 0, 0, 1)) self.accept("escape", sys.exit) # Escape quits self.accept("space", self.newGame, []) # Escape quits self.startupSeparateOne() #self.startupPerformMany() #self.startupDetective() self.newGame() self.gameTask = taskMgr.add(self.gameLoop, "gameLoop")
def spawnAsteroids(self): # Control variable for if the ship is alive self.alive = True self.asteroids = [] # List that will contain our asteroids for i in range(INIT_NUM_ASTEROIDS): # This loads an asteroid. The texture chosen is random # from "asteroid1.png" to "asteroid3.png". asteroid = loadObject("circle.png", scale=AST_DIAMS[0]) self.asteroids.append(asteroid) # This is kind of a hack, but it keeps the asteroids from spawning # near the player. It creates the list (-20, -19 ... -5, 5, 6, 7, # ... 20) and chooses a value from it. Since the player starts at 0 # and this list doesn't contain anything from -4 to 4, it won't be # close to the player. WC_SZ = FIELD_SZ/2 asteroid.setX(random.choice(tuple(range(-WC_SZ, -WC_SZ/2)) + tuple(range(WC_SZ/2, WC_SZ)))) # Same thing for Y asteroid.setY(random.choice(tuple(range(-WC_SZ, -WC_SZ/2)) + tuple(range(WC_SZ/2, WC_SZ)))) # Heading is a random angle in radians heading = random.random() * 2 * pi # Converts the heading to a vector and multiplies it by speed to # get a velocity vector v = LVector3(cos(heading), sin(heading), 0) * AST_SPEEDS[0] self.setTag("velocity", self.asteroids[i], v) self.currID += 1 self.setTag("ID", asteroid, self.currID)
def fire(self): print(self.shieldActive, self.shieldFrames) direction = radians(self.ship.getH() + 90) pos = self.ship.getPos() bullet = loadObject("bullet.png", scale=BULLET_RAD*2) # Create the object bullet.setPos(pos) vel = LVector3(cos(direction), sin(direction), 0) * BULLET_SPEED self.setTag("velocity", bullet, vel) self.bullets.append(bullet)
def __init__(self): global taskMgr, base # Initialize the ShowBase class from which we inherit, which will # create a window and set up everything we need for rendering into it. ShowBase.__init__(self) lens = OrthographicLens() lens.setFilmSize(WINDOW_SZ, WINDOW_SZ) base.cam.node().setLens(lens) # Disable default mouse-based camera control. This is a method on the # ShowBase class from which we inherit. self.disableMouse() # point camera down onto x-y plane camera.setPos(LVector3(0, 0, 1)) camera.setP(-90) self.setBackgroundColor((0, 0, 0, 1)) self.bg = loadObject("stars.jpg", WINDOW_SZ, (0, 0, 0, 1)) self.accept("escape", sys.exit) # Escape quits self.accept("space", self.newGame, []) # Escape quits speed = random.random() * 45 + 5 N = WINDOW_SZ / 2 targetColor = (1, 0, 0, 1) target = loadObject("ship.png", 2*AVATAR_RAD, targetColor) targetKinematic = Kinematic(Point2(0, 0), 0, speed, target, WINDOW_SZ) #targetSteering = KinematicCircular(targetKinematic) #targetSteering = KinematicStationary(targetKinematic) targetSteering = KinematicLinear(targetKinematic) self.target = PlayerAndMovement(targetKinematic, targetSteering) avatarColor = (0, 1, 0, 1) avatar = loadObject("ship.png", 2*AVATAR_RAD, avatarColor) avatarKinematic = Kinematic(Point2(0, 0), 0, speed, avatar, WINDOW_SZ) avatarLinear = KinematicLinear(avatarKinematic) avatarSeek = KinematicSeek(avatarKinematic, targetKinematic) avatarFlee = KinematicFlee(avatarKinematic, targetKinematic) steerings = { 'Linear' : avatarLinear, 'Seek' : avatarSeek, 'Flee' : avatarFlee } fsm = SteeringFSM() self.avatarSteering = KinematicFSM(avatarKinematic, targetKinematic, fsm, steerings) self.avatar = PlayerAndMovement(avatarKinematic, self.avatarSteering) self.newGame() self.gameTask = taskMgr.add(self.gameLoop, "gameLoop")
def __init__(self): global taskMgr, base, camera, render ShowBase.__init__(self) lens = OrthographicLens() lens.setFilmSize(GAME_SZ, GAME_SZ) base.cam.node().setLens(lens) self.disableMouse() camera.setPos(LVector3(0, 0, 1)) camera.setP(-90) self.accept("escape", sys.exit) self.accept("space", self.nextConfig) self.gameTask = taskMgr.add(self.gameLoop, "gameLoop") self.whichConfig = NUM_CONFIGS - 1 self.envNP = None self.pathNP = None self.nextConfig()
def asteroidHit(self, index): currDiam = self.asteroids[index].getScale().getX() # If the asteroid is small it is simply removed if currDiam == AST_DIAMS[2]: self.asteroids[index].removeNode() del self.asteroids[index] self.hits[2] += 1 else: which = find(AST_DIAMS, currDiam) self.hits[which] += 1 # If it is big enough, divide it up into little asteroids. # First we update the current asteroid. asteroid = self.asteroids[index] newScale = AST_DIAMS[which+1] asteroid.setScale(newScale) # The new direction is chosen as perpendicular to the old direction # This is determined using the cross product, which returns a # vector perpendicular to the two input vectors. By crossing # velocity with a vector that goes into the screen, we get a vector # that is orthogonal to the original velocity in the screen plane. newSpeed = AST_SPEEDS[which+1] vel = self.getTag("velocity", asteroid) vel.normalize() vel = LVector3(0, 0, 1).cross(vel) vel *= newSpeed self.setTag("velocity", asteroid, vel) self.currID += 1 self.setTag("ID", asteroid, self.currID) # Now we create a new asteroid identical to the current one newAst = loadObject(scale=newScale) self.setTag("velocity", newAst, -vel) newAst.setPos(asteroid.getPos()) newAst.setTexture(asteroid.getTexture(), 1) self.asteroids.append(newAst) self.currID += 1 self.setTag("ID", newAst, self.currID)