C语言小游戏--打砖块(完整代码)

C语言小游戏-打砖块

本文基于作者博客的详解过程,编写出完整代码

前言

本文基于C语言编写出的相对简单的小游戏,实现的函数都是相对比较简单的,只是其中代码实现中关于光标消失以及清理屏幕运用了一些可能不太常用的知识,希望这篇文章对读者的C语言学习带来些许兴趣。
关于本文代码的详解请关注博主文章

代码实现

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>

//全局变量
int high, width;			//游戏画面大小
int ball_x, ball_y;			//小球的坐标
int ball_vx, ball_vy;		//小球的速度
int position_x, position_y;	//挡板的中心坐标
int ridus;					//挡板半径大小
int left, right;			//挡板的左右位置
int ball_number;			//反弹小球次数
int block_x, block_y;		//砖块的位置
int score;					//消掉的砖块个数



//光标移到(X,Y)位置
void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

//隐藏光标
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };  //第二个值为0,表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

//数据初始化
void startup()
{
	high = 15;
	width = 20;
	ball_x = 0;
	ball_y = width / 2;
	ball_vx = 1;
	ball_vy = 1;
	ridus = 5;
	position_x = high;
	position_y = width / 2;
	left = position_y - ridus;
	right = position_y + ridus;
	ball_number = 0;
	block_x = 0;
	block_y = width / 2;
	score = -1;//初始状态下会导致score为1,因为砖块与小球的初始位置相同
}

//显示画面
void show()
{
	gotoxy(0, 0);
	int i, j;
	for (i = 0; i <= high+1; i++)
	{
		for (j = 0; j <= width; j++)
		{
			if (i == ball_x && j == ball_y)
				printf("O");		//输出小球
			else if (j == width)
				printf("|");		//输出右边框
			else if (i == high + 1)
				printf("_");		//输出下边框
			else if (i == high && j >= left && j <= right)
				printf("*");		//输出挡板
			else if (i == block_x && j == block_y)
				printf("B");
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("反弹小球数:%d\n", ball_number);
	printf("消掉的砖块数:%d\n",score);
}

//与用户输入无关的更新
void updateWithoutInput()
{
	if (ball_x == high - 1)			
	{
		if (ball_y >= left && ball_y <= right)	//没有被挡板挡住
		{
			ball_number++;			//次数加1
			printf("\a");			//响铃
			ball_y = ball_y + rand() % 4 - 2;
		}
		else
		{
			printf("游戏失败\n");
			system("pause");
			exit(0);
		}
	}
	if (ball_x == block_x && ball_y == block_y)
	{
		score++;
		block_y = rand() % width;		//产生新的砖块
	}
	ball_x += ball_vx;
	ball_y += ball_vy;

	if (ball_x == 0 || ball_x == high - 1)
		ball_vx = -ball_vx;
	if (ball_y == 0 || ball_y == width - 1)
		ball_vy = -ball_vy;
	Sleep(80);
}

//与用户输入有关的更新
void updateWithInput()
{
	char input;
	if (_kbhit())		//判断是否有输入
	{
		input = _getch();
		if (input == 'a'&&left>1)
		{
			position_y--;			//位置左移
			left = position_y - ridus;
			right = position_y + ridus;
		}
		if (input == 'd'&&right<width-1)			//位置右移
		{
			position_y++;
			left = position_y - ridus;
			right = position_y + ridus;
		}
		
	}

}

int main()
{
	HideCursor();
	startup();
	while (1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java打砖块游戏是一个经典的小游戏,下面是一个简单的Java打砖块游戏的代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BrickBreaker extends JFrame implements ActionListener { private Timer timer; private boolean play = false; private int score = 0; private int paddleX = 310; private int ballPosX = 120; private int ballPosY = 350; private int ballXDir = -1; private int ballYDir = -2; private int[][] bricks; private int brickRows = 3; private int brickCols = 7; public BrickBreaker() { bricks = new int[brickRows][brickCols]; for (int i = 0; i < brickRows; i++) { for (int j = 0; j < brickCols; j++) { bricks[i][j] = 1; } } setTitle("Brick Breaker"); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(800, 600); setLocationRelativeTo(null); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_RIGHT) { if (paddleX >= 600) { paddleX = 600; } else { moveRight(); } } if (e.getKeyCode() == KeyEvent.VK_LEFT) { if (paddleX <= 10) { paddleX = 10; } else { moveLeft(); } } if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (!play) { play = true; ballPosX = 120; ballPosY = 350; ballXDir = -1; ballYDir = -2; paddleX = 310; score = 0; bricks = new int[brickRows][brickCols]; for (int i = 0; i < brickRows; i++) { for (int j = 0; j < brickCols; j++) { bricks[i][j] = 1; } } repaint(); } } } }); timer = new Timer(8, this); timer.start(); setVisible(true); } public void paint(Graphics g) { // 绘制背景 g.setColor(Color.black); g.fillRect(1, 1, 792, 592); // 绘制砖块 for (int i = 0; i < brickRows; i++) { for (int j = 0; j < brickCols; j++) { if (bricks[i][j] == 1) { g.setColor(Color.white); g.fillRect(j * 110 + 30, i * 50 + 50, 100, 40); } } } // 绘制球拍 g.setColor(Color.green); g.fillRect(paddleX, 550, 100, 8); // 绘制球 g.setColor(Color.yellow); g.fillOval(ballPosX, ballPosY, 20, 20); // 绘制得分 g.setColor(Color.white); g.setFont(new Font("serif", Font.BOLD, 25)); g.drawString("" + score, 700, 30); if (ballPosY > 570) { play = false; ballXDir = 0; ballYDir = 0; g.setColor(Color.red); g.setFont(new Font("serif", Font.BOLD, 30)); g.drawString("Game Over, Score: " + score, 190, 300); g.setFont(new Font("serif", Font.BOLD, 20)); g.drawString("Press Enter to Restart", 290, 350); } g.dispose(); } public void moveRight() { play = true; paddleX += 20; } public void moveLeft() { play = true; paddleX -= 20; } public void actionPerformed(ActionEvent e) { timer.start(); if (play) { if (new Rectangle(ballPosX, ballPosY, 20, 20).intersects(new Rectangle(paddleX, 550, 100, 8))) { ballYDir = -ballYDir; } A: for (int i = 0; i < brickRows; i++) { for (int j = 0; j < brickCols; j++) { if (bricks[i][j] == 1) { int brickX = j * 110 + 30; int brickY = i * 50 + 50; int brickWidth = 100; int brickHeight = 40; Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight); Rectangle ballRect = new Rectangle(ballPosX, ballPosY, 20, 20); Rectangle brickRect = rect; if (ballRect.intersects(brickRect)) { bricks[i][j] = 0; score += 5; if (ballPosX + 19 <= brickRect.x || ballPosX + 1 >= brickRect.x + brickRect.width) { ballXDir = -ballXDir; } else { ballYDir = -ballYDir; } break A; } } } } ballPosX += ballXDir; ballPosY += ballYDir; if (ballPosX < 0) { ballXDir = -ballXDir; } if (ballPosY < 0) { ballYDir = -ballYDir; } if (ballPosX > 770) { ballXDir = -ballXDir; } } repaint(); } public static void main(String[] args) { new BrickBreaker(); } } ``` 这段代码实现了一个简单的打砖块游戏,包括砖块、球拍、球和得分的绘制,以及键盘事件的处理。你可以在Java环境中运行该代码,通过方向键控制球拍的移动,按下回车键开始游戏。当球碰到砖块时,砖块消失并增加得分。当球触底时,游戏结束并显示最终得分。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值