Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)