前言
halo,包子们下午好
今天给打击整一个魂斗罗小游戏
很多小伙伴接触魂斗罗应该是在小时候的一个手柄游戏上面吧
我记得作为90后的我,玩这一款游戏是在小学的时候
废话不多说,直接上才艺
今天给大家直接安排
相关文件
关注小编,私信小编领取哟!
当然别忘了一件三连哟~~
公众号:Python日志
源码点击蓝色字体领取
Python零基础入门到精通视频合集
【整整800集】Python爬虫项目零基础入门合集,细狗都学会了,你还不会?
开发工具
Python版本:3.7.8
相关模块:
pygame模块;
random模块;
以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
游戏玩法
玩法
WASD 是控制人物的移动,J是发射子弹 boss 是不知火舞 ,哈哈
做了一点小改变,不过问题不大很大
运行文件
点击文件Contra.py 运行游戏
代码实现
导入模块
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("images/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("魂斗罗 公众号:Python日志 游戏源码领取QQ群 :494958217")
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()
人物模块
class Game_Player():
def __init__(self,game_settings,screen):
self.screen = screen
self.game_settings = game_settings
self.image = pygame.image.load('images/PR/player.png')
self.rect = self.image.get_rect()
self.screen_rect = self.screen.get_rect()
self.screen_center_pos = self.screen_rect.centerx
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = 380
self.center = float(self.rect.centerx)
self.moving_right = False
self.moving_left = False
self.player_moving = False
self.pos_i = 0.0#跑步画面改变速度
self.pos_j = 0.0#跳跃的改变初始速度
self.pos_n = 0.0#射击
self.pos_d = 0.0
self.player_direction = 1#1为右,-1为左
self.player_down = False
self.player_up = False
self.player_jump = False
self.player_start_Y = 380#开始人物的高度
self.player_Y = self.player_start_Y
self.player_shooting = False
def update(self):
if self.game_settings.player_is_alive:
if self.moving_right and self.rect.right < self.screen_rect.right:
if self.game_settings.boss_appear:
self.center += self.game_settings.player_speed
elif self.center > self.screen_center_pos:
self.center += 0
else:
self.center += self.game_settings.player_speed
if self.moving_left and self.rect.left > 0:
self.center -= self.game_settings.player_speed
self.rect.centerx = self.center
self.update_image_moving()
else:
self.update_die()
def update_die(self):
self.names = locals()
self.players = []
for self.i in range(1,3):
self.names['player_image%s' %self.i] = pygame.image.load('images/PR/death%s.png' %self.i)
self.players.append(self.names['player_image%s' %self.i])
self.image = self.players[int(self.pos_d)]
self.pos_d += 0.1
if self.pos_d > 2.0:
self.pos_d = 0.0
self.game_settings.player_die_end = True
def update_image_moving_direction(self,direction):#循环图片
if self.player_shooting == True:
self.names = locals()
self.players = []
for self.i in range(1,4):
self.names['player_image%s' %self.i] = pygame.image.load('images/'+direction+'/shooting%s.png' %self.i)
self.players.append(self.names['player_image%s' %self.i])
self.image = self.players[int(self.pos_n)]
self.pos_n += 0.1#射击时跑步速率
if self.pos_n > 3.0:#射击时跑步有3个动作
self.pos_n = 0.0
else:
self.names = locals()
self.players = []
for self.i in range(1,6):
self.names['player_image%s' %self.i] = pygame.image.load('images/'+direction+'/player%s.png' %self.i)
self.players.append(self.names['player_image%s' %self.i])
self.image = self.players[int(self.pos_i)]
self.pos_i += 0.1#跑步速率
if self.pos_i > 5.0:#跑步有5个动作
self.pos_i = 0.0
def update_image_jump(self,direction):#循环图片
if self.game_settings.player_is_alive:
if self.player_jump == True:
self.names1 = locals()
self.players1 = []
for self.j in range(1,5):
self.names1['player_image%s' %self.j] = pygame.image.load('images/'+direction+'/jump%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_j)]
self.pos_j += 0.3#跳跃旋转速率
if self.pos_j > 4.0:#跳跃有4个动作
self.pos_j = 0.0
else:
self.update_die()
def update_image_moving(self):
if self.player_moving:
if self.moving_right:
self.update_image_moving_direction('PR')#PR是向右的图片
elif self.moving_left:
self.update_image_moving_direction('PL')
def get_player_state(self,player_state):#检测player的状态是否为趴下,向上,跳跃等
if self.player_direction == 1:
self.image = pygame.image.load('images/PR/'+player_state+'.png')
if self.player_direction == -1:
self.image = pygame.image.load('images/PL/'+player_state+'.png')
self.rect = self.image.get_rect()
self.rect.centerx = self.center
def get_player_down(self):
self.get_player_state('down')
self.rect.bottom = self.player_start_Y
self.screen.blit(self.image,self.rect)
def get_player_up(self):
self.get_player_state('up')
self.rect.bottom = self.player_start_Y
self.screen.blit(self.image,self.rect)
def get_player_jump(self):
self.get_player_state('jump1')
self.rect.bottom = self.player_Y
if self.game_settings.jump_vel < 0:
self.game_settings.jump_vel += 0.6#跳跃上升的增加的速率
elif self.game_settings.jump_vel > 0:
self.game_settings.jump_vel += 0.8#跳跃下降增加的速率
self.player_Y += self.game_settings.jump_vel
if self.player_Y > self.player_start_Y:
self.player_jump = False
self.player_Y = self.player_start_Y
self.game_settings.jump_vel = -14.0#恢复跳跃开始的速度
if self.player_direction == 1:
self.image = pygame.image.load('images/PR/player.png')
self.reset_player()
if self.player_direction == -1:
self.image = pygame.image.load('images/PL/player.png')
self.reset_player()
if self.player_jump == True:#判断是否处于跳跃状态来决定是否旋转跳跃的图像
if self.player_direction == 1:
self.update_image_jump('PR')
if self.player_direction == -1:
self.update_image_jump('PL')
self.screen.blit(self.image,self.rect)
def reset_player(self):
self.rect = self.image.get_rect()
self.rect.centerx = self.center
self.rect.bottom = self.player_start_Y
self.screen.blit(self.image,self.rect)
def blitme(self):
if self.player_jump:
self.get_player_jump()
elif self.player_down:
self.get_player_down()
elif self.player_up:
self.get_player_up()
else:
self.reset_player()
def revive_player(self):
self.center = self.screen_rect.centerx
self.game_settings.player_is_alive = True
游戏统计信息模块
class GameStats():
def __init__(self,game_settings):
self.game_settings = game_settings
self.reset_stats()
def reset_stats(self):
self.players_left = self.game_settings.players_limit
boss模块
import pygame
import random
from pygame.sprite import Sprite
import datetime
class Game_Boss(Sprite):
def __init__(self,game_settings,screen,player):
super().__init__()
self.pos_i = 0.0
self.pos_j = 0.0
self.pos_k = 0.0
self.game_settings = game_settings
self.screen = screen
self.image = pygame.image.load('images/boss/left/move/bz1.png')
self.rect = self.image.get_rect()
self.rect.x = self.game_settings.screen_width-self.rect.width
self.rect.bottom = 370
self.x = float(self.rect.x)#敌人的位置
self.speed_factor = self.game_settings.enemy_speed_factor
self.boss_start_Y = 370#开始boss人物的高度
self.boss_Y = self.boss_start_Y
self.attack1_order = 0
self.player = player
self.mx = self.x
def blitme(self):
self.screen.blit(self.image,self.rect)
def update(self):
if self.game_settings.boss_alive:
self.direction()
self.rand_att()
if self.game_settings.attack_1:
if self.game_settings.boss_direction == 1:
self.attack_1('left')
else:
self.attack_1('right')
elif self.game_settings.attack_2:
if self.game_settings.boss_direction == 1:
self.attack_2('left')
else:
self.attack_2('right')
elif self.game_settings.boss_jump:
self.jump()
elif self.game_settings.boss_run:
if self.game_settings.boss_direction == 1:
self.run('left')
else:
self.run('right')
elif self.player.rect.centerx > self.rect.x:
self.x += self.speed_factor
self.rect.x = self.x
self.move('right')
elif self.player.rect.centerx < self.rect.x:
self.x -= self.speed_factor
self.rect.x = self.x
self.move('left')
else:
self.boom()
self.game_settings.game_win = True
def rand_att(self):
random.seed(datetime.datetime.now())
if self.game_settings.attack_1 == False and self.game_settings.attack_2 == False and self.game_settings.boss_jump == False and self.game_settings.boss_run == False:
self.rand_num = random.randint(1,70)
if self.rand_num == 1:
self.game_settings.attack_1 = True
elif self.rand_num == 2:
self.game_settings.attack_2 = True
elif self.rand_num == 3:
self.game_settings.boss_jump =True
elif self.rand_num == 4:
self.game_settings.boss_run = True
def attack_1(self,direction):
self.rect.bottom = 410#做此动作时人物的位置
self.names1 = locals()
self.players1 = []
for self.j in range(0,34):
self.names1['player_image%s' %self.j] = pygame.image.load('images/boss/'+direction+'/attack_1/%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_i)]
self.pos_i += 0.5
if self.pos_i >= 34.0 :#循环完成后:
self.pos_i = 0.0
self.game_settings.attack_1 = False
def attack_2(self,direction):
self.rect.bottom = 355
self.names1 = locals()
self.players1 = []
for self.j in range(0,30):
self.names1['player_image%s' %self.j] = pygame.image.load('images/boss/'+direction+'/attack_2/%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_i)]
self.pos_i += 0.5
if self.pos_i >= 30:
self.pos_i = 0.0
self.game_settings.attack_2 = False
def move(self,direction):#循环图片
self.rect.bottom = 370
self.names1 = locals()
self.players1 = []
for self.j in range(1,7):
self.names1['player_image%s' %self.j] = pygame.image.load('images/boss/'+direction+'/move/bz%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_i)]
self.pos_i += 0.1#图片变化速率
if self.pos_i > 6.0:#敌人bag有4个动作
self.pos_i = 0.0
def run(self,direction):
self.rect.bottom = 400
self.names1 = locals()
self.players1 = []
for self.j in range(0,6):
self.names1['player_image%s' %self.j] = pygame.image.load('images/boss/'+direction+'/run/%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_i)]
self.pos_i += 0.3
if self.pos_i >= 6:
self.pos_i = 0.0
self.game_settings.boss_run = False
if self.player.rect.centerx > self.rect.x :#or self.x < self.mx:
self.x += 6
self.rect.x = self.x
elif self.player.rect.centerx < self.rect.x :#or self.x > self.mx:
self.x -= 6
self.rect.x = self.x
def jump(self):
self.rect.bottom = self.boss_Y
if self.game_settings.boss_jump_vel < 0:
self.game_settings.boss_jump_vel += 0.6#跳跃上升的增加的速率
elif self.game_settings.boss_jump_vel > 0:
self.game_settings.boss_jump_vel += 0.8#跳跃下降增加的速率
self.boss_Y += self.game_settings.boss_jump_vel
if self.boss_Y > self.boss_start_Y:
self.game_settings.boss_jump = False#结束跳跃
self.boss_Y = self.boss_start_Y
self.game_settings.boss_jump_vel = -12.0#恢复跳跃开始的速度
if self.player.rect.centerx > self.rect.x :#or self.x < self.mx:
self.x += self.speed_factor
self.rect.x = self.x
elif self.player.rect.centerx < self.rect.x :#or self.x > self.mx:
self.x -= self.speed_factor
self.rect.x = self.x
self.names1 = locals()
self.players1 = []
for self.j in range(3,10):
self.names1['player_image%s' %self.j] = pygame.image.load('images/boss/left/jump/%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_j)]
self.pos_j += 0.4#跳跃旋转速率
if self.pos_j >= 7:#跳跃有7个动作
self.pos_j = 0.0
def direction(self):
if self.player.rect.centerx > self.rect.x:
self.game_settings.boss_direction = -1
else:
self.game_settings.boss_direction = 1
def boom(self):
self.names1 = locals()
self.players1 = []
for self.j in range(1,4):
self.names1['player_image%s' %self.j] = pygame.image.load('images/boss/boom%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_k)]
self.pos_k += 0.05
if self.pos_k >= 3.0:
#self.game_settings.boom_end = True
self.game_settings.boss_boom_end = True
按钮模块
import pygame.font
class Button():
def __init__(self,game_settings,screen,msg):
self.screen = screen
self.screen_rect = self.screen.get_rect()
self.width,self.height = 300,150
self.button_color=(0,255,0)
self.text_color = (255,255,255)
self.font = pygame.font.SysFont(None,48)
self.rect = pygame.Rect(0,0,self.width,self.height)
self.rect.center = self.screen_rect.center
self.prep_msg(msg)
def prep_msg(self,msg):
self.msg_image = self.font.render(msg,True,self.text_color,self.button_color)
self.msg_image_rect = self.msg_image.get_rect()
self.msg_image_rect.center = self.rect.center
def draw_button(self):
self.screen.fill(self.button_color,self.rect)
self.screen.blit(self.msg_image,self.msg_image_rect)
敌人模块
import pygame
from pygame.sprite import Sprite
class Enemy(Sprite):
def __init__(self,game_settings,screen):
super().__init__()
self.pos_j = 0.0
self.pos_i = 0.0
self.game_settings = game_settings
self.screen = screen
self.image = pygame.image.load('images/enemy/EL/bag1.png')
#self.image = pygame.image.load('images/enemy/EL/bz1.png')
self.rect = self.image.get_rect()
self.rect.x = self.game_settings.screen_width+self.rect.width*2
self.rect.bottom = 367
self.x = float(self.rect.x)#敌人的位置
self.speed_factor = self.game_settings.enemy_speed_factor
def blitme(self):
self.screen.blit(self.image,self.rect)
def update(self):
if self.game_settings.enemy_is_alive:
if self.game_settings.screen_rolling:
self.x -= self.speed_factor+1 #屏幕滚动速度减去敌人移动速度
self.rect.x = self.x
self.x -= self.speed_factor
self.rect.x = self.x
self.update_run_direction('EL')
else:
if self.game_settings.screen_rolling:
self.x -= self.speed_factor+1 #屏幕滚动速度减去敌人移动速度
self.rect.x = self.x
self.boom()
def boom(self):
self.names1 = locals()
self.players1 = []
for self.j in range(1,3):
self.names1['player_image%s' %self.j] = pygame.image.load('images/enemy/boom%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_i)]
self.pos_i += 0.1
if self.pos_i > 2.0:
self.game_settings.boom_end = True
def update_run_direction(self,direction):#循环图片
self.names1 = locals()
self.players1 = []
for self.j in range(1,5):
self.names1['player_image%s' %self.j] = pygame.image.load('images/enemy/'+direction+'/bag%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_j)]
self.pos_j += 0.1#图片变化速率
if self.pos_j > 4.0:#敌人bag有4个动作
self.pos_j = 0.0
总结
代码量有点大,所以就不一一给大家展示了,上方是部分代码展示
如果有想要的小伙伴可以看看相关文件