[PYTHON] Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 3] ~ Implementation of nervous breakdown ~

<< Part 2 | Part 4 >>

Prepare an image of playing cards.

Get an image of playing cards from "Irasutoya" by scraping

See here What a mess, please download 54 by hand or code with vue.

Let's display 52 playing cards. (Without Joker)

PlayScreen.vue


<template>
  <div>
    <span v-for="(card, index) in cards" :key="index">
      <img
        v-bind:src="card"
        alt=""
        width="110"
        height="135"
      />
    </span>
  </div>
</template>

<script>
export default {
  name: 'TopPage',
  data: function () {
    return {
      cards: [],
    }
  },
  methods: {
    setCardImage: function(type) {
      for (let i = 1; i < 14; i++) {
        let number = ('00' + i).slice(-2)
        this.cards.push(require('@/assets/images/card_' + type + '_' + number + '.png'))
      }
    }
  },
  mounted () {
    this.setCardImage('club');
    this.setCardImage('diamond');
    this.setCardImage('heart');
    this.setCardImage('spade');
  }
}
</script>

image.png

Shuffle playing cards

PlayScreen.vue


<template>
  ...
  ..
  .
</template>

<script>
export default {
  ...
  ..
  .
  methods: {
    //add to
    shuffle: function () {
      for (let i = this.cards.length - 1; i > 0; i--){
        let r = Math.floor(Math.random() * (i + 1));
        let tmp = this.cards[i];
        this.cards[i] = this.cards[r];
        this.cards[r] = tmp;
      }
    },
    ...
    ..
    .
  },
  mounted () {
    this.setCardImage('club');
    this.setCardImage('diamond');
    this.setCardImage('heart');
    this.setCardImage('spade');
    this.shuffle(); //add to
  }
}
</script>

It is now shuffled. image.png

Add front and back options to the card

PlayScreen.vue


<template>
  <div>
    <span v-for="(card, index) in cards" :key="index">
      <!--Set the URL of the image with style binding-->
      <img
        v-bind:src="card.isOpen ? card.cardInfo.front : card.cardInfo.back"
        alt=""
        width="110"
        height="135"
      />
    </span>
  </div>
</template>

<script>
export default {
  ...
  ..
  .
  methods: {
    ...
    ..
    .
    //Edit this method
    setCardImage: function(type) {
      for (let i = 1; i < 14; i++) {
        let number = ("00" + i).slice(-2);
        let card = {
          isOpen: false, //The initial state should be displayed on the back.
          cardInfo: {
            number: i,
            front: require(`@/assets/images/card_${type}_${number}.png`), //table
            back: require("@/assets/images/card_back.png ") //back
          }
        };
        this.cards.push(card);
      }
    }
  },
  mounted () {
    ...
    ..
    .
  }
}
</script>

image.png

Make it possible to display the card face up by clicking the image.

PlayScreen.vue


<template>
  <div>
    <span v-for="(card, index) in cards" :key="index">
      <!--Give a click event and set the added method-->
      <img
        v-bind:src="card.isOpen ? card.cardInfo.front : card.cardInfo.back"
        alt=""
        width="110"
        height="135"
        @click="open(index)"
      />
    </span>
  </div>
</template>

<script>
export default {
  ...
  ..
  .
  methods: {
    ...
    ..
    .
    //Add this method
    open: function(index) {
      //Turn the card face up
      this.cards[index].isOpen = true;
    }
  },
  mounted () {
    ...
    ..
    .
  }
}
</script>

image.png

Implementing nervous breakdown

Make sure all the cards are turned face down when you flip two cards

PlayScreen.vue


<template>
  ...
  ..
  .
</template>

<script>
let openCountByPlayer = 0; //How many cards the player turned over

export default {
  ...
  ..
  .
  methods: {
    ...
    ..
    .
    //Edit this method
    open: function(index) {
      //Do not turn over two or more
      if (openCountByPlayer + 1 > 2) return;

      //Turn the card face up
      this.cards[index].isOpen = true;

      openCountByPlayer++;

      if (openCountByPlayer == 2) {
        //Make sure the card is face down when you flip two cards
        this.reset();
      }
    },
    //Add this method
    reset: function() {
      setTimeout(() => {
        this.cards.forEach((card, index) => {
          if (card.isOpen) {
            this.cards[index].isOpen = false;
          }
        });
        openCountByPlayer = 0;
      }, 2000);
    },
  },
  ...
  ..
  .
}
</script>

If the two flipped numbers match, leave them face up

PlayScreen.vue


<template>
  <div>
    <span v-for="(card, index) in cards" :key="index">
      <!--Cards already acquired(isGet != null)Make it face up.-->
      <img
        v-bind:src="(card.isOpen || card.isGet != null) ? card.cardInfo.front : card.cardInfo.back"
        alt=""
        width="110"
        height="135"
        @click="open(index)"
      />
    </span>
  </div>
</template>

<script>
let openCountByPlayer = 0; //How many cards the player turned over
const PLAYER = "player";
const COMPUTER = "computer";

export default {
  ...
  ..
  .
  methods: {
    ...
    ..
    .
    //Edit this method
    setCardImage: function(type) {
      for (let i = 1; i < 14; i++) {
        let number = ("00" + i).slice(-2);
        let card = {
          isOpen: false,
          isGet: null, //Added isGet property
          cardInfo: {
            number: i,
            front: require(`@/assets/images/card_${type}_${number}.png`),
            back: require("@/assets/images/card_back.png ")
          }
        };
        this.cards.push(card);
      }
    },
    //Edit this method
    open: function(index) {
      //Do not turn over two or more
      if (openCountByPlayer + 1 > 2) return;

      //Turn the card face up
      this.cards[index].isOpen = true;

      openCountByPlayer++;

      if (openCountByPlayer == 2) {
        this.isNumbersMatch();

        //Make sure the card is face down when you flip two cards
        this.reset();
      }
    },
    //Edit this method
    reset: function() {
      setTimeout(() => {
        //Turn all cards other than the one you have acquired face down
        this.cards.forEach((card, index) => {
          if (card.isOpen && card.isGet == null) {
            this.cards[index].isOpen = false;
          }
        });
        openCountByPlayer = 0;
      }, 2000);
    },
    isNumbersMatch: function() {
      //Get a face-up image (cards that already match the numbers (isGet)!=null) is excluded)
      let openCards = [];
      this.cards.forEach((card, index) => {
        if (card.isOpen && card.isGet == null) {
          openCards.push({ index, card });
        }
      });

      //Get if the numbers match
      let firstCard = openCards[0];
      let secondCard = openCards[1];
      if (firstCard.card.cardInfo.number == secondCard.card.cardInfo.number) {
        this.cards[firstCard.index].isGet = PLAYER;
        this.cards[secondCard.index].isGet = PLAYER;
      }
    }
  },
  ...
  ..
  .
}
</script>

Display the number of acquired pairs

PlayScreen.vue


<template>
  <div>
    <v-card>
      <!--Use the added calculated property-->
      <v-row no-gutters>
        <v-col>Number of pairs you got:{{ getPlayerPairs }}set</v-col>
      </v-row>

      <v-divider class="mb-4"></v-divider>

      ...
      ..
      .
    </v-card>
  </div>
</template>

<script>
let openCountByPlayer = 0; //How many cards the player turned over
const PLAYER = "player";
const COMPUTER = "computer";

export default {
  ...
  ..
  .
  //Added calculated property
  computed: {
    getPlayerPairs: function() {
      if (!this.cards || this.cards.length === 0) return;

      return (
        this.cards.filter(card => { return card.isGet === PLAYER; }).length / 2
      );
    }
  },
  ...
  ..
  .
}
</script>

image.png

<< Part 2 | Part 4 >>

Recommended Posts

Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 3] ~ Implementation of nervous breakdown ~
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 6] ~ User Authentication 2 ~
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 5] ~ User authentication ~
Let's make a nervous breakdown app with Vue.js and Django-Rest-Framework [Part 2] ~ Vue setup ~
Let's make a nervous breakdown app with Vue.js and Django-Rest-Framework [Part 1] ~ Django setup ~
Try to make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 4] ~ MySQL construction and DB migration with Docker ~
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Let's make a simple game with Python 3 and iPhone
Let's make a Mac app with Tkinter and py2app
Let's make a GUI with python.
Let's make a breakout with wxPython
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
Make a DNN-CRF with Chainer and recognize the chord progression of music
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Implement a model with state and behavior (3) --Example of implementation by decorator
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Let's make an A to B conversion web application with Flask! From scratch ...
I tried to make a simple mail sending application with tkinter of Python
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
Run cbc of "Let's make a normal compiler" with Java 8 or later + 64bit
Get the stock price of a Japanese company with Python and make a graph
Implementation of TRIE tree with Python and LOUDS
Let's replace UWSC with Python (5) Let's make a Robot
Let's make an app that can search similar images with Python and Flask Part1
Let's make an app that can search similar images with Python and Flask Part2
Get a large amount of Starbucks Twitter data with python and try data analysis Part 1
Let's make a Makefile and build it (super beginner)
[Let's play with Python] Make a household account book
How to make a shooting game with toio (Part 1)
[# 2] Make Minecraft with Python. ~ Model drawing and player implementation ~
Create a batch of images and inflate with ImageDataGenerator
Let's make dependency management with pip a little easier
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
[Super easy] Let's make a LINE BOT with Python.
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
Let's make a websocket client with Python. (Access token authentication)
Make a Linux version of OpenSiv3D with find_package a little easier
As a result of mounting and tuning with POH! Lite
Make a tky2jgd plugin with no practicality in QGIS Part 2
Associate Python Enum with a function and make it Callable
Make a given number of seconds into hours, minutes and seconds
Let's create a PRML diagram with Python, Numpy and matplotlib.
Make a tky2jgd plugin with no practicality in QGIS Part 1
Detect objects of a specific color and size with Python
Make a 2D RPG with Ren'Py (3) -Items and Tool Shop
Let's make a diagram that can be clicked with IPython
Make a BLE thermometer and get the temperature with Pythonista3