Python魂鬥羅小遊戲原始碼
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("魂鬥羅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()
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跟單網()
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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2752175/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 用Python寫個魂鬥羅,另附30個Python小遊戲原始碼Python遊戲原始碼
- 橋本和久 魂鬥羅祕籍之外的故事
- 《魂鬥羅歸來》之代理模式VS裝飾模式模式
- 《魂鬥羅歸來》之狀態模式VS策略模式模式
- 黑鯊助力《魂鬥羅:歸來》第七屆燃魂戰隊聯賽,專業競技更燃魂
- “魂鬥羅”值得現代遊戲開發者學習的上古神作遊戲開發
- 為什麼魂鬥羅只有128KB卻可以實現那麼長的劇情?
- 鬥魂競技場--英雄推薦
- Android鬥地主遊戲原始碼Android遊戲原始碼
- 深度分析FC魂鬥羅的操作設計理念,為何能成為後續同型別標杆產品型別
- 鬥羅世界NFT遊戲系統技術開發丨鬥羅世界鏈遊開發模式詳情遊戲模式
- xss小遊戲原始碼分析遊戲原始碼
- Java寫的鬥地主遊戲原始碼Java遊戲原始碼
- 13個Python小遊戲(含原始碼),開始敲起來,玩吧!Python遊戲原始碼
- 如何獲得微信小遊戲原始碼遊戲原始碼
- python實現超級瑪麗小遊戲(動圖演示+原始碼分享)Python遊戲原始碼
- Python 三人鬥地主程式碼Python
- python手機版做小遊戲程式碼大全-python簡單小遊戲程式碼 怎麼用Python製作簡單小遊戲...Python遊戲
- 100款微信小遊戲原始碼附H5開原始碼遊戲原始碼H5
- 【GDC 21】《對馬島之魂》戰鬥系統講解
- Java打飛機小遊戲(附完整原始碼)Java遊戲原始碼
- Python鬥地主Python
- 30個Python小遊戲,上班摸魚我能玩一天【內附原始碼】Python遊戲原始碼
- Python入門塔防小遊戲,開發步驟和原始碼,帶你輕鬆學pythonPython遊戲原始碼
- Python程式碼實現“FlappyBird”小遊戲PythonAPP遊戲
- python之掃雷小遊戲(附程式碼)Python遊戲
- JavaScript實現垃圾分類小遊戲教程,附原始碼!JavaScript遊戲原始碼
- 如何獲得微信小遊戲跳一跳原始碼遊戲原始碼
- NFT鏈遊鬥羅世界系統開發技術(功能分析)
- HTML小遊戲2—— 2048網頁版(附完整原始碼)HTML遊戲網頁原始碼
- 青蛙吃蚊子小遊戲原始碼_自適應手機端遊戲原始碼
- 老羅Android視訊[詳細版]全集下載(含原始碼)Android原始碼
- python入門靈魂5問Python
- 射擊遊戲的原始模型——牛仔決鬥遊戲模型
- (完整原始碼)貪吃蛇小遊戲——HTML+CSS+JavaScript實現原始碼遊戲HTMLCSSJavaScript
- pyqt5製作俄羅斯方塊小遊戲-----原始碼解析QT遊戲原始碼
- Python原始碼分析-PyDictObjectPython原始碼Object
- 原始碼安裝python原始碼Python