不知道红白机上的《魂斗罗》是不是小伙伴儿们小时候的经典回忆呢?反正木木子那时候貌似是有
的,BUT 女孩子一般是不玩儿这类游戏的!!
(哎呀,木木子是不是偷偷保住了我年龄的秘密......)⛲⛲
魂斗罗是一款经典动作射击闯关类游戏,在当时的游戏市场上,可以说是一款相当有挑战难度的游戏。毕竟那时候消息闭塞,压根没有攻略之说,更何况那时年少,玩游戏图的是爽快,很多人也不会刻意去深度研究卡位、战斗技巧等,就是最基础的突突突、闪闪闪,于是就有了N种死亡经历。
今天木木子带大家来看看如何打造Python像素版本的经典游戏——魂斗罗叭!
像素风游戏类型之前也有几款的哈比如:《我的世界》、《坦克大战》、《超级马里奥》......可以
看文末的文章汇总哈!往期的资料素材等都在顾木子的主页左侧信息获取哈!
这边是一个简易版本的魂斗罗哈,有大佬自行优化一波!纯分享给大家娱乐。
大家都知道Python版本的游戏编写基本上都是Pygame游戏模块,今天就用的这个哈。
游戏规则:上下左右大家熟悉的——ASDW键,K跳跃,J放枪子儿。
打完一个小兵一个大boss就闯关成功了。嗯哼——超简单,有手就玩的赢的游戏23333!
像素风格任务人物形象(仅部分)
背景图展示:
主要人物素材:(部分图)
这款简易版本的《魂斗罗游戏》木木子环境设置的是:Python3、Pycharm、Pygame部分自带的不说了哈。
模块安装:
pip install -i https://pypi.douban.com/simple/ +模块名
二、正式敲代码
1)游戏敌方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('图片/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('图片/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('图片/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('图片/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('图片/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('图片/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('图片/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
2)玩家定义的子弹设置
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
def __init__(self,game_settings,screen,player):
super().__init__()
self.game_settings = game_settings
self.player = player
self.screen = screen
self.image = pygame.image.load('图片/bullet1.png')
self.rect = self.image.get_rect()
self.rect.left = player.rect.right-20#开始子弹默认往右射击
if self.player.player_direction == -1:
self.rect.right = player.rect.left+20
self.rect.centery = player.rect.centery-15
if self.player.player_down:
self.rect.centery = player.rect.centery#调整子弹位置
if self.player.player_up:
self.rect.bottom = player.rect.top+20
self.x = float(self.rect.x)
self.y = float(self.rect.y)
self.mx = self.x#mx为保存初始x的坐标
self.my = self.y
self.speed_factor = game_settings.bullet_speed_factor
def update(self):
if self.yself.mx or self.x == self.mx:#默认为右方向
self.x += self.speed_factor
self.rect.x = self.x
def blit_bullet(self):
self.screen.blit(self.image,self.rect)
3)主程序
'''
csdn:顾木子吖
'''
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#地图移动o
pygame.init()
screen = pygame.display.set_mode(
(game_settings.screen_width,game_settings.screen_height))
pygame.display.set_caption("经典像素风游戏——魂斗罗")
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()
等等等等.......游戏代码太多了。
我的速度调的比较快哈,自己调慢点儿。
嘿!新一轮的小游戏就写到这里结束啦,如需完整的资料项目点下方链接哦~
我还得想想下一个写什么内容呢——苦思冥想中。你们玩儿,我先撤了。
往期的资料素材等都在顾木子的主页左侧信息获取哈!