SNAKE

#include<stdio.h>
#include<graphics.h> //图形库 easyx
#include<conio.h>

IMAGE  beijin;//背景图片
IMAGE  wanjia[2];//玩家图片
IMAGE  zidan[2];//子弹
enum  My
{
	WIDTH = 591,  //枚举窗口的高宽
	HIGHT = 864,
	BULLLET_NUM = 500//枚举玩家子弹数量
};

struct Planmove//飞机移动状态
{
	int x;
	int y;
	bool live;//是否活着

}player, bull[BULLLET_NUM];

//加载图片
void jiazaitupian()
{
	//加载图片
	loadimage(&beijin, "./images/background.jpg");
	loadimage(&wanjia[0], "./images/planeNormal_1.jpg");//掩码图和原图
	loadimage(&wanjia[1], "./images/planeNormal_2.jpg");
	loadimage(&zidan[0], "./images/bullet1.jpg");//子弹图片两张
	loadimage(&zidan[1], "./images/bullet2.jpg");
}

//游戏初始化
void gameInit()
{
	jiazaitupian();
	player.x = WIDTH / 2 - 45;
	player.y = HIGHT - 120;
	player.live = true;
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		bull[i].x = 0;
		bull[i].y = 0;
		bull[i].live = false;
	}
}

//创建子弹1
void creataBullet()
{
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (!bull[i].live)
		{
			bull[i].x = player.x + 60;
			bull[i].y = player.y;
			bull[i].live = true;
			break;
		}
	}
}
//创建子弹2
void creataBullet2()
{
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (!bull[i].live)
		{
			bull[i].x = player.x + 120;
			bull[i].y = player.y;
			bull[i].live = true;
			break;
		}
	}
}
//创建子弹3
void creataBullet3()
{
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (!bull[i].live)
		{
			bull[i].x = player.x + 0;
			bull[i].y = player.y;
			bull[i].live = true;
			break;
		}
	}
}
//游戏桌面绘制函数
void GameDraw()
{
	//背景图放在窗口上
	putimage(0, 0, &beijin);//0,0开始放置图面左上角
	putimage(player.x, player.y, &wanjia[0], NOTSRCCOPY);
	putimage(player.x, player.y, &wanjia[1], SRCINVERT);
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (bull[i].live) //为真的  才画子弹
		{
			putimage(bull[i].x, bull[i].y, &zidan[0], NOTSRCCOPY);
			putimage(bull[i].x, bull[i].y, &zidan[1], SRCINVERT);
		}
	}
}

//角色移动,获取键盘,上下左右
void Playermove(int speed)//玩家移动
{
	/*第一种方法*/
	/*
	//扫描键盘是否按下返回1  0
	if (_kbhit)
	{
		char  key = _getch();
		switch (key)
		{
		case'W':
		case'w':
			player.y -= speed;
			break;
		case'a':
		case'A':
			player.x -= speed;
			break;
		case's':
		case'S':
			player.y += speed;
			break;
		case'd':
		case'D':
			player.x += speed;
			break;
		}
	}*/
	/*第二种  使用windows AIP*/
	if (GetAsyncKeyState(VK_UP)) //非阻塞,异步的 很流畅
	{
		if (player.y > 0)
			player.y -= speed;
	}
	if (GetAsyncKeyState(VK_DOWN))
	{
		if (player.y < HIGHT - 120)
		{
			player.y += speed;
		}
	}
	if (GetAsyncKeyState(VK_LEFT))
	{
		if (player.x + 60 > 0)
		{
			player.x -= speed;
		}
	}
	if (GetAsyncKeyState(VK_RIGHT))
	{
		if (player.x - 60 < WIDTH - 117)
		{
			player.x += speed;
		}
	}
	static DWORD t1=0,t2=0; //延迟

	if (GetAsyncKeyState(VK_SPACE)&& t2-t1>100)
	{
		//按下空格就创建一个子弹
		creataBullet();
		creataBullet3();
		creataBullet2();

		t1 = t2;
	}
	t2 = GetTickCount();
}

//子弹移动
void BulletMove()
{
	for (int i = 0; i < BULLLET_NUM; i++)
	{
		if (bull[i].live)
		{
			bull[i].y -= 1.2;
			if (bull[i].y < 0)
			{
				bull[i].live = false;
			}
		}
	}
}

int  main()
{
	//游戏各个参数初始化
	gameInit();
	//创建图形窗口
	initgraph(WIDTH, HIGHT);
	GameDraw();
	//双缓冲绘图
	BeginBatchDraw();


	while (true)
	{

		GameDraw();
		FlushBatchDraw();//双缓冲中间暂停一帧	
		Playermove(1);	
		BulletMove();

	}
	EndBatchDraw();//双缓冲绘图至此处
	return 0;
}
提供的引用内容未涉及“buu”和“snake”相关的IT信息。一般来说,“buu”可能指“BUUCTF”,这是一个在线CTF(Capture The Flag)平台,为网络安全爱好者提供了各种类型的安全挑战题目,涵盖Web安全、逆向工程、密码学等多个领域。“snake”在IT领域常代表“Snake Case”(蛇形命名法),它是一种命名规范,单词之间用下划线分隔,如`variable_name`,常用于Python等编程语言中变量和函数的命名。此外,“snake”也可能指经典的贪吃蛇游戏,在编程学习中,实现贪吃蛇游戏是一个常见的实践项目,可以使用Python、Java等多种编程语言完成。 ```python # 一个简单的Python贪吃蛇游戏示例框架 import pygame import time import random snake_block = 10 snake_speed = 15 # 初始化pygame pygame.init() # 设置显示窗口 dis_width = 800 dis_height = 600 dis = pygame.display.set_mode((dis_width, dis_height)) pygame.display.set_caption('Snake Game') clock = pygame.time.Clock() # 定义颜色 white = (255, 255, 255) yellow = (255, 255, 102) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213) font_style = pygame.font.SysFont("bahnschrift", 25) score_font = pygame.font.SysFont("comicsansms", 35) def Your_score(score): value = score_font.render("Your Score: " + str(score), True, yellow) dis.blit(value, [0, 0]) def our_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block]) def message(msg, color): mesg = font_style.render(msg, True, color) dis.blit(mesg, [dis_width / 6, dis_height / 3]) def gameLoop(): game_over = False game_close = False x1 = dis_width / 2 y1 = dis_height / 2 x1_change = 0 y1_change = 0 snake_List = [] Length_of_snake = 1 foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 while not game_over: while game_close == True: dis.fill(blue) message("You Lost! Press C-Play Again or Q-Quit", red) Your_score(Length_of_snake - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: gameLoop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block x1_change = 0 if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change dis.fill(blue) pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block]) snake_Head = [] snake_Head.append(x1) snake_Head.append(y1) snake_List.append(snake_Head) if len(snake_List) > Length_of_snake: del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True our_snake(snake_block, snake_List) Your_score(Length_of_snake - 1) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 Length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit() gameLoop() ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值