Пример #1
0
class Menu_play_button:

#-------------------------------------------------------------------------------------------------------

	def __init__( self ):
		self.text = Text( "menu/KGHAPPY.ttf", 80 )
		self.sprite = Sprite( "menu/blankbutton.png", 4 )
		self.click = pygame.mixer.Sound( "menu/click.wav" )

		self.next = 0
		self.on = 0
		self.mouseup = 0

#-------------------------------------------------------------------------------------------------------
	
	def load( self, width, height ):

		self.text.createText( "play", [ 0x82, 0xae, 0x20 ] )
		self.text.setX( width/2 - self.text.getWidth()/2 )
		self.text.setY( height/2 - self.text.getHeight()/2 -10 +120 )

		self.sprite.setX( width/2 - self.sprite.getWidth()/2 )
		self.sprite.setY( height/2 - self.sprite.getHeight()/2 +120 )

#-------------------------------------------------------------------------------------------------------
	
	def draw( self, window ):
		self.sprite.draw( window )
		self.text.draw( window )

#-------------------------------------------------------------------------------------------------------

	def fadein( self, i = 1, m = 255 ):
		self.sprite.fadein( i, m )
		self.text.fadein( i, m )

#-------------------------------------------------------------------------------------------------------

	def fadeout( self, i = 1, m = 0 ):
		self.sprite.fadeout( i, m )
		self.text.fadeout( i, m )

#-------------------------------------------------------------------------------------------------------

	def setState( self ):
		if self.on == 0:
			self.on = 1
		else:
			self.on = 0
		#print self.on

#-------------------------------------------------------------------------------------------------------
	
	def handle( self, event ):
		
		self.focus = False
		if event.type == pygame.MOUSEBUTTONDOWN:
			self.focus = True
			self.mouseup = 1
		elif event.type == pygame.MOUSEBUTTONUP:
			self.mouseup = 0
			
		
		self.sprite.setOffset( 0 )
		self.x, self.y = pygame.mouse.get_pos()

		if self.x > self.sprite.getLeft() and self.x < self.sprite.getRight() and self.sprite.getAlpha() == 255:
			if self.y > self.sprite.getTop() and self.y < self.sprite.getBot():
				if self.focus == True:
					if self.on == 0:
						self.click.play()
					self.next = 1

				if self.focus == True or self.mouseup == 1:
					self.sprite.setOffset( 2 )
				else:
					self.sprite.setOffset( 1 )

#-------------------------------------------------------------------------------------------------------

	def getAlpha( self ):
		return self.sprite.getAlpha()

#-------------------------------------------------------------------------------------------------------

	def getState( self ):
		return self.next

#-------------------------------------------------------------------------------------------------------

	def setNext( self, n ):
		self.next = n

#-------------------------------------------------------------------------------------------------------

	def getLeft( self ):
		return self.sprite.getLeft()

#-------------------------------------------------------------------------------------------------------

	def getBot( self ):
		return self.sprite.getBot()
Пример #2
0
class Hero:

#-------------------------------------------------------------------------------------------------------
	
	def __init__( self ):

		self.hero = Sprite( "hero/0.png", 8 )
		self.hero.setOffset( 4 )

		self.move = 8
		self.delay = 2
		self.startstate = 0
		self.stateD = False
		self.stateA = False
		self.stateSpace = False
		self.spacecounter = 0

#-------------------------------------------------------------------------------------------------------

	def load( self, width, height ):
		self.hero.setX( 0 )
		self.hero.setY( height - 130 - self.hero.getHeight() )

		self.minX = width/4
		self.maxX = width*4/5

#-------------------------------------------------------------------------------------------------------
	
	def draw( self, window ):
		
		if self.hero.getAlpha() == 255:

			if self.startstate == 0:
				if self.hero.getX() < self.minX:
					self.hero.setX( self.hero.getX() + 3 )
				else:
					self.startstate = 1
			else:
				if self.stateD and self.hero.getRight() < self.maxX:
					self.hero.setX( self.hero.getX() + 7 )
				elif self.hero.getRight() >= self.maxX:
					self.stateD = False

				if self.hero.getX() > self.minX +5:
					self.hero.setX( self.hero.getX() - 3 )
				if self.stateA and self.hero.getX() > self.minX +5:
					self.hero.setX( self.hero.getX() - 4 )


			self.move += 1
			if self.move >= 4 * self.delay:
				self.move = 0

		self.hero.setOffset( self.move / self.delay )
		self.hero.draw( window )

#-------------------------------------------------------------------------------------------------------

	def handle( self, event ):

		if self.startstate == 1:
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_d:
					self.stateD = True
				elif event.key == pygame.K_a:
					self.stateA = True

				if event.key == pygame.K_SPACE:
					self.stateSpace = True
			elif event.type == pygame.KEYUP:
				if event.key == pygame.K_d:
					self.stateD = False
				elif event.key == pygame.K_a:
					self.stateA = False

				if event.key == pygame.K_SPACE:
					self.stateSpace = False
		
#-------------------------------------------------------------------------------------------------------

	def fadein( self, i = 1, m = 255 ):
		self.hero.fadein( i, m )

#-------------------------------------------------------------------------------------------------------

	def fadeout( self, i = 1, m = 0 ):
		self.hero.fadeout( i, m )