HNU小学期工训-千本樱电子音乐与小游戏

前几个学期一直在搬运csdn上各个大佬的内容,今年想着造福一下学弟学妹们,做的挺烂的,但是能小玩一下或者要是能帮到大家一丢丢也是很好的,希望大家把hnu开源的精神继续发扬光大哈哈哈

下面这个是自己手搓的千本樱,然后如果你懂一丢丢的乐理知识,就可以模仿我写的并且复刻出自己想听的歌曲,记得当时小学期的时候有什么孤勇者什么的哈哈哈

(具体运行方式已经记不太得了,依稀记得是编译一下生成一个hex文件然后在stc-isp软件里面把文件下载到板子上,播放的效果其实还不错,很有小时候游戏机的味道 )

/**********************
myMusic ????
??:IAP15F2K60S2 ??:12MHz
************************/
#include <STC15F2K60S2.h>
#define uint unsigned int
#define uchar unsigned char

/*---------??????---------*/
sbit sbtBeep = P3 ^ 4;   // ???
sbit key1 = P3 ^ 2;      // ??1
sbit sbtLedSel = P2 ^ 3; // ???? LED ?????

/*---------????---------*/
uchar code arrLed[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; // LED ?
uchar ucTimerH, ucTimerL;                                                     // ?????????
bit flag;                                                                     // ???,?????
uchar code arrMusic[] =                                                       // ????,???: ??, ??
    {
        // ?????????????,1?????,2?????,3?????
        // ?????????,??0x15??????S0,0x21??????DO?
        // ????????,??:0x10????,0x20????,0x08??1/2?
        0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x24, 0x08, 0x24, 0x08, 0x25, 0x10, 0x22, 0x0c, 0x22, 0x04, 0x22, 0x08,
        0x21, 0x08, 0x22, 0x18, 0x21, 0x08, 0x16, 0x08, 0x21, 0x08, 
				0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x24, 0x08, 0x24, 0x08, 0x25, 0x08, 0x26, 0x10, 0x25, 0x08, 0x26, 0x04, 0x25, 0x04,
        0x24, 0x10, 0x22, 0x10,
				0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x24, 0x08, 0x24, 0x08, 0x25, 0x08,
        0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x21, 0x08, 0x16, 0x08, 0x21, 0x08,
			
				0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x24, 0x08, 0x24, 0x08, 0x25, 0x08,
				0x26, 0x10,0x25, 0x08, 0x26, 0x04, 0x25, 0x04, 0x24, 0x10, 0x22, 0x10, 
			
				//5
				0x24, 0x10, 0x23, 0x10, 0x22, 0x10, 0x21, 0x10, 0x21, 0x08, 0x21, 0x04, 0x22, 0x04, 0x16, 0x08,
				0x15, 0x08,0x16, 0x20, 0x16, 0x08, 0x21, 0x08, 0x22, 0x10, 0x25, 0x10,0x23, 0x10,
				
				//6
				0x24, 0x0c, 0x24, 0x04, 0x23, 0x08, 0x21, 0x08, 0x22, 0x20, 0x24, 0x10, 0x23, 0x10, 0x22, 0x10,
				0x21, 0x10,0x21, 0x08, 0x21, 0x04, 0x22, 0x04, 0x16, 0x08, 0x15, 0x08,0x16,0x10, 0x16, 0x08,0x21,0x08,
			
				//7
					0x22, 0x08, 0x22, 0x10, 0x22, 0x08, 0x24, 0x10, 0x25, 0x10, 0x23, 0x30, 0x22, 0x08, 0x24, 0x08,
					
					0x25, 0x0c,0x25, 0x0c, 0x26, 0x08, 0x26, 0x18, 0x16, 0x08,
					
				//8
					0x31, 0x08, 0x32, 0x08, 0x25, 0x08, 0x24, 0x08, 0x26, 0x10, 0x22, 0x08, 0x24, 0x08, 0x25, 0x0c,
					0x25, 0x0c,0x26, 0x08, 0x26, 0x18, 0x26, 0x08, 0x26, 0x08, 0x26, 0x08,0x25,0x08, 0x24, 0x08,0x24,0x10,0x22, 0x08,0x24,0x08,
					
				//9
					0x25, 0x0c, 0x25, 0x0c,0x26, 0x08, 0x26, 0x18, 0x26, 0x08,0x31, 0x08, 0x32, 0x08, 0x25, 0x08, 0x24, 0x08, 0x26, 0x10, 0x22, 0x30, 0x24, 0x08, 0x26, 0x10,
					0x26, 0x10,0x25, 0x10, 0x24,0x10,
					
				//10
					0x25, 0x08, 0x26, 0x08,0x23, 0x08, 0x21, 0x08, 0x22, 0x10,0x22, 0x08, 0x24, 0x08,
					
					0x25, 0x0c,0x25, 0x0c, 0x26, 0x08, 0x26, 0x18, 0x16, 0x08,
					
				//8
					0x31, 0x08, 0x32, 0x08, 0x25, 0x08, 0x24, 0x08, 0x26, 0x10, 0x22, 0x08, 0x24, 0x08, 0x25, 0x0c,
					0x25, 0x0c,0x26, 0x08, 0x26, 0x18, 0x26, 0x08, 0x26, 0x08, 0x26, 0x08,0x25,0x08, 0x24, 0x08,0x24,0x10,0x22, 0x08,0x24,0x08,
					
				//9
					0x25, 0x0c, 0x25, 0x0c,0x26, 0x08, 0x26, 0x18, 0x26, 0x08,0x31, 0x08, 0x32, 0x08, 0x25, 0x08, 0x24, 0x08, 0x26, 0x10, 0x22, 0x30, 0x24, 0x08, 0x26, 0x10,
					0x26, 0x10,0x25, 0x10, 0x24,0x10,
					
					0x25, 0x08, 0x24, 0x08,0x26, 0x08, 0x31, 0x08, 0x32, 0x30,
					
					
			/*0x32, 0x18, 0x31, 0x08, 0x27, 0x20, 0x27, 0x08, 0x20, 0x08,
        0x22, 0x08, 0x22, 0x10, 0x22, 0x10, 0x21, 0x10, 0x22, 0x10, 0x24, 0x20, 0x24, 0x08, 0x25, 0x08,
        */
			
			
			
			
			/*0x26, 0x08, 0x27, 0x08, 0x31, 0x08, 0x27, 0x10, 0x32, 0x10, 0x31, 0x10, 0x26, 0x10, 0x25, 0x20, 0x25, 0x08, 0x20, 0x08,
        0x33, 0x08, 0x32, 0x08, 0x31, 0x20, 0x25, 0x18, 0x23, 0x08, 0x26, 0x20, 0x24, 0x08, 0x20, 0x08,
        0x32, 0x0c, 0x31, 0x04, 0x27, 0x20, 0x26, 0x10, 0x25, 0x10, 0x25, 0x20, 0x25, 0x08, 0x20, 0x08,
        0x25, 0x10, 0x33, 0x20, 0x32, 0x10, 0x25, 0x10, 0x31, 0x20, 0x27, 0x18,
        0x27, 0x08, 0x26, 0x18, 0x25, 0x08, 0x26, 0x10, 0x32, 0x10, 0x32, 0x20, 0x32, 0x08, 0x30, 0x08,
        0x33, 0x0c, 0x32, 0x04, 0x31, 0x20, 0x25, 0x18, 0x23, 0x08, 0x26, 0x20, 0x24, 0x08, 0x20, 0x08,
        0x32, 0x0c, 0x31, 0x04, 0x27, 0x20, 0x26, 0x10, 0x25, 0x10, 0x33, 0x30,
        0x33, 0x10, 0x35, 0x20, 0x34, 0x10, 0x33, 0x10, 0x32, 0x18, 0x33, 0x08, 0x34, 0x10, 0x30, 0x08,
        0x34, 0x08, 0x33, 0x18, 0x33, 0x08, 0x32, 0x18, 0x32, 0x08, 0x31, 0x30,*/
        0x00, 0x00};
uchar code arrMusicToTimerNum[] =
    {
        // ???????????????????,??????,??????
        0xf8, 0x8c, // ???,?1
        0xf9, 0x5b,
        0xfa, 0x15, // ?3
        0xfa, 0x67,
        0xfb, 0x04, // ?5
        0xfb, 0x90,
        0xfc, 0x0c, // ?7
        0xfc, 0x44, // ?? C ?
        0xfc, 0xac, // ?2
        0xfd, 0x09,
        0xfd, 0x34, // ?4
        0xfd, 0x82,
        0xfd, 0xc8, // ?6
        0xfe, 0x06,
        0xfe, 0x22, // ???,?1
        0xfe, 0x56,
        0xfe, 0x85, // ?3
        0xfe, 0x9a,
        0xfe, 0xc1, // ?5
        0xfe, 0xe4,
        0xff, 0x03, // ?7
        0xff, 0xff};

/*---------?????---------*/
void DelayMs(uint xms)
{
    uint i, j;
    for (i = xms; i > 0; i--)
        for (j = 124; j > 0; j--)
            ;
}

/*---------?????---------*/
// ?? tem ??? arrMusicToTimerNum ???????
uchar GetPosition(uchar tem)
{
    uchar ucBase, ucOffset, ucPosition; // ??????????
    ucBase = tem / 16;                  // ?4?????,??
    ucOffset = tem % 16;                // ?4????,???
    if (ucOffset == 0)                  // ????0?,???42
    {
        P0 = 0x00;
        return 42;
    }
    // ??????? LED
    P0 = arrLed[ucOffset];
    if (ucBase == 1) // ?????1?,?????,???0
        ucBase = 0;
    else if (ucBase == 2) // ?????2?,?????,???14
        ucBase = 14;
    else if (ucBase == 3) // ?????3?,?????,???28
        ucBase = 28;
    // ?????????,???????? arrMusicToTimerNum ??????
    ucPosition = ucBase + (ucOffset - 1) * 2;
    return ucPosition; // ????????
}

/*---------????????---------*/
void PlayMusic()
{
    // ucNoteTmp ???,ucRhythmTmp ???
    uchar ucNoteTmp, ucRhythmTmp, tem;
    // ??? uchar ??? uint ?,?? i ???255,????128???,?????�???�???
    uint i = 0;
    while (1)
    {
        if (flag == 1) // ????
        {
            ucNoteTmp = arrMusic[i];
            if (ucNoteTmp == 0x00) // ???????,??1?,????????
            {
                i = 0;
                P0 = 0x00;
                sbtBeep = 0;
                DelayMs(1000);
            }
            else // ???????????
            {
                tem = GetPosition(arrMusic[i]);     // ??????? arrMusicToTimerNum ???????
                ucTimerH = arrMusicToTimerNum[tem]; // ??????????????? ucTimerH ? ucTimerL
                ucTimerL = arrMusicToTimerNum[tem + 1];
                i++;

                TH0 = ucTimerH; // ? ucTimerH ? ucTimerL ?????
                TL0 = ucTimerL;
                ucRhythmTmp = arrMusic[i]; // ????
                i++;

                TR0 = 1;                  // ????1
                if (ucTimerH == ucTimerL) // ?????????
                    TR0 = 0;
                DelayMs(ucRhythmTmp * 170); // ??????, ?? P3^4 ?????
                TR0 = 0;                    // ????1
                sbtBeep = 0;
            }
        }
        else
            while (flag != 1) // ??
                ;
    }
}

/*---------??????---------*/
// ????? IO ?
void InitSys()
{
    P0M0 = 0xff;
    P0M1 = 0x00;
    P2M0 = 0x08;
    P2M1 = 0x00;
    P3M0 = 0x10;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;
    sbtLedSel = 1;
}

/*---------???0??????---------*/
void InitT0()
{
    TMOD = 0x01;
    TH0 = 0xD8;
    TL0 = 0xEF;
    IE = 0x87; // 1000 0111  EA=1,EX0=1,ET0=1,EX1=0;
    IP = 0x02; // 0000 0010  PT0=1;   ???0????
    TR0 = 0;
    sbtBeep = 0;
}

/*---------???---------*/
void main()
{
    InitSys();
    InitT0();
    P0 = 0x00;
    key1 = 1;
    flag = 0;
    PlayMusic();
}

/*---------???0??????---------*/
// ????,?? sbtBeep ???,????
void T0_Process() interrupt 1 // ???????
{
    TH0 = ucTimerH;
    TL0 = ucTimerL;
    sbtBeep = ~sbtBeep;
}

/*---------??1??????---------*/
// ?????????
void ex1() interrupt 0
{
    DelayMs(5);
    if (key1 == 0) // ?? key1 ????
    {
        DelayMs(5);
        if (key1 == 0)
        {
            while (!key1)
                ;
            flag = ~flag; // ???????,??????
        }
    }
}

下面这个是我自己做的大作业,借鉴了一些大佬做的东西,不过整个游戏还是自己手写的,模仿的是节奏大师,能玩但是不是很好玩哈哈哈(大致规则就是在那个横杠下落到底部的时候按一下会得到一分,累计到多少分到下一关什么的,然后速度会越来越快)

#include "STC15F2K60S2.H"        //必须。
#include "sys.H"                 //必须。
#include "displayer.H" 
#include <stdlib.h>
#include "key.H"
#include "vib.h"
#include <intrins.h>
#include "beep.H"
#include "music.H"

code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00, 0x01,0x40,0x41,0x08, 0x48,0x6e,0x3f, 0x3e,0x73,0x77,0x6d,0x49,0x48,0x22,0x14,
	              /* 序号:   0   -    ——	   二  _	  三    er	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
unsigned int b1=9,b2=0,b3=22,k1=1,k2=1,k3=1,k4=1,sum=0,cnt=0,guan=9,win=0;
code unsigned char song[]={0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x24, 0x08, 0x24, 0x08, 0x25, 0x10, 0x22, 0x0c, 0x22, 0x04, 0x22, 0x08,
        0x21, 0x08, 0x22, 0x18, 0x21, 0x08, 0x16, 0x08, 0x21, 0x08, 
				0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x24, 0x08, 0x24, 0x08, 0x25, 0x08, 0x26, 0x10, 0x25, 0x08, 0x26, 0x04, 0x25, 0x04,
        0x24, 0x10, 0x22, 0x10,
				0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x24, 0x08, 0x24, 0x08, 0x25, 0x08,
        0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x21, 0x08, 0x16, 0x08, 0x21, 0x08,
			
				0x22, 0x0c, 0x22, 0x04, 0x22, 0x08, 0x21, 0x08, 0x22, 0x08, 0x24, 0x08, 0x24, 0x08, 0x25, 0x08,
				0x26, 0x10,0x25, 0x08, 0x26, 0x04, 0x25, 0x04, 0x24, 0x10, 0x22, 0x10, 
			
				//5
				0x24, 0x10, 0x23, 0x10, 0x22, 0x10, 0x21, 0x10, 0x21, 0x08, 0x21, 0x04, 0x22, 0x04, 0x16, 0x08,
				0x15, 0x08,0x16, 0x20, 0x16, 0x08, 0x21, 0x08, 0x22, 0x10, 0x25, 0x10,0x23, 0x10,
				
				//6
				0x24, 0x0c, 0x24, 0x04, 0x23, 0x08, 0x21, 0x08, 0x22, 0x20, 0x24, 0x10, 0x23, 0x10, 0x22, 0x10,
				0x21, 0x10,0x21, 0x08, 0x21, 0x04, 0x22, 0x04, 0x16, 0x08, 0x15, 0x08,0x16,0x10, 0x16, 0x08,0x21,0x08,
			
				//7
					0x22, 0x08, 0x22, 0x10, 0x22, 0x08, 0x24, 0x10, 0x25, 0x10, 0x23, 0x30, 0x22, 0x08, 0x24, 0x08,
					
					0x25, 0x0c,0x25, 0x0c, 0x26, 0x08, 0x26, 0x18, 0x16, 0x08,
					
				//8
					0x31, 0x08, 0x32, 0x08, 0x25, 0x08, 0x24, 0x08, 0x26, 0x10, 0x22, 0x08, 0x24, 0x08, 0x25, 0x0c,
					0x25, 0x0c,0x26, 0x08, 0x26, 0x18, 0x26, 0x08, 0x26, 0x08, 0x26, 0x08,0x25,0x08, 0x24, 0x08,0x24,0x10,0x22, 0x08,0x24,0x08,
					
				//9
					0x25, 0x0c, 0x25, 0x0c,0x26, 0x08, 0x26, 0x18, 0x26, 0x08,0x31, 0x08, 0x32, 0x08, 0x25, 0x08, 0x24, 0x08, 0x26, 0x10, 0x22, 0x30, 0x24, 0x08, 0x26, 0x10,
					0x26, 0x10,0x25, 0x10, 0x24,0x10,
					
				//10
					0x25, 0x08, 0x26, 0x08,0x23, 0x08, 0x21, 0x08, 0x22, 0x10,0x22, 0x08, 0x24, 0x08,
					
					0x25, 0x0c,0x25, 0x0c, 0x26, 0x08, 0x26, 0x18, 0x16, 0x08,
					
				//8
					0x31, 0x08, 0x32, 0x08, 0x25, 0x08, 0x24, 0x08, 0x26, 0x10, 0x22, 0x08, 0x24, 0x08, 0x25, 0x0c,
					0x25, 0x0c,0x26, 0x08, 0x26, 0x18, 0x26, 0x08, 0x26, 0x08, 0x26, 0x08,0x25,0x08, 0x24, 0x08,0x24,0x10,0x22, 0x08,0x24,0x08,
					
				//9
					0x25, 0x0c, 0x25, 0x0c,0x26, 0x08, 0x26, 0x18, 0x26, 0x08,0x31, 0x08, 0x32, 0x08, 0x25, 0x08, 0x24, 0x08, 0x26, 0x10, 0x22, 0x30, 0x24, 0x08, 0x26, 0x10,
					0x26, 0x10,0x25, 0x10, 0x24,0x10,
					
					0x25, 0x08, 0x24, 0x08,0x26, 0x08, 0x31, 0x08, 0x32, 0x30,0x00,0x00};
unsigned char c=0x81;
code char a[]={10,11,12,14,10,11,13,23,14,10,11,13,22,23,14,10,24,25,10,11,12,14,10,11,12,14,10,11,12,14,10,11,13,22,22,23,14,10,11,12,14};
void my100mS_callback()
{ 
	

	if(win==0)
{	if(cnt==guan)
	{
		
		if( c == 0x18) c=0x24;
		else if(c==0x24) c=0x42;
		else if(c==0x42) c=0x81;
		else c=0x18;
		LedPrint(c);
		if( a[b1%41]==14||a[b1%41]==23||a[b1%41]==22)
	{
	k1=0;
	}
	else
	k1=1;
	
	if(a[b2%41]==14||a[b2%41]==23||a[b2%41]==22)
	{
	k2=0;
	}
	else
	k2=1;
	
	if(a[b3%41]==14||a[b3%41]==23||a[b3%41]==22)
	{
	k3=0;
	}
	else
	k3=1;
	if(a[(b1%41)]==25||a[(b2%41)]==25||a[(b3%41)]==25)
	{
		k4=0;
	}
	else
	k4=1;
	
  Seg7Print(sum/100,(sum/10)%10,sum%10,10,guan,a[(b3++)%41],a[(b2++)%41],a[(b1++)%41]);	
	if(sum>=30)
	{
		if(guan>1)
		{guan--;
		sum=0;}
		else
		{
		 win=1;
		}
	}
	cnt=0;
}
	else
	{
	cnt++;
	}
}
else
	{
			Seg7Print(16,17,18,10,19,20,21,21);	
			SetMusic(140,0xFC,&song,sizeof(song),enumMscDrvLed);
			SetPlayerMode(enumModePlay);
	}
	
}

void myvib1_callback()
{ 
	 
   if( GetVibAct() == enumVibQuake)
	 { 
		 if(k4==0)
	   {sum=sum+10;
		 SetBeep(11000, 50);
			k4=1;
			}
	
	 }
}
void myKey_callback()
{ 
	
	if( GetKeyAct(enumKey1) == enumKeyPress )
	{
	
		if(k1==0)
		{sum++;
		SetBeep(5000, 10);
		k1=1;
		}
		else
		{
		if(sum!=0)			
		sum--;
		}
	}

		if( GetKeyAct(enumKey2) == enumKeyPress )
		{
		if(k2==0)
		{sum++;
		SetBeep(7000, 10);
			k2=1;
		}
		else
		{
		if(sum!=0)			
		sum--;
		}
		}
	
		if( GetKeyAct(enumKey3) == enumKeyPress )
	{
		if(k3==0)
		{sum++;
		SetBeep(9000, 10);
			k3=1;
		}
		else
		{
			if(sum!=0)
			sum--;}
	}
		

}

void main() 
{ 
	KeyInit();  
	BeepInit();
	VibInit();
	MusicPlayerInit();
	DisplayerInit();  
	SetDisplayerArea(0,7);
	Seg7Print(10,10,10,10,10,10,10,10);		
  LedPrint(0);
	SetEventCallBack(enumEventKey, myKey_callback);
	SetEventCallBack(enumEventVib,myvib1_callback);
	SetEventCallBack(enumEventSys100mS, my100mS_callback);
  MySTC_Init();	    
	while(1)             	
		{ MySTC_OS();    
		}	             
}                 

希望大家可以顺利的运行起来,可以借鉴但请有自己的想法,也希望大家小学期愉快!暑假愉快!!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值