Beispiel #1
0
def game_level(window):
#Main loop to manage the game in Level mode.
    level = Level(0, "level")

    while not level.end_game:
        barre = Paddle("Img/Paddle.png")
        ball = Ball("Img/Ball.png")

        level.reset()

        level.load()

        t=time.time()
        #loop to check event about the closure of the game
        while not level.end and not level.end_game:
            for event in pygame.event.get():
                if event.type == QUIT:
                    level.end_game = True
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        level.end_game = True
                    elif event.key == K_SPACE:
                        ball.go()
            #check the event every milli second
            if time.time() - t > 0.001:
                #event to check the movement of the paddle
                tkey = pygame.key.get_pressed()
                if tkey[K_LEFT] != 0:
                    barre.movement("left")
                elif tkey[K_RIGHT] != 0:
                    barre.movement("right")
                ball.movement(barre, level)
                t = time.time()
                #Game over condition
                if ball.game_over == True:
                    message(window, level, "Game Over")
                    level.end = True
                #Victory condition
                if level.victory() == True:
                    message(window, level, "Victory", 50)
                    level.end = True
                    level.next_level()

            #Draw the game
            window.fill(0)
            window.blit(barre.image,(barre.x ,barre.y))
            window.blit(ball.image,(ball.x,ball.y))
            level.draw_bricks(window)
            pygame.display.flip()
Beispiel #2
0
def main():
	#Pygame Settings
	pygame.mixer.pre_init(44100, 16, 2, 4096)
	pygame.init()
	pygame.mixer.init()
	clock = pygame.time.Clock()
	screen = pygame.display.set_mode((WIDTH, HEIGHT), FULLSCREEN)
	pygame.display.set_caption("Nahui Tochtli")
	
	#Game Settings
	game_menu = Main_Menu(screen)
	level_number = get_levels()
	current_level = Level(level_number)
	current_status = "mainscreen"
	pause = False
	
	#Media
	music = SFX(pygame.mixer.Sound("audio/music/nahuitochtli.wav"))
	
	
	#Main Loop
	while True:
		time=clock.tick(60)
		KEYS_PRESSED = pygame.key.get_pressed()
		EVENTS = pygame.event.get()
		
		#MAIN MENU
		if current_status == "mainscreen":
			#Play music
			if not music.is_playing():
				music.get_sfx().play(-1)
			opt = game_menu.blit_menu("mainscreen", (0,0), current_level, EVENTS, level_number)
			if opt == -1:
				current_status = "quit"
			elif opt == 0:
				current_status = "newgame"
				#Stop music
				music.get_sfx().fadeout(100)
				#Play intro
				pygame.mixer.quit()
				play_movie("intro", screen)
				pygame.mixer.init()
				#Start level
				current_level = Level()
				level_number = 1
			elif opt == 1:
				current_status = "newgame"
				current_level = Level(level_number)
				#Stop music
				music.get_sfx().fadeout(100)
			elif opt == 2:
				current_status = "codices"
			elif opt == 3:
				current_status = "howtoplay"
			elif opt == 4:
				current_status = "about"
				
		#QUIT				
		if current_status == "quit":
			save_level(level_number)
			sys.exit(0)
		
		#NEW GAME / CONTINUE
		if current_status == "newgame":
			
			if not current_level.completed():
				if pause == False:
					current_level.update(time, KEYS_PRESSED, EVENTS, screen)
					if KEYS_PRESSED[K_p]:
						pause = True
						current_level.stop_music()
				if pause == True:
					opt = game_menu.blit_menu("pause", (0,0), current_level, EVENTS, level_number)
					if opt == 0:
						current_status = "mainscreen"
						pause = False
					elif opt == 1:
						pause = False
			else:
				if current_level.victory():
					#Last level
					if level_number == 11:
						#Play ending video
						pygame.mixer.quit()
						play_movie("end", screen)
						pygame.mixer.init()
						#Return to codices
						current_status = "codices"
						level_number += 1
					#Other levels
					else:
						opt = game_menu.blit_menu("win", (0,0), current_level, EVENTS, level_number)
						if opt == 0:
							level_number += 1
							current_status = "mainscreen"
						elif opt == 2:
							level_number += 1
							current_status = "codices"
						elif opt == 1:
							if level_number < 11:
								level_number += 1
								current_level = Level(level_number)

				elif not current_level.victory():
					opt = game_menu.blit_menu("lose", (0,0), current_level, EVENTS, level_number)
					#Restart this level
					if opt == 1:
						current_level = Level(level_number)
					elif opt == 0:
						current_status = "mainscreen"
		
		#ABOUT
		if current_status == "about":
			#Play music
			if not music.is_playing():
				music.get_sfx().play(-1)
				
			opt = game_menu.blit_menu("about", (0,0), current_level, EVENTS, level_number)
			if opt == 0:
				current_status = "mainscreen"
		
		#HOW TO PLAY
		if current_status == "howtoplay":
			#Play music
			if not music.is_playing():
				music.get_sfx().play(-1)
				
			opt = game_menu.blit_menu("howtoplay", (0,0), current_level, EVENTS, level_number)
			if opt == 0:
				current_status = "mainscreen"
				
		#CODICES
		if current_status == "codices":
			#Play music
			if not music.is_playing():
				music.get_sfx().play(-1)
				
			opt = game_menu.blit_menu("codices", (0,0), current_level, EVENTS, level_number)
			if opt == 0:
				current_status = "mainscreen"
			elif opt == 1:
				current_status = "codex_1"
			elif opt == 2:
				current_status = "codex_2"
			elif opt == 3:
				current_status = "codex_3"
			elif opt == 4:
				current_status = "codex_4"
			elif opt == 5:
				current_status = "codex_5"
			elif opt == 6:
				current_status = "codex_6"
			elif opt == 7:
				current_status = "codex_7"
			elif opt == 8:
				current_status = "codex_8"
			elif opt == 9:
				current_status = "codex_9"
			elif opt == 10:
				current_status = "codex_10"
			elif opt == 11:
				current_status = "codex_11"
		
		#CODEX X
		if current_status.startswith("codex_"):
			#Play music
			if not music.is_playing():
				music.get_sfx().play(-1)
				
			opt = game_menu.blit_menu(current_status, (0,0), current_level, EVENTS, level_number)
			if opt == 0:
				current_status = "codices"
				
		#System Events
		for event in EVENTS:
			if event.type==QUIT:
				save_level(level_number)
				sys.exit(0)
		pygame.display.update()
Beispiel #3
0
def game_endless(window):
#Main loop to manage the game in Endless mode.
    level = Level(0, "endless")
    font = pygame.font.SysFont('freesans', 30)

    while not level.end_game:
        barre = Paddle("Img/Paddle.png")
        ball = Ball("Img/Ball.png")

        level.reset()

        for i in range(3):
            level.add_row()

        t=time.time()
        level.end = False
        #loop to check event about the closure of the game
        while not level.end and not level.end_game:
            for event in pygame.event.get():
                if event.type == QUIT:
                    level.end_game = True
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        level.end_game = True
                    elif event.key == K_SPACE:
                        ball.go()
                    elif event.key == K_RETURN:
                        level.add_row()
            #check the event every milli second
            if time.time() - t > 0.001:
                 #event to check the movement of the paddle
                tkey = pygame.key.get_pressed()
                if tkey[K_LEFT] != 0:
                    barre.movement("left")
                elif tkey[K_RIGHT] != 0:
                    barre.movement("right")
                ball.movement(barre, level)
                t = time.time()
                window.fill(0)
                if ball.game_over == True or level.brick_under_limit():
                    #Game over condition
                    pygame.draw.rect(window, (0,255,0),(0, 450, 800, 2))
                    window.blit(font.render("Scores : "+str(level.score),True, (255,0,0)), (325, 600))
                    window.blit(barre.image,(barre.x ,barre.y))
                    window.blit(ball.image,(ball.x,ball.y))
                    level.draw_bricks(window)
                    level.add_score()
                    message(window, level, "Game Over", y=170)
                    level.end = True
                #add a row when the rebound number reach a certain value.
                if ball.rebound_number == ball.max_rebound():
                    ball.rebound_number = 0
                    ball.level_number += 1
                    level.add_row()
                #check if the ball is under the limit and if the level is empty and add a row when it's True.
                if ball.under_limit():
                    if level.victory():
                        level.change_score(6, ball)
                        ball.rebound_number = 0
                        level.add_row()
            #Draw game
            window.fill(0)
            window.blit(font.render("Scores : "+str(level.score),True, (255,0,0)), (325, 600))
            pygame.draw.rect(window, (0,255,0),(0, 450, 800, 2))
            window.blit(barre.image,(barre.x ,barre.y))
            window.blit(ball.image,(ball.x,ball.y))
            level.draw_bricks(window)
            pygame.display.flip()