def __init__(self, parent): self.parent = parent globalvars.asdf = 0 self.lagcount = 0 self.leftkeydown = 0 self.rightkeydown = 0 self.enemylist = [] self.list_enemys = EnemyManager() self.stage = Stage(self.list_enemys, globalvars.player_list) self.list_allie_shots = pygame.sprite.RenderUpdates() self.enemy_shots = pygame.sprite.RenderUpdates()
def __init__(self,parent): self.parent=parent globalvars.asdf = 0 self.lagcount=0 self.leftkeydown=0 self.rightkeydown=0 self.enemylist=[] self.list_enemys=EnemyManager() self.stage=Stage(self.list_enemys,globalvars.player_list) self.list_allie_shots=pygame.sprite.RenderUpdates() self.enemy_shots=pygame.sprite.RenderUpdates()
def __init__(self): ShowBase.__init__(self) base.setFrameRateMeter(True) set_app(self) app = self # Rocket Gui LoadFontFace("gui/Raleway.otf") self.region = RocketRegion.make('pandaRocket', app.win) self.region.setActive(1) self.context = self.region.getContext() self.hud = self.context.LoadDocument('gui/hud.rml') ih = RocketInputHandler() app.mouseWatcher.attachNewNode(ih) self.region.setInputHandler(ih) self.hud.Show() self.cTrav = CollisionTraverser('coltrav') #self.cTrav.showCollisions(self.render) self.ai_world = AIWorld(render) self.enableParticles() self.scene = [] self.player = Player(self) self.scene.append(self.player) self.backgrounds = self.gen_background() self.enemy_manager = EnemyManager(self) self.enemies = self.gen_enemies(self.scene) self.bullet_manager = BulletManager(self) self.bullet_count = 0 self.taskMgr.add(self.update, "update") self.mouse_pos = Point2(0,0) self.set_keys() self.music = self.loader.loadSfx('sounds/onegameamonthjan.ogg') self.music.setVolume(0.2) self.music.play() self.item_manager = ItemManager(self) self.tree_manager = TreeManager(self) self.trees = self.gen_trees()
def __init__(self, parent): self.parent = parent globalvars.asdf = 0 ##some key vars, the works self.lagcount = 0 #a little hack so that the background works nicer self.background = globalvars.screen #make the rectlist to handle dirty updating self.updaterects = [] #background object self.bgstars = BackgroundManager() ##make the lists to handle various sprites self.list_enemys = EnemyManager() self.player_list = pygame.sprite.RenderUpdates() self.list_allie_shots = pygame.sprite.OrderedUpdates() self.enemy_shots = pygame.sprite.RenderUpdates() self.token_list = pygame.sprite.RenderUpdates() ##make a new stage object self.stage = Stage(self, self.list_enemys, self.player_list, self.enemy_shots, self.list_allie_shots) #self.setup_events() self.new_display()
def __init__(self,parent): self.parent=parent globalvars.asdf = 0 ##some key vars, the works self.lagcount=0 #a little hack so that the background works nicer self.background=globalvars.screen #make the rectlist to handle dirty updating self.updaterects=[] #background object self.bgstars=BackgroundManager() ##make the lists to handle various sprites self.list_enemys=EnemyManager() self.player_list=pygame.sprite.RenderUpdates() self.list_allie_shots=pygame.sprite.OrderedUpdates() self.enemy_shots=pygame.sprite.RenderUpdates() self.token_list=pygame.sprite.RenderUpdates() ##make a new stage object self.stage=Stage(self,self.list_enemys,self.player_list,self.enemy_shots,self.list_allie_shots) #self.setup_events() self.new_display()
from pygame import mixer import player import screen from object_manager import ObjectManager from enemy import EnemyManager from help import getch, log player = player.Player(x=2, y=4, px=2, py=2, health=5, inventory=[]) move_commands = ['d', 'a', 's', 'w'] attack_commands = ['j', 'k', 'l', 'i', 'm'] map = screen.MapController() enemy_manager = EnemyManager() object_manager = ObjectManager() mixer.init() mixer.Sound("sounds/noodle.wav").play(-1) playing = True # set console size command = 'mode con: cols={} lines={}'.format(map.columns, map.rows+2) os.system(command) # player ui, world ui pui = "" wui = "" while playing: # updates the map with player pos, all room objects
class Gamelolz: #This is the __init__ #its important. def __init__(self, parent): self.parent = parent globalvars.asdf = 0 ##some key vars, the works self.lagcount = 0 #a little hack so that the background works nicer self.background = globalvars.screen #make the rectlist to handle dirty updating self.updaterects = [] #background object self.bgstars = BackgroundManager() ##make the lists to handle various sprites self.list_enemys = EnemyManager() self.player_list = pygame.sprite.RenderUpdates() self.list_allie_shots = pygame.sprite.OrderedUpdates() self.enemy_shots = pygame.sprite.RenderUpdates() self.token_list = pygame.sprite.RenderUpdates() ##make a new stage object self.stage = Stage(self, self.list_enemys, self.player_list, self.enemy_shots, self.list_allie_shots) #self.setup_events() self.new_display() def new_display(self): self.side_panel = pygame.sprite.RenderUpdates() self.points = Points() self.side_panel.add(self.points) self.health = Health() self.healthbar = HealthBar(self.health) self.livesbar = LivesBar(self.health, self.healthbar) self.side_panel.add(self.healthbar) self.side_panel.add(self.health) self.side_panel.add(self.livesbar) #clears all the variables def clear_vars(self): self.leftkeydown = 0 self.rightkeydown = 0 self.points.set_points(0) globalvars.x = 400 globalvars.y = globalvars.WIN_RESY - 60 self.stage.set_stage(0) self.list_enemys.empty() self.list_allie_shots.empty() self.player_list.empty() self.enemy_shots.empty() print "Game Restarted" def register_inputs(self): import defaultinputs defaultinputs.setup(self.inputmanager, self) #define function to draw player ship on X, Y plane def pship(self): self.player_list.clear(globalvars.surface, self.background) self.updaterects += self.player_list.draw(globalvars.surface) #Define function to move the enemy ship def emove(self): self.list_enemys.clear(globalvars.surface, self.background) self.updaterects += self.list_enemys.draw(globalvars.surface) #So i'm trying out having the program check for collisions, instead of the enemy objects #i think i might switch to the objects, but still keep this function just hand the computing to the object #seems most efficient def test_collision(self): todie = ecollision.groupcollide(self.list_enemys, self.list_allie_shots, 0, 0, ecollision.BOTTOM) #todie=pygame.sprite.groupcollide(self.list_enemys, self.list_allie_shots,0,0) #print todie for enemy, bullet in todie.iteritems(): bullet.set_hit(1) enemy.hit(bullet.damage) self.points.add_points(1) for q in pygame.sprite.spritecollide(self.player, self.enemy_shots, 0): #for q in ecollision.spritecollide(self.player, self.enemy_shots,0,ecollision.TOP): #print "ZOMFG SHOTZORZ" self.player.set_hit(q.damage) self.enemy_shots.remove(q) #if self.token_list: # for token in ecollision.spritecollide(self.player, self.token_list,0, ecollision.TOP): # print token #if there are no enemys left, go to the next stage def check_done(self): if not globalvars.asdf % 20 and not self.list_enemys: #the %20 lets it wait a fraction of a second self.stage.next_stage() #checks to see if we can expand the ranges of the bots so its nice and.... umm... nice. def check_rows(self): if globalvars.asdf % 20 == 0: self.list_enemys.check_enemy_rows() #major hack just to get this thing playable..... sorry def again(self): if self.player.health <= 0: return False return True #this is called if the player shoots... to be honest i dont know why this is a function def pshoot(self, **args): self.player.shoot() #draws the bullet.... duh. come on dude. def drawbullets(self): #for x in self.list_allie_shots: #x.draw() self.list_allie_shots.clear(globalvars.surface, self.background) self.enemy_shots.clear(globalvars.surface, self.background) self.updaterects += self.list_allie_shots.draw(globalvars.surface) self.updaterects += self.enemy_shots.draw(globalvars.surface) #... def drawsidepanel(self): if globalvars.asdf % 5 == 0: self.side_panel.update() self.side_panel.clear(globalvars.surface, self.background) self.updaterects += self.side_panel.draw(globalvars.surface) #dont ask why i made this a function, i made draw<asdf> for everythign else #so figured id make this too def drawtokens(self): if self.token_list: self.token_list.clear(globalvars.surface, self.background) self.updaterects += self.token_list.draw(globalvars.surface) #goes through all the arrays and makes each of them move 1 space, simple and easy yet it deserves a comment... def tick(self): self.bgstars.update() self.list_allie_shots.update() self.list_enemys.update() self.enemy_shots.update() self.token_list.update() self.player.update() ###################### #heres a bunch of metafunctions #i break it up so its really easy to add new features #like if we ant a counter? add something to check() and draw() #all of these are called once per frame def check(self): self.check_done() self.test_collision() self.check_rows() self.list_enemys.shoot(self.stage.enemyodds) def draw(self): self.updaterects += self.bgstars.draw() self.updaterects += self.bgstars.clear() self.drawbullets() self.pship() self.emove() self.drawsidepanel() self.drawtokens() #does just what it sounds like..... def clear_screen(self): globalvars.surface.fill(globalvars.bgcolor) pygame.display.flip() #for debugging info mostly... No entirely def dispvars(self): print "The Enemy Array size is:", len(self.list_enemys.sprites()) print "The Player Shot Array size is:", len( self.list_allie_shots.sprites()) print "The Enemy Shot Array size is:", len(self.enemy_shots.sprites()) #does lots and lots of stuff, it really needs to be cleaned up def input(self, events): self.inputmanager.check(events) def mousemove(self, parent, event): tempx = pygame.mouse.get_pos()[0] - parent.player.rect.width / 2 ## Just to make sure we don't get the ship way out there: if tempx > globalvars.xmax: #if its outside the globalvars.window, just stick it as far as possible parent.player.move(globalvars.xmax, globalvars.y) elif tempx < globalvars.xmin: parent.player.move(globalvars.xmin, globalvars.y) elif abs( tempx - globalvars.x ) > globalvars.smooth_scroll_var1: #smooth scrolling if the mouse gets far from the ship parent.player.move( parent.player.get_pos().left + (tempx - parent.player.get_pos().left) / globalvars.smooth_scroll_var2, globalvars.y) else: #if it gets down to this point, #we've passed all sanity checks so just move it parent.player.move(tempx, globalvars.y) ################################################################################################################## #pretty simple def start(self): self.clear_vars() self.player = Player(self.player_list, self.token_list, Gun(self.list_allie_shots, Bullet)) self.player_list.add(self.player) self.player.set_pos(globalvars.x, globalvars.y) self.health.link_sprite(self.player) self.inputmanager = self.parent.inputmanager self.register_inputs() self.loop() #Yeah see this one does all of the work def loop(self): #start loop while self.again(): #refresh self.background...needs to be done once in a while if globalvars.asdf >= globalvars.REFRESH_TIME: #self.clear_screen() globalvars.asdf = 0 globalvars.asdf += 1 #check everythign and see if changes need to be made self.check() #move everything 1 self.tick() #initiate input function self.input(pygame.event.get()) #draw everything self.draw() #applies the smart screen updating #globalvars.surface.set_alpha(150,pygame.RLEACCEL) ......dont ask why #globalvars.surface2.blit(globalvars.surface,globalvars.surfacerect) pygame.display.update(self.updaterects) self.updaterects = [] #pauses and waits self.time = globalvars.clock.get_time() if self.time > globalvars.FPS: timeittook = globalvars.clock.tick(globalvars.FPS - (self.time - globalvars.FPS)) print "Lag at frame %s\t%sms" % (globalvars.asdf, self.time) else: timeittook = globalvars.clock.tick(globalvars.FPS)
class Gamelolz: #This is the __init__ #its important. def __init__(self, parent): self.parent = parent globalvars.asdf = 0 self.lagcount = 0 self.leftkeydown = 0 self.rightkeydown = 0 self.enemylist = [] self.list_enemys = EnemyManager() self.stage = Stage(self.list_enemys, globalvars.player_list) self.list_allie_shots = pygame.sprite.RenderUpdates() self.enemy_shots = pygame.sprite.RenderUpdates() #clears all the variables def clear_vars(self): self.leftkeydown = 0 self.rightkeydown = 0 health.set_health(globalvars.max_health) points.set_points(0) globalvars.x = 400 globalvars.y = globalvars.WIN_RESY - 60 self.stage.set_stage(-1) #hax globalvars.enemy_bullet_odds = 100 self.list_enemys.empty() self.list_allie_shots.empty() globalvars.player_list.empty() self.enemy_shots.empty() print "Game Restarted" #define function to draw player ship on X, Y plane def pship(self, x, y): globalvars.player_list.clear(globalvars.surface, globalvars.screen) self.enemylist += globalvars.player_list.draw(globalvars.surface) #Define function to move the enemy ship def emove(self): self.list_enemys.clear(globalvars.surface, globalvars.screen) self.enemylist += self.list_enemys.draw(globalvars.surface) #draws all the enemys you ask it def draw_enemys(self): #k so now some recursive loops: for enemycol in range(self.stage.get_stage()[0]): #now for the rows for enemyrow in range(self.stage.get_stage()[1]): #make a new enemy object: tempenemy = Enemy(self.list_enemys) #this ones a long one, but it works: tempenemy.set_pos( globalvars.xmin + enemycol * (globalvars.enemy_width + globalvars.enemy_spacing_x), globalvars.ymin + enemyrow * (globalvars.enemy_height + globalvars.enemy_spacing_y) - 150) #this one is even worse, but works even better: tempenemy.set_range( globalvars.xmin + enemycol * (globalvars.enemy_width + globalvars.enemy_spacing_x), globalvars.xmax - (self.stage.get_stage()[0] - enemycol) * (globalvars.enemy_height + globalvars.enemy_spacing_x)) #now add the temp enemy to the array and we're good to go self.list_enemys.add(tempenemy) #So i'm trying out having the program check for collisions, instead of the enemy objects #i think i might switch to the objects, but still keep this function just hand the computing to the object #seems most efficient def test_collision(self): todie = pygame.sprite.groupcollide(self.list_enemys, self.list_allie_shots, 0, 0) #print todie for enemy, bullet in todie.iteritems(): self.list_allie_shots.remove(bullet) enemy.set_state(0) points.add_points(1) if pygame.sprite.spritecollideany(self.player, self.enemy_shots): #print "ZOMFG SHOTZORZ" self.player.set_hit() health.hit() #if there are no enemys left, go to the next stage def check_done(self): if not self.list_enemys: self.stage.next_stage() self.draw_enemys() #checks to see if we can expand the ranges of the bots so its nice and.... umm... nice. def check_rows(self): if globalvars.asdf % 20 == 0: #simple sorting algorithm to find the highest values highest = globalvars.xmin lowest = globalvars.xmax for enemy in self.list_enemys: if enemy.get_range()[1] > highest: highest = enemy.get_range()[1] if enemy.get_range()[0] < lowest: lowest = enemy.get_range()[0] highest = globalvars.xmax - highest lowest = lowest - globalvars.xmin if highest != 0 or lowest != 0: #makes things |--| this much more efficient for enemy in self.list_enemys: erange = enemy.get_range() enemy.set_range(erange[0] - lowest, erange[1] + highest) #major hack just to get this thing playable..... sorry def again(self): if health.get_health() <= 0: return False return True #this is called if the player shoots def pshoot(self, sx, sy): self.player.shoot(self.list_allie_shots, sx, sy) #draws the bullet.... duh. come on dude. def drawbullets(self): #for x in self.list_allie_shots: #x.draw() self.list_allie_shots.clear(globalvars.surface, globalvars.screen) self.enemy_shots.clear(globalvars.surface, globalvars.screen) self.enemylist += self.list_allie_shots.draw(globalvars.surface) self.enemylist += self.enemy_shots.draw(globalvars.surface) #... def drawsidepanel(self): if globalvars.asdf % 5 == 0: globalvars.side_panel.update() globalvars.side_panel.clear(globalvars.surface, globalvars.screen) self.enemylist += globalvars.side_panel.draw(globalvars.surface) #goes through all the arrays and makes each of them move 1 space, simple and easy yet it deserves a comment... def tick(self): self.list_allie_shots.update() self.list_enemys.update() self.enemy_shots.update() ###################### #heres a bunch of metafunctions #i break it up so its really easy to add new features #like if we ant a counter? add something to check() and draw() #all of these are called once per frame def check(self): self.check_done() self.test_collision() self.check_rows() bgstars.update() self.list_enemys.shoot(self.enemy_shots) self.player.update() def draw(self): self.enemylist += bgstars.draw() self.enemylist += bgstars.clear() self.drawbullets() self.pship(globalvars.x, globalvars.y) self.emove() self.drawsidepanel() #does just what it sounds like..... def clear_screen(self): globalvars.surface.fill(globalvars.bgcolor) pygame.display.flip() #for debugging info mostly def dispvars(self): print "The Enemy Array size is:", len(self.list_enemys.sprites()) print "The Player Shot Array size is:", len( self.list_allie_shots.sprites()) print "The Enemy Shot Array size is:", len(self.enemy_shots.sprites()) #does lots and lots of stuff, it really needs to be cleaned up def input(self, events): global x global y pygame.event.pump( ) #somewhere in their docs it said this line was a good idea for event in events: if event.type == QUIT: sys.exit(0) if event.type == pygame.MOUSEMOTION: pygame.event.get() tempx = pygame.mouse.get_pos()[0] - self.player.rect.width / 2 ## Just to make sure we don't get the ship way out there: if tempx > globalvars.xmax: #if its outside the globalvars.window, just stick it as far as possible self.player.move(globalvars.xmax, globalvars.y) elif tempx < globalvars.xmin: self.player.move(globalvars.xmin, globalvars.y) elif abs( tempx - globalvars.x ) > globalvars.smooth_scroll_var1: #smooth scrolling if the mouse gets far from the ship self.player.move( self.player.get_pos().left + (tempx - self.player.get_pos().left) / globalvars.smooth_scroll_var2, globalvars.y) else: #if it gets down to this point, #we've passed all sanity checks so just move it self.player.move(tempx, globalvars.y) ## if the mouse is clicked, shoot! if event.type == pygame.MOUSEBUTTONDOWN: self.pshoot( self.player.rect.centerx - globalvars.BULLET_WIDTH / 2, globalvars.y) ## if 'q' is pressed, quit if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: sys.exit(0) if event.key == pygame.K_p: menulists.pause_menu() if event.key == pygame.K_ESCAPE: sys.exit(0) #keyboard controls if event.key == pygame.K_LEFT: self.leftkeydown = 1 if event.key == pygame.K_RIGHT: self.rightkeydown = 1 if event.key == pygame.K_SPACE: self.pshoot( self.player.rect.centerx - globalvars.BULLET_WIDTH / 2, globalvars.y) #keyboard controls if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: self.leftkeydown = 0 if event.key == pygame.K_RIGHT: self.rightkeydown = 0 if self.leftkeydown: self.player.move_one(0) if self.rightkeydown: self.player.move_one(1) pygame.event.clear() ################################################################################################################## #pretty simple def start(self): self.clear_vars() self.player = Player() globalvars.player_list.add(self.player) self.player.set_pos(globalvars.x, globalvars.y) self.loop() #Yeah see this one does all of the work def loop(self): #start loop while self.again(): #refresh globalvars.screen...needs to be done once in a while if globalvars.asdf >= globalvars.REFRESH_TIME: #self.clear_screen() globalvars.asdf = 0 globalvars.asdf += 1 #check everythign and see if changes need to be made self.check() #draw bullets self.draw() #move everything 1 self.tick() #initiate input function self.input(pygame.event.get()) #applies the smart screen updating pygame.display.update(self.enemylist) self.enemylist = [] #pauses and waits timeittook = globalvars.clock.tick(globalvars.FPS)
class Alem(ShowBase): def __init__(self): ShowBase.__init__(self) base.setFrameRateMeter(True) set_app(self) app = self # Rocket Gui LoadFontFace("gui/Raleway.otf") self.region = RocketRegion.make('pandaRocket', app.win) self.region.setActive(1) self.context = self.region.getContext() self.hud = self.context.LoadDocument('gui/hud.rml') ih = RocketInputHandler() app.mouseWatcher.attachNewNode(ih) self.region.setInputHandler(ih) self.hud.Show() self.cTrav = CollisionTraverser('coltrav') #self.cTrav.showCollisions(self.render) self.ai_world = AIWorld(render) self.enableParticles() self.scene = [] self.player = Player(self) self.scene.append(self.player) self.backgrounds = self.gen_background() self.enemy_manager = EnemyManager(self) self.enemies = self.gen_enemies(self.scene) self.bullet_manager = BulletManager(self) self.bullet_count = 0 self.taskMgr.add(self.update, "update") self.mouse_pos = Point2(0,0) self.set_keys() self.music = self.loader.loadSfx('sounds/onegameamonthjan.ogg') self.music.setVolume(0.2) self.music.play() self.item_manager = ItemManager(self) self.tree_manager = TreeManager(self) self.trees = self.gen_trees() def camera_task(self): self.camera.setPos(self.player.position.x, self.player.position.y , 400) self.camera.setHpr(0,-90,0) def get_cam(self): return self.camera.getPos() def set_keys(self): self.accept("a", self.player.move_left, [True]) self.accept("a-up", self.player.move_left, [False]) self.accept("d", self.player.move_right, [True]) self.accept("d-up", self.player.move_right, [False]) self.accept("w", self.player.move_up, [True]) self.accept("w-up", self.player.move_up, [False]) self.accept("s", self.player.move_down, [True]) self.accept("s-up", self.player.move_down, [False]) self.accept("mouse1", self.player.activate, [True]) self.accept("mouse1-up", self.player.activate, [False]) self.accept("mouse3", self.player.build, [True]) self.accept("mouse3-up", self.player.build, [False]) self.accept("escape", sys.exit) def update(self, task): if(self.mouseWatcherNode.hasMouse()): self.mouse_pos.x = self.mouseWatcherNode.getMouseX() self.mouse_pos.y = self.mouseWatcherNode.getMouseY() self.camera_task() # Bullet reaping self.bullet_manager.update(task.time) self.enemy_manager.update(task.time) self.ai_world.update() self.cTrav.traverse(self.render) for entity in self.scene: entity.update(task.time) self.camera_task() self.update_hud() return Task.cont def update_hud(self): self.hud.GetElementById("health").last_child.text = "%d" % self.player.hp self.hud.GetElementById("score").last_child.text = "%d" % self.player.score self.hud.GetElementById("souls").last_child.text = "%d" % self.player.souls # should move creation into the manager def gen_enemies(self, scene): enemies = [] for i in range(randint(50,100)): self.enemy_manager.create_enemy(1.0) #for i in range(1): """ x = uniform(-900, 900) if -100.0 < x < 100.0: if x < 0.0: x = x - 200.0 if x >= 0.0: x = x + 200.0 y = uniform(-900, 900) if -100.0 < y < 100.0: if y < 0.0: y = y - 200.0 if y >= 0.0: y = y + 200.0 enemy = Enemy(Point3(x, y, 0), i, self, self.enemy_manager, uniform(1,3), randint(1,4)) #enemy = Enemy(Point3(30.0,30.0,0.0), i, self, self.enemy_manager, 1, 1) enemies.append(enemy) scene.append(enemy) """ return enemies # make this configurable def gen_background(self): bg_x = 50 bg_y = 50 tile_count = 20 backgrounds = [] self.bgnp = self.render.attachNewNode("bg") for i in range(-tile_count,tile_count): for j in range(-tile_count,tile_count): backgrounds.append(load_object("grass", pos = Point2(i*bg_x, j*bg_y), transparency = False, scale = 50)) for background in backgrounds: background.reparentTo(self.bgnp) self.bgnp.flattenStrong() return backgrounds def gen_trees(self): for i in range(randint(10,20)): self.tree_manager.add_tree(Point3(uniform(-100,100), uniform(-100,100), 0), 1)
class Gamelolz: #This is the __init__ #its important. def __init__(self,parent): self.parent=parent globalvars.asdf = 0 ##some key vars, the works self.lagcount=0 #a little hack so that the background works nicer self.background=globalvars.screen #make the rectlist to handle dirty updating self.updaterects=[] #background object self.bgstars=BackgroundManager() ##make the lists to handle various sprites self.list_enemys=EnemyManager() self.player_list=pygame.sprite.RenderUpdates() self.list_allie_shots=pygame.sprite.OrderedUpdates() self.enemy_shots=pygame.sprite.RenderUpdates() self.token_list=pygame.sprite.RenderUpdates() ##make a new stage object self.stage=Stage(self,self.list_enemys,self.player_list,self.enemy_shots,self.list_allie_shots) #self.setup_events() self.new_display() def new_display(self): self.side_panel= pygame.sprite.RenderUpdates() self.points=Points() self.side_panel.add(self.points) self.health=Health() self.healthbar=HealthBar(self.health) self.livesbar=LivesBar(self.health,self.healthbar) self.side_panel.add(self.healthbar) self.side_panel.add(self.health) self.side_panel.add(self.livesbar) #clears all the variables def clear_vars(self): self.leftkeydown=0 self.rightkeydown=0 self.points.set_points(0) globalvars.x=400 globalvars.y=globalvars.WIN_RESY-60 self.stage.set_stage(0) self.list_enemys.empty() self.list_allie_shots.empty() self.player_list.empty() self.enemy_shots.empty() print "Game Restarted" def register_inputs(self): import defaultinputs defaultinputs.setup(self.inputmanager,self) #define function to draw player ship on X, Y plane def pship(self): self.player_list.clear(globalvars.surface,self.background) self.updaterects+=self.player_list.draw(globalvars.surface) #Define function to move the enemy ship def emove(self): self.list_enemys.clear(globalvars.surface, self.background) self.updaterects+=self.list_enemys.draw(globalvars.surface) #So i'm trying out having the program check for collisions, instead of the enemy objects #i think i might switch to the objects, but still keep this function just hand the computing to the object #seems most efficient def test_collision(self): todie=ecollision.groupcollide(self.list_enemys, self.list_allie_shots,0,0,ecollision.BOTTOM) #todie=pygame.sprite.groupcollide(self.list_enemys, self.list_allie_shots,0,0) #print todie for enemy,bullet in todie.iteritems(): bullet.set_hit(1) enemy.hit(bullet.damage) self.points.add_points(1) for q in pygame.sprite.spritecollide(self.player, self.enemy_shots,0): #for q in ecollision.spritecollide(self.player, self.enemy_shots,0,ecollision.TOP): #print "ZOMFG SHOTZORZ" self.player.set_hit(q.damage) self.enemy_shots.remove(q) #if self.token_list: # for token in ecollision.spritecollide(self.player, self.token_list,0, ecollision.TOP): # print token #if there are no enemys left, go to the next stage def check_done(self): if not globalvars.asdf%20 and not self.list_enemys: #the %20 lets it wait a fraction of a second self.stage.next_stage() #checks to see if we can expand the ranges of the bots so its nice and.... umm... nice. def check_rows(self): if globalvars.asdf % 20==0: self.list_enemys.check_enemy_rows() #major hack just to get this thing playable..... sorry def again(self): if self.player.health <= 0: return False return True #this is called if the player shoots... to be honest i dont know why this is a function def pshoot(self,**args): self.player.shoot() #draws the bullet.... duh. come on dude. def drawbullets(self): #for x in self.list_allie_shots: #x.draw() self.list_allie_shots.clear(globalvars.surface,self.background) self.enemy_shots.clear(globalvars.surface,self.background) self.updaterects+=self.list_allie_shots.draw(globalvars.surface) self.updaterects+=self.enemy_shots.draw(globalvars.surface) #... def drawsidepanel(self): if globalvars.asdf%5==0: self.side_panel.update() self.side_panel.clear(globalvars.surface,self.background) self.updaterects+=self.side_panel.draw(globalvars.surface) #dont ask why i made this a function, i made draw<asdf> for everythign else #so figured id make this too def drawtokens(self): if self.token_list: self.token_list.clear(globalvars.surface,self.background) self.updaterects+=self.token_list.draw(globalvars.surface) #goes through all the arrays and makes each of them move 1 space, simple and easy yet it deserves a comment... def tick(self): self.bgstars.update() self.list_allie_shots.update() self.list_enemys.update() self.enemy_shots.update() self.token_list.update() self.player.update() ###################### #heres a bunch of metafunctions #i break it up so its really easy to add new features #like if we ant a counter? add something to check() and draw() #all of these are called once per frame def check(self): self.check_done() self.test_collision() self.check_rows() self.list_enemys.shoot(self.stage.enemyodds) def draw(self): self.updaterects+=self.bgstars.draw() self.updaterects+=self.bgstars.clear() self.drawbullets() self.pship() self.emove() self.drawsidepanel() self.drawtokens() #does just what it sounds like..... def clear_screen(self): globalvars.surface.fill(globalvars.bgcolor) pygame.display.flip() #for debugging info mostly... No entirely def dispvars(self): print "The Enemy Array size is:",len(self.list_enemys.sprites()) print "The Player Shot Array size is:",len(self.list_allie_shots.sprites()) print "The Enemy Shot Array size is:",len(self.enemy_shots.sprites()) #does lots and lots of stuff, it really needs to be cleaned up def input(self, events): self.inputmanager.check(events) def mousemove(self,parent,event): tempx=pygame.mouse.get_pos()[0]-parent.player.rect.width/2 ## Just to make sure we don't get the ship way out there: if tempx > globalvars.xmax: #if its outside the globalvars.window, just stick it as far as possible parent.player.move(globalvars.xmax,globalvars.y) elif tempx < globalvars.xmin: parent.player.move(globalvars.xmin,globalvars.y) elif abs(tempx-globalvars.x) > globalvars.smooth_scroll_var1: #smooth scrolling if the mouse gets far from the ship parent.player.move(parent.player.get_pos().left+(tempx-parent.player.get_pos().left)/globalvars.smooth_scroll_var2,globalvars.y) else: #if it gets down to this point, #we've passed all sanity checks so just move it parent.player.move(tempx,globalvars.y) ################################################################################################################## #pretty simple def start(self): self.clear_vars() self.player=Player(self.player_list,self.token_list,Gun(self.list_allie_shots,Bullet)) self.player_list.add(self.player) self.player.set_pos(globalvars.x,globalvars.y) self.health.link_sprite(self.player) self.inputmanager=self.parent.inputmanager self.register_inputs() self.loop() #Yeah see this one does all of the work def loop(self): #start loop while self.again(): #refresh self.background...needs to be done once in a while if globalvars.asdf>=globalvars.REFRESH_TIME: #self.clear_screen() globalvars.asdf=0 globalvars.asdf+=1 #check everythign and see if changes need to be made self.check() #move everything 1 self.tick() #initiate input function self.input(pygame.event.get()) #draw everything self.draw() #applies the smart screen updating #globalvars.surface.set_alpha(150,pygame.RLEACCEL) ......dont ask why #globalvars.surface2.blit(globalvars.surface,globalvars.surfacerect) pygame.display.update(self.updaterects) self.updaterects=[] #pauses and waits self.time= globalvars.clock.get_time() if self.time > globalvars.FPS: timeittook=globalvars.clock.tick(globalvars.FPS-(self.time-globalvars.FPS)) print "Lag at frame %s\t%sms"%(globalvars.asdf,self.time) else: timeittook=globalvars.clock.tick(globalvars.FPS)
class Gamelolz: #This is the __init__ #its important. def __init__(self,parent): self.parent=parent globalvars.asdf = 0 self.lagcount=0 self.leftkeydown=0 self.rightkeydown=0 self.enemylist=[] self.list_enemys=EnemyManager() self.stage=Stage(self.list_enemys,globalvars.player_list) self.list_allie_shots=pygame.sprite.RenderUpdates() self.enemy_shots=pygame.sprite.RenderUpdates() #clears all the variables def clear_vars(self): self.leftkeydown=0 self.rightkeydown=0 health.set_health(globalvars.max_health) points.set_points(0) globalvars.x=400 globalvars.y=globalvars.WIN_RESY-60 self.stage.set_stage(-1) #hax globalvars.enemy_bullet_odds=100 self.list_enemys.empty() self.list_allie_shots.empty() globalvars.player_list.empty() self.enemy_shots.empty() print "Game Restarted" #define function to draw player ship on X, Y plane def pship(self, x,y): globalvars.player_list.clear(globalvars.surface,globalvars.screen) self.enemylist+=globalvars.player_list.draw(globalvars.surface) #Define function to move the enemy ship def emove(self): self.list_enemys.clear(globalvars.surface, globalvars.screen) self.enemylist+=self.list_enemys.draw(globalvars.surface) #draws all the enemys you ask it def draw_enemys(self): #k so now some recursive loops: for enemycol in range(self.stage.get_stage()[0]): #now for the rows for enemyrow in range(self.stage.get_stage()[1]): #make a new enemy object: tempenemy=Enemy(self.list_enemys) #this ones a long one, but it works: tempenemy.set_pos(globalvars.xmin+enemycol*(globalvars.enemy_width+globalvars.enemy_spacing_x),globalvars.ymin+enemyrow*(globalvars.enemy_height+globalvars.enemy_spacing_y)-150) #this one is even worse, but works even better: tempenemy.set_range(globalvars.xmin+enemycol*(globalvars.enemy_width+globalvars.enemy_spacing_x),globalvars.xmax-(self.stage.get_stage()[0]-enemycol)*(globalvars.enemy_height+globalvars.enemy_spacing_x)) #now add the temp enemy to the array and we're good to go self.list_enemys.add(tempenemy) #So i'm trying out having the program check for collisions, instead of the enemy objects #i think i might switch to the objects, but still keep this function just hand the computing to the object #seems most efficient def test_collision(self): todie=pygame.sprite.groupcollide(self.list_enemys, self.list_allie_shots,0,0) #print todie for enemy,bullet in todie.iteritems(): self.list_allie_shots.remove(bullet) enemy.set_state(0) points.add_points(1) if pygame.sprite.spritecollideany(self.player, self.enemy_shots): #print "ZOMFG SHOTZORZ" self.player.set_hit() health.hit() #if there are no enemys left, go to the next stage def check_done(self): if not self.list_enemys: self.stage.next_stage() self.draw_enemys() #checks to see if we can expand the ranges of the bots so its nice and.... umm... nice. def check_rows(self): if globalvars.asdf % 20==0: #simple sorting algorithm to find the highest values highest=globalvars.xmin lowest=globalvars.xmax for enemy in self.list_enemys: if enemy.get_range()[1] > highest: highest=enemy.get_range()[1] if enemy.get_range()[0] < lowest: lowest=enemy.get_range()[0] highest=globalvars.xmax-highest lowest=lowest-globalvars.xmin if highest != 0 or lowest != 0: #makes things |--| this much more efficient for enemy in self.list_enemys: erange=enemy.get_range() enemy.set_range(erange[0]-lowest,erange[1]+highest) #major hack just to get this thing playable..... sorry def again(self): if health.get_health() <= 0: return False return True #this is called if the player shoots def pshoot(self, sx, sy): self.player.shoot(self.list_allie_shots,sx,sy) #draws the bullet.... duh. come on dude. def drawbullets(self): #for x in self.list_allie_shots: #x.draw() self.list_allie_shots.clear(globalvars.surface,globalvars.screen) self.enemy_shots.clear(globalvars.surface,globalvars.screen) self.enemylist+=self.list_allie_shots.draw(globalvars.surface) self.enemylist+=self.enemy_shots.draw(globalvars.surface) #... def drawsidepanel(self): if globalvars.asdf%5==0: globalvars.side_panel.update() globalvars.side_panel.clear(globalvars.surface,globalvars.screen) self.enemylist+=globalvars.side_panel.draw(globalvars.surface) #goes through all the arrays and makes each of them move 1 space, simple and easy yet it deserves a comment... def tick(self): self.list_allie_shots.update() self.list_enemys.update() self.enemy_shots.update() ###################### #heres a bunch of metafunctions #i break it up so its really easy to add new features #like if we ant a counter? add something to check() and draw() #all of these are called once per frame def check(self): self.check_done() self.test_collision() self.check_rows() bgstars.update() self.list_enemys.shoot(self.enemy_shots) self.player.update() def draw(self): self.enemylist+=bgstars.draw() self.enemylist+=bgstars.clear() self.drawbullets() self.pship(globalvars.x,globalvars.y) self.emove() self.drawsidepanel() #does just what it sounds like..... def clear_screen(self): globalvars.surface.fill(globalvars.bgcolor) pygame.display.flip() #for debugging info mostly def dispvars(self): print "The Enemy Array size is:",len(self.list_enemys.sprites()) print "The Player Shot Array size is:",len(self.list_allie_shots.sprites()) print "The Enemy Shot Array size is:",len(self.enemy_shots.sprites()) #does lots and lots of stuff, it really needs to be cleaned up def input(self, events): global x global y pygame.event.pump() #somewhere in their docs it said this line was a good idea for event in events: if event.type == QUIT: sys.exit(0) if event.type == pygame.MOUSEMOTION: pygame.event.get() tempx=pygame.mouse.get_pos()[0]-self.player.rect.width/2 ## Just to make sure we don't get the ship way out there: if tempx > globalvars.xmax: #if its outside the globalvars.window, just stick it as far as possible self.player.move(globalvars.xmax,globalvars.y) elif tempx < globalvars.xmin: self.player.move(globalvars.xmin,globalvars.y) elif abs(tempx-globalvars.x) > globalvars.smooth_scroll_var1: #smooth scrolling if the mouse gets far from the ship self.player.move(self.player.get_pos().left+(tempx-self.player.get_pos().left)/globalvars.smooth_scroll_var2,globalvars.y) else: #if it gets down to this point, #we've passed all sanity checks so just move it self.player.move(tempx,globalvars.y) ## if the mouse is clicked, shoot! if event.type == pygame.MOUSEBUTTONDOWN: self.pshoot(self.player.rect.centerx-globalvars.BULLET_WIDTH/2,globalvars.y) ## if 'q' is pressed, quit if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: sys.exit(0) if event.key == pygame.K_p: menulists.pause_menu() if event.key == pygame.K_ESCAPE: sys.exit(0) #keyboard controls if event.key == pygame.K_LEFT: self.leftkeydown=1 if event.key == pygame.K_RIGHT: self.rightkeydown=1 if event.key == pygame.K_SPACE: self.pshoot(self.player.rect.centerx-globalvars.BULLET_WIDTH/2,globalvars.y) #keyboard controls if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: self.leftkeydown=0 if event.key == pygame.K_RIGHT: self.rightkeydown=0 if self.leftkeydown: self.player.move_one(0) if self.rightkeydown: self.player.move_one(1) pygame.event.clear() ################################################################################################################## #pretty simple def start(self): self.clear_vars() self.player=Player() globalvars.player_list.add(self.player) self.player.set_pos(globalvars.x,globalvars.y) self.loop() #Yeah see this one does all of the work def loop(self): #start loop while self.again(): #refresh globalvars.screen...needs to be done once in a while if globalvars.asdf>=globalvars.REFRESH_TIME: #self.clear_screen() globalvars.asdf=0 globalvars.asdf+=1 #check everythign and see if changes need to be made self.check() #draw bullets self.draw() #move everything 1 self.tick() #initiate input function self.input(pygame.event.get()) #applies the smart screen updating pygame.display.update(self.enemylist) self.enemylist=[] #pauses and waits timeittook=globalvars.clock.tick(globalvars.FPS)