简易贪吃蛇编程代码是什么
其他 95
-
简易贪吃蛇编程代码可以使用Python语言实现。下面是一个示例代码:
import pygame import random # 初始化游戏 pygame.init() # 设置游戏窗口大小 window_width = 800 window_height = 600 window = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("贪吃蛇游戏") # 定义颜色 black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) # 定义蛇的初始位置和大小 snake_block_size = 20 snake_speed = 15 snake_list = [] snake_length = 1 snake_x = window_width / 2 snake_y = window_height / 2 snake_x_change = 0 snake_y_change = 0 # 定义食物的初始位置 food_size = 20 food_x = round(random.randrange(0, window_width - food_size) / 20) * 20 food_y = round(random.randrange(0, window_height - food_size) / 20) * 20 # 定义得分 score = 0 # 定义游戏结束标志 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: snake_x_change = -snake_block_size snake_y_change = 0 elif event.key == pygame.K_RIGHT: snake_x_change = snake_block_size snake_y_change = 0 elif event.key == pygame.K_UP: snake_y_change = -snake_block_size snake_x_change = 0 elif event.key == pygame.K_DOWN: snake_y_change = snake_block_size snake_x_change = 0 # 更新蛇的位置 snake_x += snake_x_change snake_y += snake_y_change # 判断蛇是否吃到食物 if snake_x == food_x and snake_y == food_y: score += 1 food_x = round(random.randrange(0, window_width - food_size) / 20) * 20 food_y = round(random.randrange(0, window_height - food_size) / 20) * 20 snake_length += 1 # 绘制游戏界面 window.fill(black) pygame.draw.rect(window, red, [food_x, food_y, food_size, food_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 segment in snake_list[:-1]: if segment == snake_head: game_over = True for segment in snake_list: pygame.draw.rect(window, white, [segment[0], segment[1], snake_block_size, snake_block_size]) pygame.display.update() # 刷新游戏界面 pygame.display.flip() # 控制游戏速度 pygame.time.Clock().tick(snake_speed) # 游戏结束,退出游戏 pygame.quit()以上代码实现了一个简易的贪吃蛇游戏。玩家可以通过键盘控制蛇的移动方向,蛇会不断前进并吃到食物得分,当蛇碰到自己或者墙壁时游戏结束。
1年前 -
简易贪吃蛇是一种经典的游戏,可以通过编程来实现。下面是一个简单的贪吃蛇编程代码示例:
import pygame import random # 初始化游戏 pygame.init() # 游戏窗口大小 width = 800 height = 600 # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) green = (0, 255, 0) # 初始化游戏窗口 game_display = pygame.display.set_mode((width, height)) pygame.display.set_caption('简易贪吃蛇') # 游戏时钟 clock = pygame.time.Clock() # 蛇头位置和大小 snake_size = 20 snake_speed = 15 # 字体样式 font_style = pygame.font.SysFont(None, 50) score_font = pygame.font.SysFont(None, 35) # 显示得分 def our_snake(snake_size, snake_list): for x in snake_list: pygame.draw.rect(game_display, black, [x[0], x[1], snake_size, snake_size]) # 显示分数 def message(msg, color): mesg = font_style.render(msg, True, color) game_display.blit(mesg, [width / 6, height / 3]) # 游戏循环 def game_loop(): game_over = False game_close = False # 初始位置 x1 = width / 2 y1 = height / 2 # 初始移动方向 x1_change = 0 y1_change = 0 # 蛇身位置列表 snake_list = [] length_of_snake = 1 # 随机生成食物位置 foodx = round(random.randrange(0, width - snake_size) / 20.0) * 20.0 foody = round(random.randrange(0, height - snake_size) / 20.0) * 20.0 # 游戏循环 while not game_over: while game_close == True: game_display.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: x1_change = -snake_size y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_size y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_size x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_size x1_change = 0 # 判断是否碰到边界 if x1 >= width or x1 < 0 or y1 >= height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change game_display.fill(white) pygame.draw.rect(game_display, green, [foodx, foody, snake_size, snake_size]) 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_size, snake_list) pygame.display.update() # 判断是否吃到食物 if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, width - snake_size) / 20.0) * 20.0 foody = round(random.randrange(0, height - snake_size) / 20.0) * 20.0 length_of_snake += 1 # 游戏刷新速度 clock.tick(snake_speed) pygame.quit() quit() # 启动游戏 game_loop()以上是一个使用Python和Pygame库编写的简易贪吃蛇游戏代码示例。通过该代码,可以实现一个贪吃蛇游戏窗口,在游戏中控制蛇移动并吃食物,同时会记录分数并判断游戏结束的条件。
1年前 -
简易贪吃蛇编程代码可以使用Python语言实现。下面是一个简单的贪吃蛇游戏的代码示例:
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('贪吃蛇游戏') 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, 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 game_loop(): 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("游戏结束!按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 = -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) 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() game_loop()这个代码使用了Pygame库来实现贪吃蛇游戏。游戏窗口的大小为800×600,蛇的大小为10×10。游戏中的蛇可以通过键盘的方向键来控制移动。当蛇吃到食物时,蛇的长度会增加,游戏的难度也会随着时间的推移而增加。游戏结束的条件是蛇撞到墙壁或者撞到自己的身体。
游戏的主循环中,通过监听键盘事件来控制蛇的移动方向。每帧更新蛇的位置和长度,并判断是否吃到食物或者游戏结束。游戏结束时,显示游戏结束的提示信息,并提供重新开始游戏和退出游戏的选项。
以上是一个简单的贪吃蛇游戏的代码实现,你可以根据自己的需求进行修改和扩展。
1年前