贪吃蛇简单编程代码是什么
-
贪吃蛇是一款经典的游戏,它的编程实现相对简单。下面是一个简单的贪吃蛇编程代码示例:
import pygame import time import random pygame.init() white = (255, 255, 255) yellow = (255, 255, 102) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213) 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() snake_block = 10 snake_speed = 30 font_style = pygame.font.SysFont(None, 50) score_font = pygame.font.SysFont(None, 35) def our_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(dis, green, [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 Q-Quit or C-Play Again", red) 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, yellow, [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) 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()这是一个使用Python和Pygame库编写的简单贪吃蛇游戏代码。它使用了pygame库来创建游戏窗口、控制游戏逻辑和绘制游戏画面。
代码首先初始化了游戏窗口和一些颜色变量。然后定义了一些常量,如蛇的大小、速度和字体样式。
接着定义了一些函数,包括绘制蛇、显示游戏结束信息等。在游戏主循环中,通过监听用户输入来控制蛇的移动方向,判断游戏是否结束,更新蛇的位置,检测是否吃到食物并增加蛇的长度。游戏主循环中还包含了一些事件处理逻辑和绘制游戏画面的代码。
最后调用gameLoop()函数开始游戏。游戏循环会一直运行,直到游戏结束。
这只是一个简单的贪吃蛇游戏示例,你可以根据自己的需求进行修改和扩展。希望对你有帮助!
1年前 -
贪吃蛇是一种经典的游戏,它的编程代码可以使用各种编程语言进行实现。下面是使用Python编写的简单贪吃蛇游戏代码示例:
import pygame import random # 初始化游戏 pygame.init() # 定义屏幕尺寸 screen_width = 640 screen_height = 480 # 定义贪吃蛇的尺寸和速度 snake_size = 20 snake_speed = 10 # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) # 创建屏幕对象 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("贪吃蛇") # 定义游戏结束的函数 def game_over(): pygame.quit() quit() # 定义贪吃蛇的函数 def snake(snake_size, snake_list): for x in snake_list: pygame.draw.rect(screen, black, [x[0], x[1], snake_size, snake_size]) # 定义游戏主循环 def game_loop(): game_over = False game_close = False # 初始化贪吃蛇的位置和移动方向 x = screen_width / 2 y = screen_height / 2 x_change = 0 y_change = 0 # 初始化贪吃蛇的身体 snake_list = [] snake_length = 1 # 初始化食物的位置 food_x = round(random.randrange(0, screen_width - snake_size) / 20) * 20 food_y = round(random.randrange(0, screen_height - snake_size) / 20) * 20 # 游戏主循环 while not game_over: while game_close: screen.fill(white) message("游戏结束!按Q退出,按C重新开始", red) 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: 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: x_change = -snake_size y_change = 0 elif event.key == pygame.K_RIGHT: x_change = snake_size y_change = 0 elif event.key == pygame.K_UP: y_change = -snake_size x_change = 0 elif event.key == pygame.K_DOWN: y_change = snake_size x_change = 0 # 更新贪吃蛇的位置 x += x_change y += y_change # 绘制背景 screen.fill(white) # 绘制食物 pygame.draw.rect(screen, red, [food_x, food_y, snake_size, snake_size]) # 绘制贪吃蛇 snake_head = [] snake_head.append(x) snake_head.append(y) snake_list.append(snake_head) if len(snake_list) > snake_length: del snake_list[0] # 检测贪吃蛇与自身的碰撞 for segment in snake_list[:-1]: if segment == snake_head: game_close = True # 检测贪吃蛇与边界的碰撞 if x < 0 or x >= screen_width or y < 0 or y >= screen_height: game_close = True # 绘制贪吃蛇的身体 snake(snake_size, snake_list) # 更新屏幕 pygame.display.update() # 检测贪吃蛇与食物的碰撞 if x == food_x and y == food_y: food_x = round(random.randrange(0, screen_width - snake_size) / 20) * 20 food_y = round(random.randrange(0, screen_height - snake_size) / 20) * 20 snake_length += 1 # 控制贪吃蛇的速度 clock.tick(snake_speed) # 结束游戏 game_over() # 运行游戏 game_loop()这段代码使用了
pygame库来实现简单的贪吃蛇游戏。它通过监听键盘事件来控制贪吃蛇的移动,检测贪吃蛇与食物、自身、边界的碰撞,并实时更新游戏画面。游戏结束后,可以选择重新开始或退出游戏。这只是一个简单的贪吃蛇游戏代码示例,你可以根据自己的需求进行修改和扩展。1年前 -
贪吃蛇是一个经典的游戏,也是学习编程的入门项目之一。下面是一个简单的贪吃蛇游戏的编程代码示例(使用Python语言):
import pygame import random # 定义颜色常量 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # 定义方向常量 UP = 0 DOWN = 1 LEFT = 2 RIGHT = 3 # 初始化游戏 pygame.init() # 设置游戏窗口大小和标题 size = (800, 600) screen = pygame.display.set_mode(size) pygame.display.set_caption("贪吃蛇游戏") # 初始化蛇的位置和长度 snake = [(200, 200), (210, 200), (220, 200)] snake_length = 3 # 初始化食物的位置 food = (random.randint(0, 39) * 20, random.randint(0, 29) * 20) # 初始化蛇的移动方向 direction = RIGHT # 设置游戏时钟 clock = pygame.time.Clock() # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 处理按键事件 if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and direction != DOWN: direction = UP elif event.key == pygame.K_DOWN and direction != UP: direction = DOWN elif event.key == pygame.K_LEFT and direction != RIGHT: direction = LEFT elif event.key == pygame.K_RIGHT and direction != LEFT: direction = RIGHT # 移动蛇的头部 if direction == UP: snake_head = (snake[0][0], snake[0][1] - 20) elif direction == DOWN: snake_head = (snake[0][0], snake[0][1] + 20) elif direction == LEFT: snake_head = (snake[0][0] - 20, snake[0][1]) elif direction == RIGHT: snake_head = (snake[0][0] + 20, snake[0][1]) # 将新的头部加入蛇的列表中 snake.insert(0, snake_head) # 检查蛇是否吃到了食物 if snake[0] == food: # 随机生成新的食物位置 food = (random.randint(0, 39) * 20, random.randint(0, 29) * 20) # 增加蛇的长度 snake_length += 1 # 删除蛇的尾部,使其长度保持不变 if len(snake) > snake_length: snake.pop() # 绘制背景和网格 screen.fill(BLACK) for x in range(0, 800, 20): pygame.draw.line(screen, WHITE, (x, 0), (x, 600)) for y in range(0, 600, 20): pygame.draw.line(screen, WHITE, (0, y), (800, y)) # 绘制蛇 for body in snake: pygame.draw.rect(screen, GREEN, (body[0], body[1], 20, 20)) # 绘制食物 pygame.draw.rect(screen, RED, (food[0], food[1], 20, 20)) # 更新屏幕显示 pygame.display.flip() # 控制游戏帧率 clock.tick(10) # 退出游戏 pygame.quit()以上代码是一个简单的贪吃蛇游戏的基本实现,它使用pygame库来进行图形绘制和事件处理。在游戏循环中,通过处理键盘事件来改变蛇的移动方向,然后根据移动方向更新蛇的位置。在每次循环中,还需要检查蛇是否吃到了食物,并相应地调整蛇的长度。最后,通过绘制函数将蛇和食物显示在游戏窗口上,并使用时钟控制游戏帧率。
这只是一个简单的示例代码,你可以根据自己的需求进行修改和扩展,例如增加游戏结束的判断、添加障碍物、增加计分等功能。希望这个示例能帮助你入门贪吃蛇游戏的编程。
1年前