Beispiel #1
0
	def begin(self, game):

		self.game = game
		self.lives = 0
		self.bullets = 0
		self.music = None
		self.next_level = Main
	
		txt_message = Program.write(self.game.big_font, self.game.screen_size[0]/2, self.game.screen_size[1]/2, 4, 'You Won!')
		txt_message.colour = (0, 255, 0)

		if (game.joystick is not None):
			help = 'Press "Start" to continue'
		else:
			help = 'Press "Enter" to continue'

		txt_help = Program.write(self.game.font, self.game.screen_size[0]/2, self.game.screen_size[1]/2 + 50, 4, help)
		txt_help.colour = (255,255,255)		

		while True:

			if Program.key_released(K_RETURN) or (K_RETURN in game.read_joystick()):
				self.signal(S_KILL, True)
				self.game.scene = Main(self.game)
				self.game.scene.lives = Config.lives
				self.game.scene.bullets = Config.bullets 
				return

			yield
Beispiel #2
0
	def begin(self, game):

		# loading resources
		self.menu_font = Program.load_fnt(Config.menu_font, game.screen_size[1]/self.menu_size)
		self.game = game
		self.state = game.state
		self.last_active_menu = self.game.active_song
		self.calculate_page(0)

		self.game.encoder.led_red(False);
		self.game.encoder.led_green(True);

		Bg(game, Config.menu_bg_color)

		# creating menu items
		for i in range(0, len(self.game.playlist.list)):
			item = game.playlist.list[i]
			MenuItem(self, i, item.name.upper())

		while True:

			if (self.game.encoder.serial_connected == True and self.game.encoder.encoder != self.game.active_song):
				self.calculate_page(self.game.encoder.encoder - self.game.active_song)

			if (Program.key_released(K_UP)):
				self.calculate_page(-1);

			if (Program.key_released(K_DOWN)):
				self.calculate_page(1);

			if (Program.key_released(K_LEFT)):
				self.calculate_page(-self.menu_size);

			if (Program.key_released(K_RIGHT)):
				self.calculate_page(self.menu_size);

			yield
Beispiel #3
0
	def begin(self, game):
		self.x, self.y = 400, 500
		self.graph = game.g_player

		while True:

			# Processes have many member variables, one of the most common is x and y which
			# directly map to the process' coordinates on the screen. They, like all member
			# variables, can be altered at any time.
			# Here we check if the ship should move left/right and change the x (horisontal)
			# coordinate appropriately.
			if Program.key(K_LEFT):
				self.x -= 4
			if Program.key(K_RIGHT):
				self.x += 4
			# The final bit of input is shooting. Again we pass in the game object so we can
			# access graphics and honour the convention set for ourselves when creating the
			# player and enemies.
			if Program.key_released(K_SPACE):
				Bullet(game, self)
				
			yield
Beispiel #4
0
	def begin(self, level, x = None, y = None):

		if (x != None and y != None):
			self.x = x
			self.y = y
		else:
			self.x = 200
			self.y = 200
		self.graph = level.g_ship1
		self.level = level
		animate = False

		while True:

			if (animate):
				self.graph = level.g_ship2
			else:
				self.graph = level.g_ship1

			animate = not animate

			if (x == None and y == None):

				if Program.key(K_UP) or (K_UP in level.game.read_joystick()):
					self.y -= self.speed
				if Program.key(K_DOWN) or (K_DOWN in level.game.read_joystick()):
					self.y += self.speed
				if Program.key(K_LEFT) or (K_LEFT in level.game.read_joystick()):
					self.x -= self.speed
				if Program.key(K_RIGHT) or (K_RIGHT in level.game.read_joystick()):
					self.x += self.speed

				# bounds
				if (self.y < self.graph.get_height()/2):
					self.y = self.graph.get_height()/2
				if (self.x < self.graph.get_width()/2):
					self.x = self.graph.get_width()/2
				if (self.x > self.level.game.screen_size[0] - self.graph.get_width()/2):
					self.x = self.level.game.screen_size[0] - self.graph.get_width()/2
				if (self.y > self.level.game.screen_size[1] - self.graph.get_height()/2):
					self.y = self.level.game.screen_size[1] - self.graph.get_height()/2

			e = self.collision("Enemy1")
			if e and level.game.millis() - self.last_enemy_collision > 500:
				self.last_enemy_collision = level.game.millis()
				level.sound(level.s_ship_collision)
				self.health -= 10

			e = self.collision("Enemy2")
			if e and level.game.millis() - self.last_enemy_collision > 500:
				self.last_enemy_collision = level.game.millis()
				level.sound(level.s_ship_collision)
				self.health -= 20

			e = self.collision("Enemy3")
			if e and level.game.millis() - self.last_enemy_collision > 500:
				self.last_enemy_collision = level.game.millis()
				level.sound(level.s_ship_collision)
				self.health -= 30

			e = self.collision("Enemy4")
			if e and level.game.millis() - self.last_enemy_collision > 500:
				self.last_enemy_collision = level.game.millis()
				level.sound(level.s_ship_collision)
				self.health -= 50

			e = self.collision("Enemy5")
			if e and level.game.millis() - self.last_enemy_collision > 500:
				self.last_enemy_collision = level.game.millis()
				level.sound(level.s_ship_collision)
				self.health -= 10

			e = self.collision("Enemy6")
			if e and level.game.millis() - self.last_enemy_collision > 500:
				self.last_enemy_collision = level.game.millis()
				level.sound(level.s_ship_collision)
				self.health -= 50

			e = self.collision("Enemy7")
			if e and level.game.millis() - self.last_enemy_collision > 500:
				self.last_enemy_collision = level.game.millis()
				level.sound(level.s_ship_collision)
				self.health -= 20

			e = self.collision("Enemy8")
			if e and level.game.millis() - self.last_enemy_collision > 500:
				self.last_enemy_collision = level.game.millis()
				level.sound(level.s_ship_collision)
				self.health -= 20

			if Program.key_released(K_SPACE) or (K_SPACE in level.game.read_joystick() and level.game.millis() - level.game.last_joystick_btn > Config.joystick_button_delay):
				level.game.last_joystick_btn = level.game.millis()
				self.fire()

			yield
Beispiel #5
0
	def begin(self, game):

		self.game = game
		self.lives = 0
		self.bullets = 0
		self.music = "music/_music2.xm"
		self.next_level = Level1

		self.load_resources()
	
		x = self.game.screen_size[0]/2
		y = self.game.screen_size[1]/2

		txt_message = Program.write(self.game.big_font, x, y, 4, 'Aliens vs Humans')
		txt_message.colour = (255, 255, 255)

		if (game.joystick is not None):
			help1 = 'Press "Start" to run game'
			help2 = 'Press "Select" to quit'
			help3 = 'Gamepad axis to move, "Y" to fire'
		else:
			help1 = 'Press "Enter" to start'
			help2 = 'Press "ESC" to exit'
			help3 = 'Cursor keys to move, "Space" to fire'

		txt_help1 = Program.write(self.game.font, x, y + 50, 4, help1)
		txt_help1.colour = (255,255,255)

		txt_help2 = Program.write(self.game.font, x, y + 70, 4, help2)
		txt_help2.colour = (255,255,255)

		txt_help3 = Program.write(self.game.font, x, y + 90, 4, help3)
		txt_help3.colour = (255,255,255)

		txt_copy1 = Program.write(self.game.font, x, y*2 - 60, 4, 'Idea & Graphics: Nikita Karpov')
		txt_copy1.colour = (0, 255, 0)

		txt_copy2 = Program.write(self.game.font, x, y*2 - 40, 4, 'Music & Programming: Andy Karpov')
		txt_copy2.colour = (0, 255, 0)

		txt_copy3 = Program.write(self.game.font, x, y*2 - 20, 4, 'Copyright (c) 2012')
		txt_copy3.colour = (0, 255, 0)

		try:
			pygame.mixer.music.load(self.music)
			pygame.mixer.music.play(-1)
		except Exception as e:
			pass

		sky = Sky(self)
		ship = Ship(self, -200, 150)

		while True:

			ship.x += 4
			if (ship.x > self.game.screen_size[0] + 200):
				ship.x = -200

			if Program.key_released(K_RETURN) or (K_RETURN in game.read_joystick()):
				self.signal(S_KILL, True)
				self.game.scene = Level1(self.game)
				self.game.scene.lives = Config.lives
				self.game.scene.bullets = Config.bullets 
				return

			yield
Beispiel #6
0
	def begin(self, game):

		self.game = game

		self.game.encoder.led_red(True);
		self.game.encoder.led_green(True);

		Bg(game, Config.main_bg_color);

		# Bars(self)
		Clock(self)

		font = Program.load_fnt(Config.menu_font, self.game.screen_size[1]/18)
		font2 = Program.load_fnt(Config.menu_font, self.game.screen_size[1]/20)
		font3 = Program.load_fnt(Config.menu_font, self.game.screen_size[1]/24)

		station_name = Program.write(font, self.get_x(), self.get_y(0), 0, '')
		station_name.colour = Config.title_color

		playlist_pos = Program.write(font2, self.get_x(), self.get_y(1), 0, '')
		playlist_pos.colour = Config.station_color

		artist_name1 = Program.write(font3, self.get_x(), self.get_y(2), 0, '')
		artist_name1.colour = Config.song_color
		artist_name2 = Program.write(font3, self.get_x(), self.get_y(2.5), 0, '')
		artist_name2.colour = Config.song_color

		scroll_width = 0
		scroll_count = 0
		scroll_total_count = 0

		while True:

			# fetch current song from mpd every 500ms
			if (self.game.millis() - self.last_current_song > 500):
				current_song = self.game.mpd.currentsong();
				#print current_song
				if 'title' in current_song:
					title = current_song['title'].strip()
					try:
						title = title.decode('utf8')
					except Exception as e:
						pass

					if (title != self.current_song):
						scroll_width = 0
						scroll_count = 0
						scroll_total_count = 0
						self.current_song = title.upper()
						for l in self.current_song:
							scroll_width += font3.size(l)[0]
							scroll_total_count = scroll_total_count+1
							if (scroll_width <= self.game.screen_size[0] - self.get_x()):
								scroll_count = scroll_count+1
				else:
					self.current_song = ''
					scroll_width = 0
					scroll_count = 0
					scroll_total_count = 0
				self.last_current_song = self.game.millis()

				#if ('pos' in current_song and int(current_song['pos']) != self.game.active_song):
				#	self.game.active_song = int(current_song['pos'])
				#	self.game.last_active_song = int(current_song['pos'])


			# pring station, pos and song title
			station = self.game.playlist.list[self.game.active_song]
			station_name.text = station.name.upper()
			playlist_pos.text = u'STATION ' + str(self.game.active_song+1) + u' / ' + str(len(self.game.playlist.list))

			if (scroll_total_count > scroll_count):
				part = textwrap.wrap(self.current_song, scroll_count)
				if(len(part)>=2):
					artist_name1.text = part[0]
					artist_name2.text = part[1]
				else:
					artist_name1.text = ''
					artist_name2.text = self.current_song
			else:
				artist_name1.text = ''
				artist_name2.text = self.current_song

			if (self.game.encoder.serial_connected == True and self.game.encoder.encoder != self.game.active_song):
				self.calculate_page(self.game.encoder.encoder - self.game.active_song)

			if (Program.key_released(K_UP)):
				self.calculate_page(-1);

			if (Program.key_released(K_DOWN)):
				self.calculate_page(1);

			yield
Beispiel #7
0
	def begin(self):

		# Set fb device for Raspberry pi
		#os.environ["SDL_FBDEV"] = Config.fb_dev

		"Ininitializes a new pygame screen using the framebuffer"
		# Based on "Python GUI in Linux frame buffer"
		# http://www.karoltomala.com/blog/?p=679
		disp_no = os.getenv("DISPLAY")
		if disp_no:
		    print "I'm running under X display = {0}".format(disp_no)

		# Check which frame buffer drivers are available
		# Start with fbcon since directfb hangs with composite output
		drivers = ['fbcon', 'directfb', 'svgalib']
		found = False
		for driver in drivers:
			# Make sure that SDL_VIDEODRIVER is set
			if not os.getenv('SDL_VIDEODRIVER'):
				os.putenv('SDL_VIDEODRIVER', driver)
			try:
				pygame.display.init()
			except pygame.error:
				print 'Driver: {0} failed.'.format(driver)
				continue
			found = True
			break

		if not found:
			raise Exception('No suitable video driver found!')

		if (Config.detect_screen_size):
			self.screen_size = (pygame.display.Info().current_w, pygame.display.Info().current_h)

		# Set the resolution and the frames per second
		Program.set_mode(self.screen_size, self.full_screen, False)
		Program.set_fps(self.fps)
		#Program.set_title(self.window_title)

		# set mouse invisible
		pygame.mouse.set_visible(False)

		# load playlist
		self.playlist = Playlist()
		self.playlist.load(Config.playlist)

		# init mpd
		self.mpd = MPDClient()
		self.mpd.connect(Config.mpd_host, Config.mpd_port)
		if (Config.mpd_password is not None):
			self.mpd.password(Config.mpd_password)		

		# collect mpd playlist
		self.mpd.stop()
		self.mpd.clear()
		for item in self.playlist.list:
			self.mpd.add(item.url)
		# get active song from saved state
		self.state = State()
		self.active_song = self.state.load()
		self.last_active_song = self.active_song

		# init serial encoder instance
		self.encoder = Encoder(self.active_song, 0, len(self.playlist.list)-1)

		# play
		self.mpd.play(self.active_song)

		# run scene
		scene = Main(self);

		while True:
			# This is the main loop

			# Simple input check
			if (Program.key(K_ESCAPE)):
				Program.exit()

			change_scene_request = self.encoder.try_read()

			if (self.millis() - self.last_changed >= Config.save_timeout and self.last_active_song != self.active_song):
				self.last_active_song = self.active_song
				self.mpd.play(self.active_song)
				self.state.save(self.active_song)

			if (Program.key_released(K_SPACE) or Program.key_released(K_RETURN) or change_scene_request == True):
				change_scene_request = False
				if (isinstance(scene, Menu)):
					scene.signal(S_KILL, True)
					scene = Main(self)
				elif (isinstance(scene, Main)):
					scene.signal(S_KILL, True)
					scene = Menu(self)

			# The yield statement signifies that this object is finished for the
			# current frame, on the next frame the loop will resume here until it
			# hits the yield statement again. All objects are designed to act this way.
			yield