Bug超多的简易贪吃蛇

博主在假期中用C语言编写了一个简单的贪吃蛇游戏,但游戏中存在大量bug,虽然不易修复,但依然完成了基本的w,a,s,d移动和速度控制功能。" 109367920,9473504,Python开发利器:Virtualenvwrapper配置与使用详解,"['Python开发', '虚拟环境', '环境管理']
摘要由CSDN通过智能技术生成

放假闲着没事在写了个没有模块的贪吃蛇,bug有亿点多,难得改了就这样.

代码如下:

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#pragma warning (disable: 4996)
void gotoxy(int x, int y);
void HideCursor();
void UI();
struct snake {
	int x;
	int y;
	int len;
};
struct body {
	int x;
	int y;
} sn[100];
struct food {
	int x;
	int y;
};
int main() {
	UI();
	int t = 200;
	struct snake s;
	struct body sn[100];
	struct food f;
	s.len = 3;
	char ch;
	s.x = 50;
	s.y = 10;
	sn[0].x = 50;
	sn[0].y = 10;
	sn[1].x = 49;
	sn[1].y = 10;
	sn[2].x = 48;
	sn[2].y = 10;
	for (int i = 0; i < s.len; i++) {
		gotoxy(sn[i].x, sn[i].y);
		putch('*');
	}
	f.x = rand()%50+2;
	f.y =  rand()%20+2;
	gotoxy(f.x, f.y);
	putch('@');
	while (1) {
		HideCursor();
		ch = getch();
		if (f.x == s.x && f.y == s.y) {
			s.len++;
			for (int i = s.len - 1; i > 0; i--) {
				sn[i].x = sn[i - 1].x;
				sn[i].y = sn[i - 1].y;
			}
			sn[0].x = f.x;
			sn[0].y = f.y;
			f.x = rand() % 50 + 2;
			f.y = rand() % 20 + 2;
			gotoxy(f.x, f.y);
			putch('@');
		}
		if (s.x == 0 || s.y == 0 || s.x == 80 || s.y == 30) {
			gotoxy(40, 10);
			printf("游戏结束");
			break;
		}
		if (ch == &
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python可以很容易地实现贪吃蛇游戏,以下是一个简单的实现方法: ```python # -*- coding: utf-8 -*- import pygame import random # 初始化pygame pygame.init() # 设置游戏窗口大小 window_width = 800 window_height = 600 game_window = pygame.display.set_mode((window_width, window_height)) # 设置游戏标题 pygame.display.set_caption('Python贪吃蛇') # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) # 定义蛇的初始位置和大小 snake_block_size = 10 snake_speed = 15 snake_list = [] snake_length = 1 snake_x = window_width / 2 snake_y = window_height / 2 # 定义食物的初始位置和大小 food_block_size = 10 food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0 food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0 # 定义字体 font_style = pygame.font.SysFont(None, 30) # 定义分数 def score(score): value = font_style.render("Score: " + str(score), True, black) game_window.blit(value, [0, 0]) # 定义蛇的移动 def snake(snake_block_size, snake_list): for x in snake_list: pygame.draw.rect(game_window, black, [x[0], x[1], snake_block_size, snake_block_size]) # 游戏循环 game_over = False while not game_over: 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: x_change = -snake_block_size y_change = 0 elif event.key == pygame.K_RIGHT: x_change = snake_block_size y_change = 0 elif event.key == pygame.K_UP: y_change = -snake_block_size x_change = 0 elif event.key == pygame.K_DOWN: y_change = snake_block_size x_change = 0 # 判断蛇是否撞墙 if snake_x >= window_width or snake_x < 0 or snake_y >= window_height or snake_y < 0: game_over = True # 移动蛇的位置 snake_x += x_change snake_y += y_change # 判断蛇是否吃到食物 if snake_x == food_x and snake_y == food_y: food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0 food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0 snake_length += 1 # 绘制游戏窗口 game_window.fill(white) pygame.draw.rect(game_window, red, [food_x, food_y, food_block_size, food_block_size]) snake_head = [] snake_head.append(snake_x) snake_head.append(snake_y) snake_list.append(snake_head) if len(snake_list) > snake_length: del snake_list[0] for x in snake_list[:-1]: if x == snake_head: game_over = True snake(snake_block_size, snake_list) score(snake_length - 1) pygame.display.update() # 设置游戏速度 clock = pygame.time.Clock() clock.tick(snake_speed) # 退出pygame pygame.quit() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值