⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scopetris.c

📁 compiled for AtMega32 The source, to compile with AVR-G
💻 C
字号:
//
// Title        : scopetris - main 
// Author       : Lars Pontoppidan 
// Date         : Aug. 2007
// Version      : 0.1
// Target MCU   : AtMega32 at 8 MHz
//
// DESCRIPTION:
// ------------
// Vector display TETRIS with real gravity!
//
// Inputs:
//    PA0  Joy up
//    PA1  Joy down
//    PA2  Joy left
//    PA3  Joy right
//    PA4  Joy fire
//    PA5  Gravity on/off   <=>  real/naive
//
// Outputs:
//    PORTD  Vector X position (8-bit)
//    PORTC  Vector Y position (8-bit)
//
//
// CREDITS:
// scopetris was created by Lars Pontoppidan in August 2007.
//
// COMPILER INFO:
// This code compiles with avr-gcc v.4.1.2
//
// DISCLAIMER:
// The author is in no way responsible for any problems or damage caused by
// using this code. Use at your own risk.
//
// LICENSE:
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "drawlib.h"
#include "tetrislib.h"
#include "vectorfont.h"

// 
// Frame clock timing
//

void init_frame_clock(void)
{    
  // timer2 as frame clock
  TCCR2 = (1<<WGM21) | (1<<CS22) | (1<<CS21) | (1<<CS20);
  TCNT2 = 0;
  OCR2 = (0.01 * F_CPU / 1024);  // get 0.01 sec interval
}

inline void wait_frame(void)
{
  while((TIFR & (1<<OCF2)) == 0);
  TIFR = (1<<OCF2); // write logic one to clear OCF2
}


//
// Joystick input
//

#define JOY_UP      1
#define JOY_DOWN    2
#define JOY_LEFT    4
#define JOY_RIGHT   8
#define JOY_FIRE    16
#define JOY_GRAVITY 32

#define JOY_MASK  (JOY_UP|JOY_DOWN|JOY_LEFT|JOY_RIGHT|JOY_FIRE|JOY_GRAVITY)

unsigned char joy_state;

unsigned char joy_new_state;
unsigned char joy_debounce;

#define JOY_PORT PORTA
#define JOY_PIN  PINA
#define JOY_DDR  DDRA

#define JOY_DEBOUNCE 1


void joy_init(void)
{
  JOY_PORT |= JOY_MASK;
  JOY_DDR &= ~JOY_MASK;
  
  joy_state = 0;
  joy_new_state = 0;
  joy_debounce = 0;
}

// If a direction changes from off to on, it is returned
unsigned char joy_input(void)
{
  unsigned char ret=0;
  unsigned char i;
  
  i = (~JOY_PIN) & JOY_MASK;
  
  if (joy_new_state != i) {
    joy_new_state = i;
    joy_debounce = JOY_DEBOUNCE;
  }
  else {
    if (joy_debounce > 0) {
      joy_debounce--;
      if (joy_debounce==0) {
        ret = joy_new_state & ~joy_state; // return direction being set
        joy_state = joy_new_state;
      } 
    }
  }
  
  return ret;
}


void tetris_demo(void) 
{
  unsigned char i;
    
  i = joy_input();
    
  if (i&JOY_LEFT) {
    tetris_modify_brick(-1,0);
  }
  else if (i&JOY_RIGHT) {
    tetris_modify_brick(1,0);
  }
  else if (i&JOY_FIRE) {
    tetris_modify_brick(0,1);
  }
  
  tetris_mode = ((joy_state & JOY_DOWN) ? TETRIS_MODE_SPEEDUP : 0 ) |
                ((joy_state & JOY_GRAVITY) ? TETRIS_MODE_GRAVITY : 0 );
  
  tetris_handler();
  
  tetris_speed = tetris_stats_zapcount + 16;

}

int main(void)
{
  // init
  init_frame_clock();
  draw_init();
  tetris_init();
  joy_init();

  while(1) {
    wait_frame();
    tetris_demo();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -