分别用Vue和Java来实现的风靡一时的2048 游戏

这篇具有很好参考价值的文章主要介绍了分别用Vue和Java来实现的风靡一时的2048 游戏。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


2048 游戏是一个基于网格的数字益智游戏,玩家需要通过滑动相同的数字来合并它们,并最终得到一个值为 2048 的方块。以下是分别用Vue和Java来实现的 2048 游戏,包含运行效果。

1、Vue实现

首先,创建一个名为Game.vue的 Vue 单文件组件,代码如下:

<template>  
 <div class="game-container">  
   <div class="grid">  
     <div v-for="(row, rowIndex) in board" :key="rowIndex" class="cell">  
       <div v-if="row.length">  
         <div v-for="(cell, colIndex) in row" :key="colIndex" :class="{ 'highlight': cell === current }">  
           {{ cell }}  
         </div>  
       </div>  
     </div>  
   </div>  
   <div class="score">  
     <p>得分:{{ score }}</p>  
   </div>  
   <button @click="newGame">重新开始</button>  
 </div>  
</template>
<script>  
export default {  
 data() {  
   return {  
     board: [  
       [1, 1, 2, 2],  
       [3, 3],  
       [4, 4],  
       [4, 4],  
       [2, 2],  
       [1, 1, 3, 3],  
       [2, 2],  
       [4, 4],  
     ],  
     current: null,  
     score: 0,  
   };  
 },  
 methods: {  
   move(direction) {  
     if (direction === 'left' && this.current && this.current.leftCell) {  
       this.current.leftCell = this.current.leftCell.left;  
       if (!this.current.leftCell) {  
         this.current = null;  
       }  
     } else if (direction === 'right' && this.current && this.current.rightCell) {  
       this.current.rightCell = this.current.rightCell.right;  
       if (!this.current.rightCell) {  
         this.current = null;  
       }  
     }  
   },  
   newGame() {  
     this.board = [  
       [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1],  
       [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1],  
       [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1],  
       [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1],  
     ];  
     this.score = 0;  
     this.current = null;  
   },  
   slide() {  
     if (this.current) {  
       if (this.current.leftCell) {  
         this.move('left');  
       } else if (this.current.rightCell) {  
         this.move('right');  
       }  
     }  
   },  
 },  
};  
</script>
<style scoped>  
.game-container {  
 width: 100%;  
 max-width: 800px;  
 margin: 0 auto;  
 padding: 20px;  
 border: 1px solid #ccc;  
 border-radius: 5px;  
}
.grid {  
 display: flex;  
 flex-wrap: wrap;  
}
.cell {  
 width: 40px;  
 height: 40px;  
 background-color: #f2f2f2;  
 display: flex;  
 justify-content: center;  
 align-items: center;  
 border-radius: 5px;  
 margin: 10px;  
}
.cell:hover {  
 background-color: #ddd;  
}
.highlight {  
 background-color: #ffc107;  
}
.score {  
 margin-top: 20px;  
 font-size: 24px;  
 font-weight: bold;  
}
</style>

2、Java实现

import java.util.*;  
import java.util.concurrent.ThreadLocal;
public class 2048Game {  
   private static int BOARD_SIZE = 4;  
   private static int[][] board = new int[BOARD_SIZE][BOARD_SIZE];  
   private static int current = 0;  
   private static int score = 0;
   public static void main(String[] args) {  
       new ThreadLocal<2048Game>().set(new 2048Game());  
   }
   private 2048Game() {  
       reset();  
   }
   public void reset() {  
       board = new int[BOARD_SIZE][BOARD_SIZE];  
       generateBoard();  
       current = 0;  
       score = 0;  
   }
   private void generateBoard() {  
       for (int i = 0; i < board.length; i++) {  
           for (int j = 0; j < board[i].length; j++) {  
               board[i][j] = Math.floor(Math.random() * 4) + 1;  
           }  
       }  
   }
   public void slide(int direction) {  
       if (direction == 0 || direction == 1) {  
           for (int i = 0; i < board.length; i++) {  
               int[] temp = board[i];  
               int j = 0;  
               for (int k = 0; k < temp.length; k++) {  
                   if (temp[k]!= 0) {  
                       while (j < temp.length - 1 && temp[j + 1] == temp[k]) {  
                           temp[j] += temp[j + 1];  
                           j++;  
                       }  
                   }  
                   temp[j] = k;  
                   j++;  
               }  
               board[i] = temp;  
           }  
       } else if (direction == 2 || direction == 3) {  
           for (int i = 0; i < board.length; i++) {  
               int[] temp = board[i];  
               int k = 0;  
               for (int j = 0; j < temp.length; j++) {  
                   if (temp[j]!= 0) {  
                       while (k < temp.length - 1 && temp[k + 1] == temp[j]) {  
                           temp[k] += temp[k + 1];  
                           k++;  
                       }  
                   }  
                   temp[k] = j;  
                   k++;  
               }  
               board[i] = temp;  
           }  
       }  
   }
   public void printBoard() {  
       System.out.println("当前分数:" + score);  
       for (int i = 0; i < board.length; i++) {  
           for (int j = 0; j < board[i].length; j++) {  
               System.out.print(board[i][j] + " ");  
           }  
           System.out.println();  
       }  
   }
   public void checkWin() {  
       for (int i = 0; i < board.length; i++) {  
           for (int j = 0; j < board[i].length; j++) {  
               if (board[i][j] == 0) {  
                   return;  
               }  
               if (j < board[i].length - 1 && board[i][j] == board[i][j + 1]) {  
                   int sum = board[i][j] + board[i][j + 1];  
                   board[i][j] = 0;  
                   board[i][j + 1] = 0;  
                   score += sum;  
                   System.out.println("恭喜你赢得了 " + sum + " 分!");  
                   reset();  
               }  
           }  
       }  
   }  
}

运行效果:文章来源地址https://www.toymoban.com/news/detail-640962.html

当前分数:0

到了这里,关于分别用Vue和Java来实现的风靡一时的2048 游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 基于Java的2048小游戏的设计与实现,附源码+文档,适合课程设计,包远程安装调试运行

    1、项目介绍 本游戏采用Java语言编写,使用Eclipse编译器,jdk1.8编译环境。游戏的UI主要运用Java图形界面编程(AWT),实现窗口化可视化的界面。 游戏的后台通过监听键盘方向键来移动数字方块,运用随机数的思想随机产生一个2或4的随机数,显示在随机方块中,运用二维数组

    2024年02月03日
    浏览(46)
  • 2048小游戏 java版(代码+注释)

            一个纯纯小白,想写点什么,也想学习一下怎么在这里写东西,就简单的写个2048小游戏。写的不好,大佬就不用看了,希望和大家交流学习,有写的不好或有更好的建议也欢迎提出来。(需要用的可直接粘贴复制)(轻喷) 目录 游戏展示 讲解  代码        

    2024年02月09日
    浏览(30)
  • 用JAVA写一个2048的小游戏。

    如图所示: 使用软件:eclipse2021-12版 JDK版本:JDK15.0.1 链接:https://pan.baidu.com/s/1NVWaklg9K2wRmBOLew6iMQ 提取码:ts08 1. Client.java: 2.Constant.java 3.Block.java 4.BlockLoader.java 5.Drawable.java 6.Moveable.java 7.MyFrame.java 提示:代码写的有些臃肿,其中也有一些BUG,理解源码以后可以修正。 链接:

    2024年02月10日
    浏览(71)
  • python小游戏 2048小游戏设计与实现

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 2048小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 今天我们用python实现

    2024年02月11日
    浏览(44)
  • python快速实现2048小游戏

    《2048》是一款比较流行的数字游戏,最早于2014年3月20日发行。原版2048首先在GitHub上发布,原作者是Gabriele Cirulli,后被移植到各个平台。这款游戏是基于《1024》和《小3传奇》的玩法开发而成的新型数字游戏。 操作指南: 每次可以选择上下左右其中一个方向去滑动,每滑动

    2024年02月11日
    浏览(28)
  • python小游戏毕设 2048小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 2048小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: https://

    2024年02月12日
    浏览(31)
  • python项目分享 2048小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 2048小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: https://

    2024年01月18日
    浏览(40)
  • python项目分享 - 2048小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 2048小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: https://

    2024年01月18日
    浏览(42)
  • 手把手教你用Python实现2048小游戏

    感觉好久没有写小游戏玩了,今天恰巧有空.这次我来用Python做个2048小游戏吧.废话不多说,文中有非常详细的代码示例,需要的朋友可以参考下 目录 一、开发环境 二、环境搭建 三、原理介绍 四、效果图 Python版本:3.6.4 相关模块: pygame模块; 以及一些Python自带的模块。 安装

    2024年04月28日
    浏览(47)
  • 【C/C++小游戏】2048 大作战!(基于Easyx图形窗口实现)

    写在前面 游戏简介 Easyx 图形库 编写游戏 预编译代码 第一步:初始化棋盘 第二步:绘制棋盘 第三步:用户操作 第四步:封装函数 完整代码 效果展示 大家好! 本人是一个12岁六年级小学生,今年9月开始学习C++,曾经学过1年Python。 这是我的第一篇博客,决定分享一个游戏

    2024年02月10日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包