0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQLiteを使ってEnemy(敵)のデータベースを作りました。
このデータベースの表テーブルは:
[id,name,atk,def,spd,exp,hp]
idはIDで、nameは敵の名前、atkは攻撃、defはディフェンス、spdは早さ、expは得られるエックスペリアンス、とhpはHPのことです。

Screenshot 2023-12-18 at 20.33.22.png

その後、pythonでAPIを書きました。
ここのAPIは三つのMETHODがありますが、注目したいのは「#ENEMY API」。
このMETHODで、敵の名前をパラメーターとして使って、atk,def,spd,exp,hpのデータを得ます。

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Dict, Any

import sqlite3

app = FastAPI()

class Word(BaseModel):
    player: str

def db_connection(database_name):
    try:
        conn = sqlite3.connect(database_name)
        return conn
    except sqlite3.Error as e:
        print(e)
        return None

@app.get("/save/player", response_model=List[str])
def get_player_names():
    conn = db_connection("savedata.sqlite")
    cursor = conn.cursor()
    
    cursor.execute("SELECT playername FROM savedata")
    player_names = cursor.fetchall()

    if not player_names:
        raise HTTPException(status_code=404, detail="No player names found")

    return [name[0] for name in player_names]

@app.get("/save/index", response_model=List[int])
def get_player_ids():
    conn = db_connection("savedata.sqlite")
    cursor = conn.cursor()
    
    cursor.execute("SELECT savefileindex FROM savedata")
    player_ids = cursor.fetchall()

    if not player_ids:
        raise HTTPException(status_code=404, detail="No player IDs found")

    return [id[0] for id in player_ids]


#ENEMY API

@app.get("/enemy/{param}/{enemy_name}", response_model=Any)
def get_enemy_param(param: str, enemy_name: str):
    conn2 = db_connection("enemy.sqlite")
    cursor2 = conn2.cursor()
    
    cursor2.execute(f"SELECT {param} FROM enemy WHERE name=?", (enemy_name,))
    enemy = cursor2.fetchall()

    if not enemy:
        raise HTTPException(status_code=404, detail=f"ERROR")

    return enemy[0][0]


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=7070, debug=True)

このAPIでGdevelopで作った場面で表示します。
今のところはまだゲームの図鑑です。
写真などのビジュアルまだ作っていないです。

Screenshot 2023-12-18 at 20.50.23.png

今のところはデータベースとAPIとゲームエンジンが正常に動作できます。
明日からレベル1/エリア1の場面やゲームプレイを作ります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?