- UID
- 373967
- 帖子
- 8779
- 主題
- 2609
- 精華
- 0
- 積分
- 993
- 楓幣
- 2637
- 威望
- 970
- 存款
- 31556
- 贊助金額
- 0
- 推廣
- 0
- GP
- 1205
- 閱讀權限
- 50
- 在線時間
- 451 小時
- 註冊時間
- 2023-1-12
- 最後登入
- 2024-11-5
|
STC90C52RC單片機可以用來製作各種有趣的遊戲,包括打磚塊遊戲。下面是一個簡單的打磚塊遊戲的程式碼示例,使用STC90C52RC單片機和一個16x2的LCD顯示屏,同時還需要一些按鈕、蜂鳴器和其他電子元件。
以下是示例程式碼:
#include <STC89.H>
#define LCD_RS P2_5
#define LCD_RW P2_6
#define LCD_E P2_7
#define LCD_DATA P0
#define BTN_LEFT P3_1
#define BTN_RIGHT P3_2
#define BEEPER P3_3
unsigned char const BARRIER[] = { 0x0F, 0x1F, 0x3E, 0x7C, 0x7C, 0x3E, 0x1F, 0x0F }; //磚塊位圖
unsigned char const BALL[] = { 0x04, 0x0E, 0x1F, 0x0E, 0x04 }; //球的位圖
unsigned char buf[32]; //顯示屏緩存區
unsigned char score = 0; //分數
unsigned char bar_x = 7; //磚塊橫向位置
unsigned char ball_x = 7; //球的橫向位置
unsigned char ball_y = 1; //球的縱向位置
signed char ball_dx = 1; //球的橫向速度
signed char ball_dy = 1; //球的縱向速度
void delay(unsigned int t)
{
unsigned int i, j;
for (i = 0; i < t; i++)
for (j = 0; j < 1000; j++);
}
void lcd_write_cmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_E = 0;
LCD_DATA = cmd;
LCD_E = 1;
delay(1);
LCD_E = 0;
delay(1);
}
void lcd_write_data(unsigned char dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_E = 0;
LCD_DATA = dat;
LCD_E = 1;
delay(1);
LCD_E = 0;
delay(1);
}
void lcd_init()
{
lcd_write_cmd(0x38);
lcd_write_cmd(0x08);
lcd_write_cmd(0x01);
lcd_write_cmd(0x06);
lcd_write_cmd(0x0C);
}
void lcd_clear()
{
lcd_write_cmd(0x01);
}
void lcd_puts(unsigned char x, unsigned char y, unsigned char *str)
{
unsigned char i;
if (y == 0)
lcd_write_cmd(0x80 + x);
else
lcd_write_cmd(0xC0 + x);
for (i = 0; str[i]; i++)
lcd_write_data(str[i]);
}
void draw_barrier()
{
unsigned char i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
if (BARRIER[i] & (1 << j))
buf[i * 2 + 1] |= (1 << j);
else
buf[i * 2 + 1] &= ~(1 << j);
}
}
}
void draw_ball()
{
unsigned char i;
for (i = 0; i < 5; i++)
buf[(ball_y + i) * 2 + ball_x / 8] |= BALL[i] << (ball_x % 8);
for (i = 0; i < 5; i++)
buf[(ball_y + i) * 2 + ball_x / 8 + 1] |= BALL[i] >> (8 - ball_x % 8);
}
void draw_screen()
{
unsigned char i;
lcd_clear();
for (i = 0; i < 16; i++) {
lcd_write_data(buf[i * 2]);
lcd_write_data(buf[i * 2 + 1]);
}
lcd_puts(0, 0, "SCORE:");
lcd_puts(6, 0, (unsigned char *)score + '0');
}
void beep()
{
unsigned char i;
scss
Copy code
for (i = 0; i < 20; i++) {
BEEPER = 1;
delay(1);
BEEPER = 0;
delay(1);
}
}
void main()
{
unsigned char i;
lcd_init();
for (i = 0; i < 32; i++)
buf[i] = 0;
draw_barrier();
draw_ball();
draw_screen();
while (1) {
if (BTN_LEFT == 0 && bar_x > 0) {
bar_x--;
draw_barrier();
draw_screen();
}
if (BTN_RIGHT == 0 && bar_x < 14) {
bar_x++;
draw_barrier();
draw_screen();
}
buf[ball_y * 2 + ball_x / 8] &= ~(BALL[0] << (ball_x % 8));
buf[ball_y * 2 + ball_x / 8 + 1] &= ~(BALL[0] >> (8 - ball_x % 8));
ball_x += ball_dx;
ball_y += ball_dy;
if (ball_x == 0 || ball_x == 15)
ball_dx = -ball_dx;
if (ball_y == 0)
ball_dy = 1;
if (ball_y == 7) {
if (ball_x >= bar_x && ball_x <= bar_x + 2) {
ball_dy = -1;
score++;
beep();
} else {
lcd_puts(5, 1, "GAME OVER");
while (1);
}
}
draw_ball();
draw_screen();
delay(10);
}
}
這個程式碼使用了16x2的LCD顯示屏來顯示分數和遊戲畫面,同時還使用了幾個按鈕和一個蜂鳴器。在遊戲開始時,磚塊和球的初始位置會被設置,並且畫面會被繪製出來。 |
|