C语言
#include
#include
#include
#include
int width = 20, height = 20;
int gameover, score;
int x, y, fruitX, fruitY, flag;
int tailX[100], tailY[100];
int nTail;
void Setup()
{
gameover = 0;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void Draw()
{
system("cls");
for (int i = 0; i = width)
x = 0;
else if (x = height)
y = 0;
else if (y
#include
#include
#include
#include
#include
// 定义游戏界面尺寸
#define WIDTH 20
#define HEIGHT 20
// 定义方向
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int length; // 蛇的长度
int head_x, head_y; // 蛇头的位置
int food_x, food_y; // 食物的位置
int score; // 得分
int gameover; // 游戏是否结束
int direction; // 蛇的移动方向
int snake_x[100], snake_y[100]; // 蛇的身体坐标
// 初始化游戏
void Setup()
{
gameover = 0;
length = 1; // 蛇初始长度为1
head_x = WIDTH / 2; // 蛇初始位置在中间
head_y = HEIGHT / 2;
// 生成食物的初始位置
srand((unsigned)time(NULL));
food_x = rand() % (WIDTH - 1) + 1;
food_y = rand() % (HEIGHT - 1) + 1;
score = 0;
}
// 绘制游戏界面
void Draw()
{
system("cls"); // 清屏
// 绘制上边界
for (int i = 0; i = WIDTH - 1 || head_y = HEIGHT - 1)
gameover = 1;
for (int i = 0; i
Python
import pygame
import random
# 游戏窗口尺寸
window_width = 640
window_height = 480
# 贪吃蛇方块尺寸和速度
block_size = 20
snake_speed = 15
# 颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
pygame.init()
# 创建游戏窗口
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('贪吃蛇')
# 游戏时钟
clock = pygame.time.Clock()
font_style = pygame.font.SysFont(None, 30)
score_font = pygame.font.SysFont(None, 50)
def our_snake(block_size, snake_list):
for x in snake_list:
pygame.draw.rect(window, black, [x[0], x[1], block_size, block_size])
def message(msg, color):
message_text = font_style.render(msg, True, color)
window.blit(message_text, [window_width/6, window_height/3])
def game_loop():
game_over = False
game_close = False
# 贪吃蛇初始位置
x1 = window_width / 2
y1 = window_height / 2
# 贪吃蛇移动方向
x1_change = 0
y1_change = 0
# 贪吃蛇身体部分
snake_list = []
length_of_snake = 1
# 食物位置
foodx = round(random.randrange(0, window_width - block_size) / 20.0) * 20.0
foody = round(ra服务器托管网ndom.randrange(0, window_height - block_size) / 20.0) * 20.0
while not game_over:
while game_close:
window.fill(white)
message("游戏结束!按 Q-退出,C-重新开始", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
game_close = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
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 = -block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = block_size
x1_change = 0
if x1 >= window_width or x1 = window_height or y1 length_of_snake:
服务器托管网 del snake_list[0]
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
our_snake(block_size, snake_list)
pygame.display.update()
if x1 == foodx and y1 == foody:
# 吃到食物,贪吃蛇长度增加
foodx = round(random.randrange(0, window_width - block_size) / 20.0) * 20.0
foody = round(random.randrange(0, window_height - block_size) / 20.0) * 20.0
length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
game_loop()
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
1.使用keytool创建密钥对..并在服务端和客户端完成分配..具体方法可参看:java-使用keytool来创建管理密钥及证书等-java学习笔记(3) 2.建立客户端和服务端的socket实例: 客户端代码如下: public class SSLC…