极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13518|回复: 1

[求救]自製的arduino亮燈遊戲,玩到一半當機

[复制链接]
发表于 2015-11-17 23:52:08 | 显示全部楼层 |阅读模式
本帖最后由 迷你强 于 2015-11-18 22:32 编辑

我們遊戲是控制紅燈移動,然後可以放水球放完後,過一段時間後會在十字方像爆炸,如果周圍有水球會連環一起爆炸。
可是像影片一,玩到一半遊戲突然就會當掉,變成全黑,或是亮很奇怪的燈(像最後)。影片2我在loop裡面不斷serial.out.println(22),當遊戲當掉時卻就停掉了。想請教大家可能是什麼原因。

影片下載網址:https://drive.google.com/file/d/ ... nc/view?usp=sharing

原始碼下載網址:https://drive.google.com/file/d/ ... lU/view?usp=sharing

我後來發現可能跟我寫的ArrayList有關,原始碼很多地方我都用自己寫的ArrayList來放物件,不知道有沒有寫錯
想請大家幫忙看一下。謝謝大家

  1. #ifndef _ARRAYLIST_H
  2. #define _ARRAYLIST_H

  3. template <class MT>
  4. class ArrayList{
  5. private:
  6.         int size;

  7.         MT *array;
  8. public:
  9.         ArrayList();
  10.         void addItem(MT item);//加入物件
  11.         void removeItem(int index);//移除物件
  12.         MT getItem(int index);//取得arraylist中指定index的物件
  13.         void reset();//將arraylist清空
  14.         int getSize();//取得arraylist裡面的物件個數
  15.         void modifyItem(int index, MT item);//將arraylist中指定index的物件用新的物件取代
  16. };

  17. template <class MT>
  18. ArrayList<MT>::ArrayList(){
  19.         this->size = 0;
  20.         this->array = new MT[10];
  21. }
  22. template <class MT>
  23. void ArrayList<MT>::addItem(MT item) {
  24.         this->array[size] = item;
  25.         size++;
  26. }
  27. template <class MT>
  28. void ArrayList<MT>::removeItem(int index) {
  29.         MT *temporary = new MT[10];

  30.         for (int i = 0; i < index; i++){
  31.                 temporary[i] = this->array[i];
  32.         }
  33.         for (int i = index; i < size - 1; i++){
  34.                 temporary[i] = this->array[i + 1];
  35.         }
  36.         this->array = temporary;
  37.         size--;
  38. }
  39. template <class MT>
  40. MT ArrayList<MT>::getItem(int index){
  41.        

  42.         return  this->array[index];
  43. }
  44. template <class MT>
  45. void        ArrayList<MT>::reset(){
  46.         this->array = new MT[10];
  47. }
  48. template <class MT>
  49. int ArrayList<MT>::getSize(){

  50.         return this->size;
  51. }
  52. template <class MT>
  53. void ArrayList<MT>::modifyItem(int index, MT item){
  54.         array[index] = item;
  55. }

  56. #endif
复制代码
回复

使用道具 举报

发表于 2015-11-18 02:26:40 | 显示全部楼层
本帖最后由 Super169 于 2015-11-18 02:33 编辑

additem 不會多於 10 次嗎?  
reset 的時候, 也沒重設 size.  
size >= 10 時 會怎樣?

這不是真正的 array list, 只是一個有 10 個位 的 array 吧.  

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 17:35 , Processed in 0.053319 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表