c语言弹球小游戏

#include<stdio.h>
#include<easyx.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	// 初始化图形窗口,大小为 800 x 600 像素
	initgraph(800, 600);
	// 设置原点为窗口中心,x 轴正方向为右,y 轴正方向为上
	setorigin(400, 300);
	setaspectratio(1, -1);
	// 设置背景颜色为浅蓝色
	setbkcolor(RGB(164, 225, 202));
	// 清空窗口
	cleardevice();

	// 定义球的坐标,初始值为 0
	int x, y;
	x = 0;
	y = 0;
	// 定义球的速度,初始值为 5
	int vx = 5, vy = 5;
	// 定义球的半径,初始值为 40
	int r = 40;
	// 定义挡板的左上右下坐标,初始值为 -150, -280, 150, -300
	int barleft, bartop, barright, barbottom;
	barleft = -150;
	barright = 150;
	bartop = -280;
	barbottom = -300;


	// 用一个无限循环来更新画面
	while (1)
	{
		// 清空窗口
		cleardevice();
		// 用实心圆绘制球,颜色为默认的黑色
		solidcircle(x, y, r);

		// 用实心矩形绘制挡板,颜色为默认的黑色
		solidrectangle(barleft, bartop, barright, barbottom);

		// 暂停 40 毫秒,控制画面的刷新频率
		Sleep(40);

		// 判断球是否碰到窗口的上边界,如果是,就反转 y 方向的速度
		if (y >= 300 - r)
		{
			vy = -vy;
		}
		// 判断球是否碰到窗口的左右边界,如果是,就反转 x 方向的速度
		if (x <= -400 + r || x >= 400 - r)
		{
			vx = -vx;
		}
		// 判断球是否碰到挡板的上边,如果是,就反转 y 方向的速度
		if (barleft <= x && x <= barright && y <= bartop + r)
		{
			vy = -vy;
		}

		// 根据速度更新球的坐标
		x = x + vx;
		y = y + vy;

		// 判断是否有按键输入
		if (_kbhit() != 0)
		{
			// 获取按键的字符
			char c = _getch();
			// 如果按了 a 键,就向左移动挡板,如果没有超出窗口的左边界
			if (c == 'a')
			{
				if (barleft > -400)
				{
					barleft = barleft - 20;
					barright = barright - 20;
				}
			}
			// 如果按了 d 键,就向右移动挡板,如果没有超出窗口的右边界
			else if (c == 'd')
			{
				if (barright < 400)
				{
					barleft = barleft + 20;
					barright = barright + 20;
				}
			}


		}

		// 判断球是否落到窗口的底部,如果是,就重新开始游戏
		if (y <= -300)
		{
			// 用当前时间作为随机数种子
			srand((unsigned int)time(NULL));
			// 随机生成球的初始坐标,范围在窗口的中上部
			x = rand() % (400 + 1) - 200;
			y = rand() % (300 + 1) - 150;


			// 重置球的速度为 5
			vx = 5;
			vy = 5;

			// 随机决定球的初始速度的方向
			if (rand() % 2 == 0)
			{
				vy = -vy;
			}
			if (rand() % 2 == 0)
			{
				vx = -vx;
			}

			// 重置挡板的位置为中下部
			barleft = -150;
			barright = 150;
			bartop = -280;
			barbottom = -300;

		}

	}


	// 等待用户按任意键退出
	getchar();
	// 关闭图形窗口
	closegraph();
	return 0;
}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Python弹球游戏的例子,代码如下所示: ```python import turtle import time import random # 设置窗口 win = turtle.Screen() win.title("弹球游戏") win.bgcolor("black") win.setup(width=600, height=600) # 创建球拍 A paddle_a = turtle.Turtle() paddle_a.speed(0) paddle_a.shape("square") paddle_a.color("white") paddle_a.shapesize(stretch_wid=1, stretch_len=5) paddle_a.penup() paddle_a.goto(0, -250) # 创建球 ball = turtle.Turtle() ball.speed(40) ball.shape("circle") ball.color("white") ball.penup() ball.goto(0, 0) ball.dx = 3 ball.dy = -3 # 移动球拍 A def move_left(): x = paddle_a.xcor() x -= 20 paddle_a.setx(x) def move_right(): x = paddle_a.xcor() x += 20 paddle_a.setx(x) # 键盘绑定 win.listen() win.onkeypress(move_left, "Left") win.onkeypress(move_right, "Right") # 循环运行游戏 while True: win.update() # 移动球 ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) # 碰到左右边界 if ball.xcor() > 290: ball.setx(290) ball.dx *= -1 elif ball.xcor() < -290: ball.setx(-290) ball.dx *= -1 # 碰到上下边界 if ball.ycor() > 290: ball.sety(290) ball.dy *= -1 elif ball.ycor() < -290: ball.goto(0, 0) ball.dy *= -1 time.sleep(1) # 碰到球拍 if (ball.ycor() < -240 and ball.ycor() > -250) and (ball.xcor() < paddle_a.xcor() + 50 and ball.xcor() > paddle_a.xcor() - 50): ball.dy *= -1 # 游戏结束 if ball.ycor() < -290: break # 显示游戏结束 game_over = turtle.Turtle() game_over.speed(0) game_over.color("white") game_over.penup() game_over.hideturtle() game_over.goto(0, 0) game_over.write("游戏结束", align="center", font=("Courier", 24, "normal")) # 延迟关闭窗口 time.sleep(3) win.bye() ``` 这个小游戏中,玩家需要控制球拍 A 来接住弹出的球,如果球落地则游戏结束。玩家可以通过键盘左右箭头来移动球拍 A,当球碰到球拍 A 时,球会反弹。这个小游戏可以帮助玩家更好地理解 Python 类的概念,并且也是一个有趣的小游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值