当前位置:   article > 正文

Python魂斗罗小游戏源代码_微信魂斗罗代码

微信魂斗罗代码

Python魂斗罗小游戏源代码源程序,主程序Contra.py,游戏简易使用说明:A:向左,D:向右,W:跳起,S:趴下,J:射击,P:退出程序。

程序运行截图:在这里插入图片描述
Contra.py

'''
公众号:Python代码大全
'''
import pygame
import sys
from pygame.locals import *
from settings import Settings
from game_player import Game_Player 
import game_functions as gf
from pygame.sprite import Group
from game_stats import GameStats
from game_boss import Game_Boss
from button import Button

def run_game():
	bg = pygame.image.load("图片/map01.jpeg")
	game_settings = Settings()
	pos_x = 0#地图移动
	
	pygame.init()
	screen = pygame.display.set_mode(
		(game_settings.screen_width,game_settings.screen_height))
	pygame.display.set_caption("魂斗罗BY Python代码大全")
	stats = GameStats(game_settings)
	player = Game_Player(game_settings,screen)
	bullets = Group()
	boss = Group()
	enemys = Group()
	win_button = Button(game_settings,screen,"YOU WIN")
	gf.create_legion(game_settings,screen,enemys)
	gf.create_boss(game_settings,screen,player,boss)

	while True:
		pygame.mouse.set_visible(False)
		gf.check_events(game_settings,screen,player,bullets)
		gf.update_player(game_settings,stats,player,enemys)
		gf.update_bullet(game_settings,bullets,screen,enemys,boss)	
		gf.update_enemys(game_settings,enemys)	
		gf.update_boss(game_settings,boss)
		gf.update_screen(game_settings,bg,pos_x,screen,player,bullets,enemys,boss,win_button)
		
		if player.moving_right and player.center > player.screen_rect.centerx and game_settings.boss_appear == False:
			game_settings.screen_rolling = True
			pos_x -= 5#屏幕滚动速度
		else:
			game_settings.screen_rolling = False
run_game()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

game_funtcions.py

import sys
import pygame
from bullet import Bullet
from enemy import Enemy 
from game_boss import Game_Boss

def check_keydown_events(event,game_settings,screen,player,bullets):
	if event.key == pygame.K_k:#跳跃
		player.player_jump = True

	if event.key == pygame.K_d:#向右
		game_settings.bullet_direction = 'right' 
		if player.player_down or player.player_up:
			player.moving_right = False
		else:
			player.moving_right = True
		player.player_direction = 1	

	if event.key == pygame.K_a:#向左
		game_settings.bullet_direction = 'left'
		if player.player_down or player.player_up:
			player.moving_left = False
		else:
			player.moving_left = True
		player.player_direction = -1

	elif event.key == pygame.K_s:#向下
		player.player_down = True
		player.player_moving = False
		player.moving_left = False
		player.moving_right = False

	elif event.key == pygame.K_w:#向上
		player.player_up = True
		player.player_moving = False
		player.moving_left = False
		player.moving_right = False

	elif event.key == pygame.K_j:#射击
		new_bullet = Bullet(game_settings,screen,player)
		bullets.add(new_bullet)
		player.player_shooting = True

	elif event.key == pygame.K_p:
		sys.exit()

def check_keyup_events(event,player):
	if event.key == pygame.K_d:#右
		player.image = pygame.image.load('图片/PR/player.png')
		player.moving_right = False
		player.player_moving = False
	elif event.key == pygame.K_a:#左
		player.image = pygame.image.load('图片/PL/player.png')
		player.player_moving = False
		player.moving_left = False
	elif event.key == pygame.K_s:#下
		player.player_down = False
		if player.player_direction == 1:
			player.image = pygame.image.load('图片/PR/player.png')
		if player.player_direction == -1:
			player.image = pygame.image.load('图片/PL/player.png')
	elif event.key == pygame.K_w:#上
		player.player_up = False
		if player.player_direction == 1:
			player.image = pygame.image.load('图片/PR/player.png')
		if player.player_direction == -1:
			player.image = pygame.image.load('图片/PL/player.png')
	
def check_events(game_settings,screen,player,bullets):
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
		elif event.type == pygame.KEYDOWN:
			player.player_moving = True
			check_keydown_events(event,game_settings,screen,player,bullets)		
		elif event.type == pygame.KEYUP:
			check_keyup_events(event,player)

def update_screen(game_settings,bg,pos_x,screen,player,bullets,enemys,boss,win_button):
	screen.blit(bg,(pos_x,0))
	for bullet in bullets.sprites():
		bullet.blit_bullet()
	"""if game_settings.boom_end:#击中敌人爆炸
		enemys.empty()
		game_settings.boom_end = False"""
	player.blitme()
	if game_settings.boss_appear:
		boss.draw(screen)
	enemys.draw(screen)

	if game_settings.game_win:
		win_button.draw_button()
	pygame.display.flip()

def update_bullet(game_settings,bullets,screen,enemys,boss):
	bullets.update()
	for bullet in bullets.copy():
		if bullet.rect.centerx<0 or bullet.rect.centery<0 or bullet.rect.centerx > game_settings.screen_width:
			bullets.remove(bullet)
	check_bullet_enemy_collisions(game_settings,bullets,screen,enemys)
	check_bullet_boss_collisions(game_settings,bullets,boss)
	

def check_bullet_enemy_collisions(game_settings,bullets,screen,enemys):
	if game_settings.enemy_is_alive:
		collisions = pygame.sprite.groupcollide(bullets,enemys,True,False)
	else:
		collisions = pygame.sprite.groupcollide(bullets,enemys,True,True)
	if collisions != {}:
		game_settings.enemy_is_alive = False
	if len(enemys) == 0:
		create_legion(game_settings,screen,enemys)

def check_bullet_boss_collisions(game_settings,bullets,boss):
	if game_settings.boss_alive:
		collisions = pygame.sprite.groupcollide(boss,bullets,False,True)
	else:
		collisions = pygame.sprite.groupcollide(boss,bullets,True,True)

	if collisions != {}:
		game_settings.boss_lift -= 1
	if game_settings.boss_lift == 0:
		game_settings.boss_alive = False

def create_legion(game_settings,screen,enemys):
	for enemy_number in range(1):
		game_settings.enemy_is_alive = True
		enemy = Enemy(game_settings,screen)
		enemys.add(enemy)

def update_enemys(game_settings,enemys):
	if game_settings.boss_appear == False:#判断boss是否出现,出现则不出现小兵
		enemys.update()

		for enemy in enemys.copy():
			if enemy.rect.centerx<0:
				enemys.remove(enemy)
			if game_settings.boom_end:#击中敌人爆炸
				enemys.remove(enemy)
				game_settings.boom_end = False
				game_settings.boss_appear = True

def update_player(game_settings,stats,player,enemys):
	player.update()
	if pygame.sprite.spritecollideany(player,enemys):
		player_hit(game_settings,stats,player)
	if game_settings.player_die_end == True:
		player.revive_player()
		game_settings.player_die_end = False

def player_hit(game_settings,stats,player):
	stats.players_left -= 1
	game_settings.player_is_alive = False
	
def update_boss(game_settings,boss):
	if game_settings.boss_appear:
		boss.update()
	if game_settings.boss_boom_end:
		boss.empty()

def create_boss(game_settings,screen,player,boss):
	bo = Game_Boss(game_settings,screen,player)
	boss.add(bo)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163

完整程序包下载地址:

https://pan.baidu.com/s/1_B4fZJkgNb0XCDwRlR1WOQ,提取码请在“Python代码大全”公众号回复:魂斗罗提取码。获取更多Python源代码请微信关注:Python代码大全,在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/386287?site
推荐阅读
相关标签
  

闽ICP备14008679号