Beispiel #1
0
def startGame():
	'''() -> None
	Create variables required for the game then initialize and control the 
	various modes'''
	
	pygame.init()

	#Define some tuples that act as colours
	gray 		= (80, 80, 80)	
	lightGray 	= (120, 120, 120)
	white 		= (255, 255, 255)
	red 		= (210, 10, 10)

	# Define screen size and create the screen
	screenSize 	= (800, 500)
	screen 		= pygame.display.set_mode(screenSize)
	
	# Define the variables that will control physics in the game
	yVel 		= 2
	xVel 		= 0
	gravity 	= 0.1
	gravPoint	= screenSize[1]/2
	yVelEnemy	= 2
	
	# Game internals control/state
	mode 		= 0 ## 0 = Start screen, 1 = character select, 2 = game playing, 3 = game over
	attackM 	= 0
	attackE 	= 0
	angle 		= 0
	enmyHit		= 0
	jump		= 0
	direction	= "right"
	hpMain		= 250
	hpEnemy		= 250
	animStage	= "idle"
	
	# Load background
	background	= os.path.join(os.pardir, "img", "bg.png")
	bg		= pygame.image.load(background)
	
	# Set MC to be a blank for character selection at the start
	player = None
	enemyLoc = None

	
	# Create the text used in the game
	font 		= pygame.font.Font(None, 44)
	title 		= font.render('GNU-GAME', 1, white)
	font 		= pygame.font.Font(None, 22)
	newGame 	= font.render('New Game', 1, white)
	font 		= pygame.font.Font(None, 22)
	plrOneText 	= font.render('Player One', 1, white)
	plrOneChar	= font.render('Richard Stallman', 1, white)
	startFight	= font.render('Start Fight', 1, white)
	versus		= font.render('VS', 1, white)
	
	# The event loop.
	running 	= True
	while running:
	
		event = pygame.event.poll()
		if event.type == pygame.QUIT:
		    running = False
		
		if mode == 0:    
			# Draw the background on screen    
			screen.fill(gray)
			pygame.draw.rect(screen, lightGray, (screenSize[0]/2 - 70, \
							screenSize[1]/2 + 10, 100, 30))
							
			# Put text on screen
			text_pos = (screenSize[0]/2 - 100, screenSize[1]/2 - 100)
			screen.blit(title, text_pos)
			
			text_pos = (screenSize[0]/2 - 60, screenSize[1]/2 + 20)
			screen.blit(newGame, text_pos)
			
			# Check to see if the player clicks the new game button
			if event.type == pygame.MOUSEBUTTONDOWN and newBtnPress(event.pos, screenSize):
				mode = 1
				spriteName = "sprite0.png"
				charIcon = 1
		
		elif mode == 1:
			# Draw the background on screen    
			screen.fill(gray)
			
			# Draw a sprite on the screen to show the player what the character they 
			# select looks like 
			if player is not None:
				plrOne = pygame.image.load(os.path.join(os.pardir, "img", player, "sprites", spriteName))
				plrOne = pygame.transform.flip(plrOne, 1, 0)
				screen.blit(plrOne, (screenSize[0]/3, 20))
			if enemyLoc is not None:
				enmy = pygame.image.load(os.path.join(os.pardir, "img", enemyLoc, "sheet.png"))
				screen.blit(enmy, (screenSize[0] - screenSize[0]/2, 20), (130, 30, 92, 179))
			
			# Make a list for all the characters
			contents = fileBrowser.print_items(os.path.join(os.pardir, "img"))
			chars = getChars(contents, font, white, lightGray, screen, 50, 100)	
			charsTwo = getChars(contents, font, white, lightGray, screen, 580, 100)
			
			textPos = (320, 305)
			pygame.draw.rect(screen, lightGray, (305, 300, 100, 20))
			screen.blit(startFight, textPos)
			
			textPos = (screenSize[0]/2 -30, 120)
			screen.blit(versus, textPos)
					
			if event.type == pygame.MOUSEBUTTONDOWN:
				if player and enemyLoc:
					mode 	= newBtnPressStart(event.pos, screenSize)
					if mode == 2:
						# set appropriate sprites for attacks an enemies
						f 			= open(os.path.join(os.pardir, "img", player, "config.xml"),"r")
						data 		= f.read()
						idle 		= xmlParse.parse(data, "idle")
						atkE 		= xmlParse.parse(data, "atk-e")
						idleList 	= idle.split()
						atkEList	= atkE.split()
						f.close()
						
						l = []
						for i in range(len(idleList)):
							spriteName 	= "sprite" + str(idleList[i]) + ".png"
							name 		= os.path.join(os.pardir, "img", player, "sprites", spriteName)
							spriteMain	= pygame.transform.flip(pygame.image.load(name), 1, 0)
							l.append((spriteMain, 0.2))
							
						idleMain	= pyganim.PygAnimation(l,loop=False)
						l = []
						for i in range(len(atkEList)):
							spriteName 	= "sprite" + str(atkEList[i]) + ".png"
							name 		= os.path.join(os.pardir, "img", player, "sprites", spriteName)
							spriteMain	= pygame.transform.flip(pygame.image.load(name), 1, 0)
							l.append((spriteMain, 0.2))
							
						atkEMain	= pyganim.PygAnimation(l,loop=False)
						
						main 			= character.Character(10, 200, spriteMain, yVel, 0)
						spriteEnemy		= os.path.join(os.pardir, "img", enemyLoc, "sprites", "sprite0.png")
						spriteEnemy		= pygame.image.load(spriteEnemy)
						enemy 			= character.Character(500, 200, spriteEnemy, yVel, 0)
						attackMESprite 	= os.path.join(os.pardir, "img", player, "attack-e.png")
						attackMESprite	= pygame.image.load(attackMESprite)
						
						
						f = open (os.path.join(os.pardir, "img", player, "config.xml"),"r")
						data = f.read()
						# Open the config file for character info (movement speed, etc)
						attackMESpawn 	= eval(xmlParse.parse(data, "spawn"))
						velAtk			= eval(xmlParse.parse(data, "vel"))
						attackME = character.Character(attackMESpawn[0], attackMESpawn[1], attackMESprite, \
														velAtk[0], velAtk[1])
						f.close()							
						attackEESprite 	= os.path.join(os.pardir, "img", enemyLoc, "attack-e.png")
						attackEESprite	= pygame.image.load(attackEESprite)
						
						# Open the config file for character info (movement speed, etc)
						f = open (os.path.join(os.pardir, "img", enemyLoc, "config.xml"),"r")
						data = f.read()
						# Open the config file for character info (movement speed, etc)
						attackEESpawn 	= eval(xmlParse.parse(data, "spawn"))
						velAtk			= eval(xmlParse.parse(data, "vel"))
						attackEE = character.Character(attackEESpawn[0], attackEESpawn[1], attackEESprite, \
														velAtk[0], velAtk[1])
						f.close()
						
				if mode == 1:
					if charSelect(event.pos, chars):
						player = charSelect(event.pos, chars)
					else:
						player = player
						
					if charSelect(event.pos, charsTwo):
						enemyLoc = charSelect(event.pos, charsTwo)
					else:
						player	= player
					
		elif mode == 2:
			screen.blit(bg, (0,0))
			# Flip the enemys sprite so that it is always facing the player
			if main.x > enemy.x:
				spriteEnemy	= os.path.join(os.pardir, "img", enemyLoc, "sprites", "sprite8.png")
				spriteEnemy	= pygame.transform.flip(pygame.image.load(spriteEnemy), 1, 0)
				enemy 		= character.Character(enemy.x, enemy.y, spriteEnemy, yVel, 0)
			elif main.x < enemy.x:
				spriteEnemy	= os.path.join(os.pardir, "img", enemyLoc, "sprites", "sprite0.png")
				spriteEnemy	= pygame.image.load(spriteEnemy)
				enemy 		= character.Character(enemy.x, enemy.y, spriteEnemy, yVel, 0)
				
	
			# Draw the enemy on screen and check for events
			enemy.loadSprite(screen)
			enemy.applyGravity(gravity)
			enemy.checkCollision(gravPoint, jump)
			
			# Move the enemy towards the player
			if enemy.x > main.x + 50:
				enemy.x -= 1.5
			elif enemy.x < main.x - 100:
				enemy.x += 1.5
				
			# Draw the MC on screen and check for events
			jump = main.checkMoveKeys(jump, screenSize, enemy.x, enemy.y)
			main.applyGravity(gravity)
			jump = main.checkCollision(gravPoint, jump)
			
			for event in pygame.event.get():
				if event.type == KEYDOWN:
					if event.key == K_e:
						animstage = "ply"
						idleMain.stop()
						atkEMain.play()
			
			if atkEMain.isFinished():
				atkEMain.stop()
				animStage = "idle1"
				
			if animStage == "idle1":
				idleMain.play()
				idleMain.blit(screen, (main.x, main.y))		
				
			atkEMain.blit(screen, (main.x, main.y))
        	
			if(main.checkAttackKeys(attackM)):
				attackM 	= 1				
			
			if attackMESpawn[0] - 1 < attackME.x < screenSize[0] + 25:
				attackME.loadSprite(screen)
			else:
				attackME.x 	= attackMESpawn[0]
				attackM 	= 0
							
			if attackM == 1:
				attackME.x 	+= attackME.xVel
				
			if attackME.x + 100 >= enemy.x and attackME.x + 100 <= enemy.x + 10:
				hpEnemy -= 30
				
			r = random.randint(0, 999)
			if r >= 100 and r <= 200:
				if abs(main.x - enemy.x) <= 120 and main.y >= enemy.y:
					stMachine = state.stateMachine()
					stMachine.stateGet("x<20 y<15")
			elif r > 200 and r <= 205:
				if abs(main.x - enemy.x) >= 200:
					stMachine = state.stateMachine()
					stMachine.stateGet("x>20 y<20")
					attackE = 1
					
				
			if attackEESpawn[0] - 1 < attackEE.x < screenSize[0] + 25:
				attackEE.loadSprite(screen)
			else:
				attackEE.x 	= attackEESpawn[0]
				attackE 	= 0
							
			if attackE == 1:
				attackEE.x 	+= attackEE.xVel
				
			if attackEE.x + 100 >= main.x and attackEE.x + 100 <= main.x + 10:
				hpMain -= 30
			
				
			pygame.draw.rect(screen, red, (50, 10, hpMain, 20))
			pygame.draw.rect(screen, red, (750, 10, -hpEnemy, 20))
		# Refresh the screen
		pygame.display.flip()
		pygame.time.delay(1)
Beispiel #2
0
	'''(tuple, tuple) -> int
	Return a one if the tuple position is within the tuple screenSize
	'''
	
	# Here we check to make sure the click is within the box of "New Game" so that we
	# can return a 1 if it is 305, 300, 100, 20
	if position[0] >= 305 and position [0] <= 405:
		if position[1] >= 300 and position[1] <= 320:
			return 2
			
	return 1
	
			
if __name__ == "__main__":
	
	contents = fileBrowser.print_items(os.path.join(os.pardir, "img"))
	for i in contents:
		if "bg" not in i:
			input_loc = os.path.join(i, "sheet.png")
			im = Image.open(input_loc)
			pnt = 0
			for d in range(11):
				om = im.crop((pnt, 30, 120+pnt, 179))
				om.save(os.path.join(i, 'sprites', 'sprite'+str(d)+'.png'))
				pnt += 120
    
	startGame()