嗨!多一嘴: 童年玩过的经典游戏中,魂斗罗当属经典啊,最近用python写出来,赶一波回忆杀哈哈哈哈哈哈哈~
tips: 600多行代码,耐心耐心耐心!( 偷看*-|||
(1)子弹类(bullet.py)
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.y<self.my or self.y == self.my and self.x == self.mx and self.player.player_up:
self.y -= self.speed_factor
self.rect.y = self.y
elif self.x < self.mx or self.x == self.mx and self.player.player_direction == -1:
self.x -= self.speed_factor
self.rect.x = self.x
elif self.x>self.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)
(2)敌人类(enemy.py)
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('图片/enemy/EL/bag1.png')
#self.image = pygame.image.load('图片/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('图片/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('图片/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
(3)按钮类(button.py)
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)
(4)游戏大boss类(game_boss.py)
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
(5)方法封装类(game_function.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)
(6)游戏玩家类(game_player.py)
import pygame
class Game_Player():
def __init__(self,game_settings,screen):
self.screen = screen
self.game_settings = game_settings
self.image = pygame.image.load('图片/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('图片/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('图片/'+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('图片/'+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('图片/'+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('图片/PR/'+player_state+'.png')
if self.player_direction == -1:
self.image = pygame.image.load('图片/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('图片/PR/player.png')
self.reset_player()
if self.player_direction == -1:
self.image = pygame.image.load('图片/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
(7)游戏统计类(game_stats.py)
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
(8)设置类(settings.py)
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 750
self.player_speed = 5
self.jump_vel = -14.0#跳跃开始的速度,这里改动,game_player那里也要改动
self.bullet_speed_factor = 17
self.enemy_speed_factor = 1#屏幕滚动速度减去敌人移动速度为1(屏幕为5,敌人为4)移动时候才平滑。这里改动,enemy的update那里也要改
self.screen_rolling = False
self.enemy_is_alive = True
self.boom_end = False
self.players_limit = 3
self.player_is_alive = True
self.player_die_end = False
self.boss_jump_vel = -12.0
self.attack_1 = False
self.attack_2 = False
self.boss_jump = False
self.boss_run = False
self.boss_appear = False
self.boss_direction = 1#1位向左,-1为向右
self.boss_lift = 15
self.boss_alive = True
self.game_win = False
self.boss_boom_end = False
(9)主程序类(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("图片/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("魂斗罗")
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()