3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

python pygameでローグライクのRPGを作る -pygame-

Last updated at Posted at 2022-07-25

スペースを押す度変化するダンジョン

import pygame
import sys
import random

CYAN = (0,255,255)
GRAY = (96,96,96)

MAZE_W = 11
MAZE_H = 9

maze = []

for y in range(MAZE_H): #初期化
    maze.append([0]*MAZE_W)

def make_maze():
    XP = [0,1,0,-1] #一番左の柱以外は要素を0,1、0にして左に壁を作らない
    YP = [-1,0,1,0]

    for x in range(MAZE_W): #周りの壁 一番上と一番下
        maze[0][x] = 1 
        maze[MAZE_H-1][x] = 1
    for y in range(1, MAZE_H-1): #周りの壁 一番左と一番右
        maze[y][0] = 1
        maze[y][MAZE_W-1] = 1
    
    for y in range(1,MAZE_H-1): #床を設定
        for x in range(1,MAZE_W-1):
            maze[y][x] = 0
    
    for y in range(2, MAZE_H-2, 2): #2マス感覚で柱を設定
        for x in range(2, MAZE_W-2, 2):
            maze[y][x] = 1
    
    for y in range(2, MAZE_H-2, 2):
        for x in range(2, MAZE_W-2, 2):
            d = random.randint(0,3)
            if x > 2:
                d = random.randint(0,2)
            maze[y+YP[d]][x+XP[d]] = 1
def main():
    pygame.init()
    pygame.display.set_caption("迷路をつくる")
    screen = pygame.display.set_mode((528,432))
    clock = pygame.time.Clock()
    
    make_maze()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    make_maze()
        for y in range(MAZE_H):
            for x in range(MAZE_W):
                W = 48 #一マスの幅
                H = 48 #一マスの高さ
                X = x*W #描画の座標
                Y = y*H #描画の座標
                if maze[y][x] == 0:
                    pygame.draw.rect(screen,CYAN,[X,Y,W,H])
                if maze[y][x] == 1:
                    pygame.draw.rect(screen,GRAY,[X,Y,W,H])
        pygame.display.update()
        clock.tick(2)

if __name__ == '__main__':
    main()

mazemap.png

部屋と通路をつくる

import pygame
import sys
import random

BLACK = (0,0,0)
CYAN = (0,255,255)
GRAY = (96,96,96)

MAZE_W = 11
MAZE_H = 9

maze = []

for y in range(MAZE_H):
    maze.append([0]*MAZE_W)
    
DUNGEON_W = MAZE_W*3
DUNGEON_H = MAZE_H*3
dungeon =[]

for y in range(DUNGEON_H):
    dungeon.append([0]*DUNGEON_W)

imgWall = pygame.image.load("C:\ds_python/wall.png")
imgFloor = pygame.image.load("C:\ds_python/floor.png")

def make_dungeon():
    XP = [0,1,0,-1]
    YP = [-1,0,1,0]

    for x in range(MAZE_W):
        maze[0][x] = 1
        maze[MAZE_H-1][x] = 1
    for y in range(1, MAZE_H-1):
        maze[y][0] = 1
        maze[y][MAZE_W-1] = 1
    
    for y in range(1,MAZE_H-1):
        for x in range(1,MAZE_W-1):
            maze[y][x] = 0
    
    for y in range(2, MAZE_H-2, 2):
        for x in range(2, MAZE_W-2, 2):
            maze[y][x] = 1
    
    for y in range(2, MAZE_H-2, 2):
        for x in range(2, MAZE_W-2, 2):
            d = random.randint(0,3)
            if x > 2:
                d = random.randint(0,2)
            maze[y+YP[d]][x+XP[d]] = 1
    
    for y in range(DUNGEON_H):
        for x in range(DUNGEON_W):
            dungeon[y][x] = 9 #全て壁にする
            
    for y in range(1, MAZE_H-1):
        for x in range(1, MAZE_W-1):
            dx = x*3+1
            dy = y*3+1
            if maze[y][x] == 0: 
                if random.randint(0,99) < 20:
                    for ry in range(-1,2):
                        for rx in range(-1,2):
                            dungeon[dy+ry][dx+rx] = 0
                else:
                    dungeon[dy][dx] = 0
                    if maze[y-1][x] == 0:
                        dungeon[dy-1][dx] = 0
                    if maze[y+1][x] == 0:
                        dungeon[dy+1][dx] = 0
                    if maze[y][x-1] == 0:
                        dungeon[dy][dx-1] = 0
                    if maze[y][x+1] == 0:
                        dungeon[dy][dx+1] = 0
    
def main():
    pygame.init()
    pygame.display.set_caption("ダンジョンをつくる")
    screen = pygame.display.set_mode((1056,432))
    clock = pygame.time.Clock()
    
    make_dungeon()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    make_dungeon()
        for y in range(MAZE_H):
            for x in range(MAZE_W):
                X = x*48
                Y = y*48
                if maze[y][x] == 0:
                    pygame.draw.rect(screen,CYAN,[X,Y,48,48])
                if maze[y][x] == 1:
                    pygame.draw.rect(screen,GRAY,[X,Y,48,48])
                    
        for y in range(DUNGEON_H):
            for x in range(DUNGEON_W):
                X = x*16+528
                Y = y*16
                if dungeon[y][x] == 0:
                    screen.blit(imgFloor,[X,Y]) 
                if dungeon[y][x] == 9:
                    screen.blit(imgWall,[X,Y]) 
                    
        pygame.display.update()
        clock.tick(2)

if __name__ == '__main__':
    main()

mazemap.png

ダンジョン内を歩く

import pygame
import sys
import random

BLACK = (0,0,0)


MAZE_W = 11
MAZE_H = 9

maze = []

for y in range(MAZE_H):
    maze.append([0]*MAZE_W)
    
DUNGEON_W = MAZE_W*3
DUNGEON_H = MAZE_H*3

dungeon =[]

for y in range(DUNGEON_H):
    dungeon.append([0]*DUNGEON_W)

imgWall = pygame.image.load("C:\ds_python/wall.png")
imgFloor = pygame.image.load("C:\ds_python/floor.png")
imgPlayer = pygame.image.load("C:\ds_python/player.png")

pl_x = 4
pl_y = 4


def make_dungeon():
    XP = [0,1,0,-1]
    YP = [-1,0,1,0]

    for x in range(MAZE_W):
        maze[0][x] = 1
        maze[MAZE_H-1][x] = 1
    for y in range(1, MAZE_H-1):
        maze[y][0] = 1
        maze[y][MAZE_W-1] = 1
    
    for y in range(1,MAZE_H-1):
        for x in range(1,MAZE_W-1):
            maze[y][x] = 0
    
    for y in range(2, MAZE_H-2, 2):
        for x in range(2, MAZE_W-2, 2):
            maze[y][x] = 1
    
    for y in range(2, MAZE_H-2, 2):
        for x in range(2, MAZE_W-2, 2):
            d = random.randint(0,3)
            if x > 2:
                d = random.randint(0,2)
            maze[y+YP[d]][x+XP[d]] = 1
    
    for y in range(DUNGEON_H):
        for x in range(DUNGEON_W):
            dungeon[y][x] = 9
            
    for y in range(1, MAZE_H-1):
        for x in range(1, MAZE_W-1):
            dx = x*3+1
            dy = y*3+1
            if maze[y][x] ==0:
                if random.randint(0,99) < 20:
                    for ry in range(-1,2):
                        for rx in range(-1,2):
                            dungeon[dy+ry][dx+rx] = 0
                else:
                    dungeon[dy][dx] = 0
                    if maze[y-1][x] == 0:
                        dungeon[dy-1][dx] = 0
                    if maze[y+1][x] == 0:
                        dungeon[dy+1][dx] = 0
                    if maze[y][x-1] == 0:
                        dungeon[dy][dx-1] = 0
                    if maze[y][x+1] == 0:
                        dungeon[dy][dx+1] = 0
def draw_dungeon(bg):
    bg.fill(BLACK)
    #上下5マスずつ、プレイヤーが軸でダンジョンを描写
    for y in range(-5, 6):
        for x in range(-5, 6):
            X = (x+5)*16 #床か壁をつくるx座標
            Y = (y+5)*16 #床か壁をつくるy座標
            dx = pl_x + x #Xと対になっている、プレイヤーからx分足した場所のdungeon配列
            dy = pl_y + y #yと対になっている、プレイヤーからy分足した場所のdungeon配列
            if 0 <= dx and dx < DUNGEON_W and 0 <= dy and dy < DUNGEON_H:
                if dungeon[dy][dx] == 0:
                    bg.blit(imgFloor, [X,Y])
                if dungeon[dy][dx] == 9:
                    bg.blit(imgWall, [X,Y])
            if x == 0 and y == 0: #半分描写したらプレイヤーを中心に描写
                bg.blit(imgPlayer,[X,Y-8])
def move_player():
    global pl_x, pl_y
    key = pygame.key.get_pressed()
    if key[pygame.K_UP] == 1:
        if dungeon[pl_y-1][pl_x] != 9: pl_y = pl_y - 1
    if key[pygame.K_DOWN] == 1:
        if dungeon[pl_y+1][pl_x] != 9: pl_y = pl_y + 1
    if key[pygame.K_LEFT] == 1:
        if dungeon[pl_y][pl_x-1] != 9: pl_x = pl_x - 1
    if key[pygame.K_RIGHT] == 1:
        if dungeon[pl_y][pl_x+1] != 9: pl_x = pl_x + 1
    
def main():
    pygame.init()
    pygame.display.set_caption("ダンジョンを歩く")
    screen = pygame.display.set_mode((176,176))
    clock = pygame.time.Clock()
    
    make_dungeon()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        
        move_player()
        draw_dungeon(screen)
        pygame.display.update()
        clock.tick(5)

if __name__ == '__main__':
    main()

mazemap.png

戦闘でメッセージを表示

import pygame
import sys

WHITE = (255,255,255)
BLACK = (0,0,0)
imgBtlBG = pygame.image.load("C:\ds_python/btlbg.png")
imgEnemy = pygame.image.load("C:\ds_python/enemy1.png")

emy_x = 440-imgEnemy.get_width()/2
emy_y = 560-imgEnemy.get_height()

message = [""]*10

def init_message():
    for i in range(10):
        message[i] = ""

def set_message(msg): #空であればmsgを追加
    for i in range(10):
        if message[i] == "":
            message[i] = msg
            return #処理が返る
    for i in range(9): #0~8のリストをひとつずらす
        message[i] = message[i+1]
        message[9] = msg #9のリストを追加

def draw_text(bg, txt, x, y, fnt, col):
    sur = fnt.render(txt, True, BLACK)
    bg.blit(sur, [x+1,y+2])
    sur = fnt.render(txt,True,col)
    bg.blit(sur, [x,y])
    
def draw_battle(bg, fnt):
    bg.blit(imgBtlBG, [0,0])
    bg.blit(imgEnemy, [emy_x,emy_y])
    for i in range(10):
        draw_text(bg, message[i],600,100+i*50,fnt,WHITE)


def main():
    pygame.init()
    pygame.display.set_caption("戦闘中のメッセージ")
    screen = pygame.display.set_mode((880,720))
    clock = pygame.time.Clock()
    font = pygame.font.Font(None, 40)
    init_message()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                set_message("KEYDOWN"+str(event.key))
        draw_battle(screen, font)
        pygame.display.update()
        clock.tick(5)

if __name__ == '__main__':
    main()

mazemap.png

ターン制の処理

import pygame
import sys
import random
from pygame.locals import *

WHITE = (255,255,255)
BLACK = (0,0,0)
imgBtlBG = pygame.image.load("C:\ds_python/btlbg.png")
imgEffect = pygame.image.load("C:\ds_python/effect_a.png")
imgEnemy = pygame.image.load("C:\ds_python/enemy1.png")

emy_x = 440-imgEnemy.get_width()/2
emy_y = 560-imgEnemy.get_height()

emy_step = 0
emy_blink = 0
dmg_eff = 0
COMMAND = ["[A]ttack","[P]otion","[S]laze gem", "[R]un"]



message = [""]*10

def init_message():
    for i in range(10):
        message[i] = ""

def set_message(msg):
    for i in range(10):
        if message[i] == "":
            message[i] = msg
            return
    for i in range(9):
        message[i] = message[i+1]
        message[9] = msg

def draw_text(bg, txt, x, y, fnt, col):
    sur = fnt.render(txt, True, BLACK)
    bg.blit(sur, [x+1,y+2])
    sur = fnt.render(txt,True,col)
    bg.blit(sur, [x,y])
    
def draw_battle(bg, fnt):
    global emy_blink, dmg_eff
    bx = 0
    by = 0
    if dmg_eff > 0:
        dmg_eff = dmg_eff -1
        bx = random.randint(-20, 20)
        by = random.randint(-10, 10)
    bg.blit(imgBtlBG, [bx,by])
    if emy_blink % 2 == 0:
        bg.blit(imgEnemy, [emy_x,emy_y+emy_step])
    if emy_blink > 0:
        emy_blink = emy_blink - 1
    for i in range(10):
        draw_text(bg, message[i],600,100+i*50,fnt,WHITE)

def battle_command(bg, fnt):
    for i in range(4):
        draw_text(bg, COMMAND[i], 20, 300+60*i, fnt, WHITE)


def main():
    global emy_step, emy_blink, dmg_eff
    idx = 10
    tmr = 0
    pygame.init()
    pygame.display.set_caption("ターン制の処理")
    screen = pygame.display.set_mode((880,720))
    clock = pygame.time.Clock()
    font = pygame.font.Font(None, 40)
    init_message()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        draw_battle(screen, font)
        tmr = tmr + 1
        key = pygame.key.get_pressed()
        
        if idx == 10:
            if tmr == 1: set_message("Encounter")
            if tmr == 6:
                idx = 11
                tmr = 0
        elif idx == 11:
            if tmr == 1: set_message("Your turn")
            battle_command(screen, font)
            if key[K_a] == 1 or key[K_SPACE] == 1:
                idx = 12
                tmr = 0
        elif idx == 12:
            if tmr == 1: set_message("Your attack!")
            if 2 <= tmr and tmr <= 4:
                screen.blit(imgEffect, [700-tmr*120, -100+tmr*120])
            if tmr == 5:
                emr_blink = 5
                set_message("***pts of damage!")
            if tmr == 16:
                idx =13
                tmr = 0
        elif idx == 13:
            if tmr == 1: set_message("Enemy turn.")
            if tmr == 5:
                set_message("Enemy attack!")
                emy_step = 30
            if tmr == 9:
                set_message("***pts of damage!")
                dmg_eff = 5
                emy_step = 0
            if tmr == 20:
                idx = 11
                tmr = 0
        pygame.display.update()
        clock.tick(5)

if __name__ == '__main__':
    main()

mazemap.png

ローグライクゲームを完成させる

import pygame
import sys
import random
from pygame.locals import *

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255, 0, 0)
CYAN = (0, 255, 255)
BLINK = [(224,255,255),(192,240,255),(128,224,255),(64,192,255),(128,224,225),(192,240,255)]

imgTitle = pygame.image.load("C:\ds_python/image/title.png")
imgWall = pygame.image.load("C:\ds_python/image/wall.png")
imgWall2 = pygame.image.load("C:\ds_python/image/wall2.png")
imgDark = pygame.image.load("C:\ds_python/image/dark.png")
imgPara = pygame.image.load("C:\ds_python/image/parameter.png")
imgBtlBG = pygame.image.load("C:\ds_python/image/btlbg.png")
imgEnemy = pygame.image.load("C:\ds_python/image/enemy0.png")
imgItem = [
    pygame.image.load("C:\ds_python/image/potion.png"),
    pygame.image.load("C:\ds_python/image/blaze_gem.png"),
    pygame.image.load("C:\ds_python/image/spoiled.png"),
    pygame.image.load("C:\ds_python/image/apple.png"),
    pygame.image.load("C:\ds_python/image/meat.png")
]
imgFloor = [
    pygame.image.load("C:\ds_python/image/floor.png"),
    pygame.image.load("C:\ds_python/image/tbox.png"),
    pygame.image.load("C:\ds_python/image/cocoon.png"),
    pygame.image.load("C:\ds_python/image/stairs.png")
]
imgPlayer = [
    pygame.image.load("C:\ds_python/image/mychr0.png"),
    pygame.image.load("C:\ds_python/image/mychr1.png"),
    pygame.image.load("C:\ds_python/image/mychr2.png"),
    pygame.image.load("C:\ds_python/image/mychr3.png"),
    pygame.image.load("C:\ds_python/image/mychr4.png"),
    pygame.image.load("C:\ds_python/image/mychr5.png"),
    pygame.image.load("C:\ds_python/image/mychr6.png"),
    pygame.image.load("C:\ds_python/image/mychr7.png"),
    pygame.image.load("C:\ds_python/image/mychr8.png"),
]
imgEffect = [
    pygame.image.load("C:\ds_python/image/effect_a.png"),
    pygame.image.load("C:\ds_python/image/effect_b.png")
]

speed = 1
idx = 0
tmr = 0
floor = 0
fl_max = 1
welcome = 0

pl_x = 0
pl_y = 0
pl_d = 0
pl_a = 0
pl_lifemax = 0
pl_life = 0
pl_str = 0
food = 0
potion = 0
blazegem = 0
treature = 0

emy_name = ""
emy_lifemax = 0
emy_life = 0
emy_str = 0
emy_x = 0
emy_y = 0
emy_step = 0
emy_blink = 0

dmg_eff = 0
btl_cmd = 0

COMMAND =["[A]ttack","[P]otion","[B]laze gem","[R]un"]
TRE_NAME =["Potion","Blaze gem","Food spoiled","Food +20", "Food + 100"]
EMY_NAME =[
    "Green slime",
    "Red slime",
    "Axe beast",
    "Ogre",
    "Sword man",
    "Death hornet",
    "Signal slime",
    "Devil plant",
    "Twin killer",
    "Hell"
]

MAZE_W = 11
MAZE_H = 9
maze = []
for y in range(MAZE_H):
    maze.append([0]*MAZE_W)

DUNGEON_W = MAZE_W*3
DUNGEON_H = MAZE_H*3
dungeon = []

for y in range(DUNGEON_H):
    dungeon.append([0]*DUNGEON_W)

def make_dungeon():
    XP = [0,1,0,-1]
    YP = [-1,0,1,0]

    for x in range(MAZE_W):
        maze[0][x] = 1
        maze[MAZE_H-1][x] = 1
    for y in range(1, MAZE_H-1):
        maze[y][0] = 1
        maze[y][MAZE_W-1] = 1
    
    for y in range(1,MAZE_H-1):
        for x in range(1,MAZE_W-1):
            maze[y][x] = 0
    
    for y in range(2, MAZE_H-2, 2):
        for x in range(2, MAZE_W-2, 2):
            maze[y][x] = 1
    
    for y in range(2, MAZE_H-2, 2):
        for x in range(2, MAZE_W-2, 2):
            d = random.randint(0,3)
            if x > 2:
                d = random.randint(0,2)
            maze[y+YP[d]][x+XP[d]] = 1
    
    for y in range(DUNGEON_H):
        for x in range(DUNGEON_W):
            dungeon[y][x] = 9
            
    for y in range(1, MAZE_H-1):
        for x in range(1, MAZE_W-1):
            dx = x*3+1
            dy = y*3+1
            if maze[y][x] == 0:
                if random.randint(0,99) < 20:
                    for ry in range(-1,2):
                        for rx in range(-1,2):
                            dungeon[dy+ry][dx+rx] = 0
                else:
                    dungeon[dy][dx] = 0
                    if maze[y-1][x] == 0:
                        dungeon[dy-1][dx] = 0
                    if maze[y+1][x] == 0:
                        dungeon[dy+1][dx] = 0
                    if maze[y][x-1] == 0:
                        dungeon[dy][dx-1] = 0
                    if maze[y][x+1] == 0:
                        dungeon[dy][dx+1] = 0
                        
def draw_dungeon(bg, fnt):
    bg.fill(BLACK)
    #上下5マスずつ、プレイヤーが軸でダンジョンを描写
    for y in range(-4, 6):
        for x in range(-5, 6):
            X = (x+5)*80 #床か壁をつくるx座標
            Y = (y+4)*80 #床か壁をつくるy座標
            dx = pl_x + x #Xと対になっている、プレイヤーからx分足した場所のdungeon配列
            dy = pl_y + y #yと対になっている、プレイヤーからy分足した場所のdungeon配列
            if 0 <= dx and dx < DUNGEON_W and 0 <= dy and dy < DUNGEON_H:
                if dungeon[dy][dx] <= 3:
                    bg.blit(imgFloor[dungeon[dy][dx]], [X,Y])
                if dungeon[dy][dx] == 9:
                    bg.blit(imgWall, [X,Y-40])
                    if dy > 1 and dungeon[dy-1][dx] == 9:
                        bg.blit(imgWall2, [X,Y-80])
            if x == 0 and y == 0: #半分描写したらプレイヤーを中心に描写
                bg.blit(imgPlayer[pl_a],[X,Y-40])
    bg.blit(imgDark, [0,0])
    draw_pare(bg,fnt)

def put_event():
    global pl_x,pl_y,pl_d,pl_a
    
    while True:
        x = random.randint(3, DUNGEON_W-4)
        y = random.randint(3, DUNGEON_H-4)
        if(dungeon[y][x] == 0):
            for ry in range(-1, 2):
                for rx in range(-1, 2):
                    dungeon[y+ry][x+rx] = 0
            dungeon[y][x] = 3
            break
    for i in range(60):
        x = random.randint(3, DUNGEON_W-4)
        y = random.randint(3, DUNGEON_H-4)
        if(dungeon[y][x] == 0):
            dungeon[y][x] = random.choice([1,2,2,2,2])
            
    while True:
        pl_x = random.randint(3, DUNGEON_W-4)
        pl_y = random.randint(3, DUNGEON_H-4)
        if(dungeon[pl_y][pl_x] == 0):
            break
    
    pl_d = 1
    pl_a = 2


def move_player(key):
    global idx, tmr, pl_x, pl_y, pl_d, pl_a, pl_life, food, potion, blazegem, treasure
    
    if dungeon[pl_y][pl_x] == 1:
        dungeon[pl_y][pl_x] = 0
        treasure = random.choice([0,0,0,1,1,1,1,1,1,2])
        if treasure == 0:
            potion = potion + 1
        if treasure == 1:
            blazegem = blazegem + 1
        if treasure == 2:
            food = int(food/2)
        idx = 3
        tmr = 0
        return
    if dungeon[pl_y][pl_x] == 2:
        dungeon[pl_y][pl_x] = 0
        r = random.randint(0,99)
        if r < 40 :
            treasure = random.choice([3,3,3,4])
            if treasure == 3: food = food + 20
            if treasure == 4: food = food + 100
            idx = 3
            tmr = 0
        else:
            idx = 10
            tmr = 0
        return
    if dungeon[pl_y][pl_x] == 3:
        idx = 2
        tmr = 0
        return
    x = pl_x
    y = pl_y
    if key[K_UP] == 1:
        pl_d = 0
        if dungeon[pl_y-1][pl_x] != 9:
            pl_y = pl_y - 1
    if key[K_DOWN] == 1:
        pl_d = 1
        if dungeon[pl_y+1][pl_x] != 9:
            pl_y = pl_y + 1
    if key[K_LEFT] == 1:
        pl_d = 2
        if dungeon[pl_y][pl_x-1] != 9:
            pl_x = pl_x - 1
    if key[pygame.K_RIGHT] == 1:
        pl_d = 3
        if dungeon[pl_y][pl_x+1] != 9:
            pl_x = pl_x + 1
    pl_a = pl_d * 2
    if pl_x != x or pl_y != y:
        pl_a = pl_a + tmr%2
        if food > 0:
            food = food -1
            if pl_life < pl_lifemax:
                pl_life = pl_life + 1
        else:
            pl_life = pl_life - 5
            if pl_life <= 0:
                pl_life = 0
                pygame.mixer.music.stop()
                idx = 9
                tmr = 0



def draw_text(bg, txt, x, y, fnt, col):
    sur = fnt.render(txt, True, BLACK)
    bg.blit(sur, [x+1,y+2])
    sur = fnt.render(txt,True,col)
    bg.blit(sur, [x,y])
    
def draw_pare(bg,fnt):
    X = 30
    Y = 600
    bg.blit(imgPara, [X,Y])
    col = WHITE
    if pl_life < 10 and tmr%2 == 0: col = RED
    draw_text(bg, "{}/{}".format(pl_life, pl_lifemax), X+128, Y+6, fnt, col)
    draw_text(bg, str(pl_str), X+128, Y+33, fnt, WHITE)
    col = WHITE
    if food == 0 and tmr%2 == 0: col = RED
    draw_text(bg, str(food), X+128, Y+60, fnt, col)
    draw_text(bg, str(potion), X+266, Y+6, fnt, WHITE)
    draw_text(bg, str(blazegem), X+266, Y+33, fnt, WHITE)
    

def init_battle():
    global imgEnemy, emy_name, emy_lifemax, emy_life, emy_str, emy_x, emy_y
    typ = random.randint(0, floor)
    if floor >= 10:
        typ = random.randint(0, 9)
    lev = random.randint(1, floor)
    imgEnemy = pygame.image.load("C:\ds_python/image/enemy" + str(typ)+ ".png")
    emy_name = EMY_NAME[typ] + " LV" + str(lev)
    emy_lifemax = 60*(typ+1) + (lev-1)*10
    emy_life = emy_lifemax
    emy_str = int(emy_lifemax/8)
    emy_x = 440-imgEnemy.get_width()/2
    emy_y = 560-imgEnemy.get_height()

def draw_bar(bg, x, y, w, h, val, ma):
    pygame.draw.rect(bg, WHITE, [x-2, y-2, w+4, h+4])
    pygame.draw.rect(bg, BLACK, [x, y, w, h])
    if val > 0:
        pygame.draw.rect(bg, (0,128,255),[x,y,w*val/ma, h])

def draw_battle(bg, fnt):
    global emy_blink, dmg_eff
    bx = 0
    by = 0
    if dmg_eff > 0:
        dmg_eff = dmg_eff -1
        bx = random.randint(-20, 20)
        by = random.randint(-10, 10)
    bg.blit(imgBtlBG, [bx,by])
    if emy_life > 0 and emy_blink % 2 == 0:
        bg.blit(imgEnemy, [emy_x,emy_y+emy_step])
    draw_bar(bg,340,580,200,10, emy_life, emy_lifemax)
    if emy_blink > 0:
        emy_blink = emy_blink - 1
    for i in range(10):
        draw_text(bg, message[i],600,100+i*50,fnt,WHITE)
    draw_pare(bg, fnt)

def battle_command(bg, fnt, key):
    global btl_cmd
    ent = False
    if key[K_a]:
        btl_cmd = 0
        ent = True
    if key[K_p]:
        btl_cmd = 1
        ent = True
    if key[K_b]:
        btl_cmd = 2
        ent = True
    if key[K_r]:
        btl_cmd = 3
    if key[K_UP] and btl_cmd > 0:
        btl_cmd -= 1
    if key[K_DOWN] and btl_cmd < 3:
        btl_cmd += 1
    if key[K_SPACE] or key[K_RETURN]:
        ent = True
    for i in range(4):
        c = WHITE
        if btl_cmd == i: c = BLINK[tmr%6]
        draw_text(bg, COMMAND[i], 20, 360+i*60, fnt, c)
    return ent
message = [""]*10

def init_message():
    for i in range(10):
        message[i] = ""

def set_message(msg):
    for i in range(10):
        if message[i] == "":
            message[i] = msg
            return
    for i in range(9):
        message[i] = message[i+1]
    message[9] = msg

def main():
    global speed, idx, tmr, floor, fl_max, welcome
    global pl_a, pl_lifemax, pl_life, pl_str, food, potion, blazegem
    global emy_life, emy_step, emy_blink, dmg_eff
    dmg = 0
    lif_p = 0
    str_p = 0
    
    pygame.init()
    pygame.display.set_caption("one hour Dungeon")
    screen = pygame.display.set_mode((880,720))
    clock = pygame.time.Clock()
    font = pygame.font.Font(None, 40)
    fontS = pygame.font.Font(None, 40)
    se = [pygame.mixer.Sound("C:\ds_python/sound/ohd_se_attack.ogg"),
            pygame.mixer.Sound("C:\ds_python/sound/ohd_se_blaze.ogg"),
            pygame.mixer.Sound("C:\ds_python/sound/ohd_se_potion.ogg"),
            pygame.mixer.Sound("C:\ds_python/sound/ohd_jin_gameover.ogg"),
            pygame.mixer.Sound("C:\ds_python/sound/ohd_jin_levup.ogg"),
            pygame.mixer.Sound("C:\ds_python/sound/ohd_jin_win.ogg")
    ]
    
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_s:
                    speed = speed + 1
                    if speed == 4:
                        speed = 1
                        
        tmr = tmr + 1
        key = pygame.key.get_pressed()
        
        if idx == 0:
            if tmr == 1:
                pygame.mixer.music.load("C:\ds_python/sound/ohd_bgm_title.ogg")
                pygame.mixer.music.play(-1)
            screen.fill(BLACK)
            screen.blit(imgTitle, [40,60])
            if fl_max >= 2:
                draw_text(screen, "You reachd floor {}".format(fl_max),300, 460, font, CYAN)
            draw_text(screen, "Press space key",320, 560, font, BLINK[tmr%6])
            if key[K_SPACE] == 1:
                make_dungeon()
                put_event()
                floor = 1
                welcome = 15
                pl_lifemax = 300
                pl_life = pl_lifemax
                pl_str = 100
                food = 300
                potion = 0
                blazegen = 0
                idx = 1
                pygame.mixer.music.load("C:\ds_python/sound/ohd_bgm_field.ogg")
                pygame.mixer.music.play(-1)
        elif idx == 1:
            move_player(key)
            draw_dungeon(screen, fontS)
            draw_text(screen, "floor {} ({},{})".format(floor, pl_x, pl_y), 60, 40, fontS, WHITE)
            if welcome > 0:
                welcome = welcome - 1
                draw_text(screen, "Welcome to floor {}".format(floor), 300, 180, font, CYAN)
                
        elif idx == 2:
            draw_dungeon(screen, fontS)
            if 1 <= tmr and tmr <= 5:
                h = 80*tmr
                pygame.draw.rect(screen, BLACK, [0,0, 880, h])
                pygame.draw.rect(screen, BLACK, [0, 720-h, 880, h])
            if tmr == 5:
                floor = floor + 1
                if floor > fl_max:
                    fl_max = floor
                welcome = 15
                make_dungeon()
                put_event()
            if 6 <= tmr and tmr <= 9:
                h = 80*(10-tmr)
                pygame.draw.rect(screen, BLACK, [0,0,880,h])
                pygame.draw.rect(screen, BLACK, [0,720-h, 880, h])
            if tmr == 10:
                idx = 1
        elif idx == 3:
            draw_dungeon(screen, fontS)
            screen.blit(imgItem[treasure], [320,220])
            draw_text(screen, TRE_NAME[treasure], 380, 240, font, WHITE)
            if tmr == 10:
                idx =1
        
        elif idx == 9:
            if tmr <= 30:
                PL_TURN = [2,4,0,6]
                pl_a = PL_TURN[tmr%4]
                if tmr == 30: pl_a = 8
                draw_dungeon(screen, fontS)
            elif tmr == 31:
                se[3].play()
                draw_text(screen, "You died.", 360, 240, font, RED)
                draw_text(screen, "Game over", 360, 380, font, RED)
            elif tmr == 100:
                idx = 0
                tmr = 0
            
        if idx == 10:
            if tmr == 1: 
                pygame.mixer.music.load("C:\ds_python/sound/ohd_bgm_battle.ogg")
                pygame.mixer.music.play(-1)
                init_battle()
                init_message()
            if tmr <= 4:
                bx = (4-tmr) * 220
                by = 0
                screen.blit(imgBtlBG, [bx,by])
                draw_text(screen, "Encounter", 350, 200, font, WHITE)
            if tmr <= 16:
                draw_battle(screen, fontS)
                draw_text(screen,emy_name+" appear!", 300, 200, font, WHITE)
            else:
                idx = 11
                tmr = 0
        elif idx == 11:
            draw_battle(screen, fontS)
            if tmr == 1: set_message("Your turn")
            if battle_command(screen, font, key) == True:
                if btl_cmd == 0:
                    idx =12
                    tmr = 0
                if btl_cmd == 1 and potion > 0:
                    idx = 20
                    tmr = 0
                if btl_cmd == 2 and blazegem > 0:
                    idx = 21
                    tmr = 0
                if btl_cmd == 3:
                    idx = 14
                    tmr = 0
                    
                
            if key[K_a] == 1 or key[K_SPACE] == 1:
                idx = 12
                tmr = 0
        elif idx == 12:
            draw_battle(screen,fontS)
            if tmr == 1: 
                set_message("Your attack!")
                se[0].play()
                dmg = pl_str + random.randint(0,9)
            if 2 <= tmr and tmr <= 4:
                screen.blit(imgEffect[0], [700-tmr*120, -100+tmr*120])
            if tmr == 5:
                emr_blink = 5
                set_message(str(dmg) + "pts of damage!")
            if tmr == 11:
                emy_life = emy_life -dmg
                if emy_life <= 0:
                    emy_life = 0
                    idx = 16
                    tmr = 0
            if tmr == 16:
                idx =13
                tmr = 0
                
        elif idx == 13:
            draw_battle(screen,fontS)
            if tmr == 1: 
                set_message("Enemy turn.")
            if tmr == 5:
                set_message(emy_name + " attack!")
                se[0].play
                emr_step = 30
            if tmr == 9:
                dmg = emy_str + random.randint(0,9)
                set_message(str(dmg) + "pts of damege!")
                dmg_eff = 5
                emy_step = 0
            if tmr == 15:
                pl_life = pl_life -dmg
                if pl_life <= 0:
                    pl_life = 0
                    idx = 15
                    tmr = 0
            if tmr == 20:
                idx = 11
                tmr = 0

        elif idx == 14:
            draw_battle(screen,fontS)
            if tmr == 1: set_message("...")
            if tmr == 2: set_message(".....")
            if tmr == 3: set_message(".......")
            if tmr == 4: set_message(".........")
            if tmr == 5:
                if random.randint(0,99) < 60:
                    idx = 22
                else:
                    set_message("You failed to flee.")
            if tmr == 10:
                idx =13
                tmr = 0
        elif idx == 15:
            draw_battle(screen,fontS)
            if tmr == 1:
                pygame.mixer.music.stop()
                set_message("You lose")
        elif idx == 16:
            draw_battle(screen,fontS)
            if tmr == 1:
                set_message("You win!")
                pygame.mixer.music.stop()
                se[5].play()
            if tmr == 28:
                idx = 22
                if random.randint(0, emy_lifemax) > random.randint(0, pl_lifemax):
                    idx = 17
                    
        elif idx == 17:
            draw_battle(screen, fontS)
            if tmr == 1:
                set_message("Level up!")
                se[4].play()
                lif_p = random.randint(10, 20)
                str_p = random.randint(5,10)
            if tmr == 21:
                set_message("Max life + " + str(lif_p))
                pl_lifemax = pl_lifemax + lif_p
            if tmr == 26:
                set.message("str + " + str(str_p))
                pl_str = pl_str + str_p
            if tmr == 50:
                idx = 22
        elif idx == 20:
            draw_battle(screen, fontS)
            if tmr == 1:
                set_message("Potion!")
                se[2].play
            if tmr == 6:
                pl_life = pl_lifemax
                potion = potion - 1
            if tmr == 11:
                idx = 13
                tmr = 0
                
        elif idx == 21:
            draw_battle(screen, fontS)
            img_rz = pygame.transform.rotozoom(imgEffect[1], 30*tmr, (12-tmr)/8)
            X = 440-img_rz.get_width()/2
            Y = 360-img_rz.get_height()/2
            screen.blit(img_rz, [X,Y])
            if tmr == 1:
                set_message("Blaze gem!")
                se[1].play
            if tmr == 6:
                blazegem = blazegem - 1
            if tmr == 11:
                dmg = 1000
                idx = 12
                tmr = 4
        elif idx == 22:
            pygame.mixer.music.load("C:\ds_python/sound/ohd_bgm_field.ogg")
            pygame.mixer.music.play(-1)
            idx = 1
        
        draw_text(screen, "[S]peed " +str(speed), 740, 40, fontS, WHITE)

        pygame.display.update()
        clock.tick(3+2*speed)

if __name__ == '__main__':
    main()

mazemap.png

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?