LoginSignup
5
3

More than 3 years have passed since last update.

Djangoでゲームを作る

Last updated at Posted at 2021-03-26

背景

学びのためにDjangoでゲームを作ってみることにする。
テトリスなど色々候補はあると思うがスロットゲームにすることにする。

モヤモヤ

Djangoと言ったってRestもあるし・・・画面も面倒だし・・・
どうしたって業務・即戦力がきになる・・・オシャレなこともしたいし・・・
→ データアクセスがどっちも1つの山なんでデータベースから取ってきて
JSONで投入するという一旦完璧を目指す。
この時に、スクレイピングとかデータ解析などもやってしまう。

考えないこと

DjangoRest

ロードマップ 作り物系

・公式やってみる。
・toDoリスト作ってみる
・ゲーム作ってみる
    → 今ここ
・データベースから取ってきて管理画面だけのを作ってみる。
 →JSONでデータ投入

仕様

・スロットマシン
・初期表示 4 が3枚並んでいる。
・ボタンを押すとランダム検索が起こる。
・スロットの絵柄と得点 例
6 3個揃うと 10000点 
7 3個揃うと  7000点
5 3個揃うと 300点
・結果を表示する。
・Python3 Djangoを使用

やって見て

もちろんやって見てよかったと思う。
やりすぎない感じで練習できたらと思う。
他の人のソースも参考にしたほうがいいと思う。

ソースコード

from django.http import HttpResponse
from django.shortcuts import render


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

def card(request):

    import random
    word = ''
    suits = ['S', 'H', 'D', 'C']
    ranks = range(4, 8)
    deck = [(x, y) for x in ranks for y in suits]
    if request.method == 'GET':
        dictionary = {
            'suit': 'C',
            'rank': '04',
            'suit2': 'D',
            'rank2': '04',
            'suit3': 'C',
            'rank3': '04',
             'word': word
        }

    elif request.method == 'POST':
        random.shuffle(deck)
        card1 = deck.pop()
        card2 = deck.pop()
        card3 = deck.pop()

        if card1[0] == 6 and card2[0] == 6 and card3[0] == 6:
            word = "scoreは10000点です"
        elif card1[0] == 7 and card2[0] == 7 and card3[0] == 7:
            word = "scoreは7000点です"
        elif card1[0] == 5 and card2[0] == 5 and card3[0] == 5:
            word = "scoreは300点です"

        dictionary = {
            'suit' : card1[1],
            'rank' : str(card1[0]).zfill(2),
            'suit2' : card2[1],
            'rank2' : str(card2[0]).zfill(2),
            'suit3': card3[1],
            'rank3': str(card3[0]).zfill(2),
            'word': word,
        }

    return render(request, 'card.html', dictionary)

{% load static %}
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>トランプ</title>
</head>
<body>
    {%static "cards/" as card_path %}
    <img src="{{card_path}}{{ suit }}_{{rank}}.png">
    <img src="{{card_path}}{{ suit2 }}_{{rank2}}.png">
    <img src="{{card_path}}{{ suit3 }}_{{rank3}}.png">
<form action="" method="post">
{% csrf_token %}
    <input type="submit" value="送信">
</form>
{{ word }}
</body>
</html>

スクリーンショット 2021-03-26 16.07.14.png

5
3
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
5
3