贪吃蛇简单编程代码是什么
-
贪吃蛇是一款经典的游戏,编写简单的贪吃蛇代码可以帮助初学者学习编程基础。下面是一个简单的贪吃蛇游戏的编程代码示例:
import pygame import sys import time import random pygame.init() white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) display_width = 800 display_height = 600 gameDisplay = pygame.display.set_mode((display_width, display_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(gameDisplay, black, [x[0], x[1], snake_block, snake_block]) def message(msg, color): mesg = font_style.render(msg, True, color) gameDisplay.blit(mesg, [display_width / 6, display_height / 3]) def gameLoop(): game_over = False game_close = False x1 = display_width / 2 y1 = display_height / 2 x1_change = 0 y1_change = 0 snake_List = [] Length_of_snake = 1 foodx = round(random.randrange(0, display_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, display_height - snake_block) / 10.0) * 10.0 while not game_over: while game_close == True: gameDisplay.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: 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 >= display_width or x1 < 0 or y1 >= display_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change gameDisplay.fill(white) pygame.draw.rect(gameDisplay, red, [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, display_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, display_height - snake_block) / 10.0) * 10.0 Length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit() gameLoop()以上是一个使用Python编写的简单的贪吃蛇游戏代码。运行代码后,你可以使用上下左右键控制蛇的移动,吃到食物后蛇的长度会增加。如果蛇碰到墙壁或自己的身体,游戏会结束并提示你输了。你可以按Q退出游戏,按C重新开始游戏。
这个代码使用了Pygame库来实现游戏窗口的显示和游戏逻辑的处理。通过不断更新蛇的位置和食物的位置,并判断蛇是否碰到墙壁或自己的身体,来实现贪吃蛇游戏的基本功能。
希望这个简单的贪吃蛇代码能够帮助你入门编程,并激发你对编程的兴趣!
1年前 -
贪吃蛇是一款经典的游戏,它的编程代码可以使用各种编程语言来实现。以下是一个简单的贪吃蛇游戏的编程代码示例,使用Python语言实现:
import pygame import random pygame.init() # 游戏窗口的宽度和高度 width = 800 height = 600 # 定义颜色 black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) # 初始化游戏窗口 game_display = pygame.display.set_mode((width, height)) pygame.display.set_caption('贪吃蛇游戏') # 设置游戏时钟 clock = pygame.time.Clock() # 设置蛇的大小和速度 snake_block_size = 10 snake_speed = 15 # 定义字体 font_style = pygame.font.SysFont(None, 50) score_font = pygame.font.SysFont(None, 35) def our_snake(snake_block_size, snake_list): for x in snake_list: pygame.draw.rect(game_display, black, [x[0], x[1], snake_block_size, snake_block_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_block_size) / 10.0) * 10.0 foody = round(random.randrange(0, height - snake_block_size) / 10.0) * 10.0 while not game_over: while game_close: game_display.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 = -snake_block_size y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block_size y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block_size x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block_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_block_size, snake_block_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_block_size, snake_list) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, width - snake_block_size) / 10.0) * 10.0 foody = round(random.randrange(0, height - snake_block_size) / 10.0) * 10.0 length_of_snake += 1 clock.tick(snake_speed) pygame.quit() game_loop()以上代码是一个使用pygame库实现的贪吃蛇游戏,其中包括了初始化游戏窗口、定义颜色、设置蛇的大小和速度、定义字体等。游戏的主要逻辑是通过不断监听用户的按键事件来控制蛇的移动,当蛇吃到食物时,蛇的长度增加,游戏分数也随之增加。当蛇碰到墙壁或者自己时,游戏结束。游戏结束后,可以选择重新开始或退出游戏。这个简单的代码实现了贪吃蛇游戏的基本功能,你可以根据自己的需求对其进行修改和扩展。
1年前 -
贪吃蛇是一款经典的游戏,编写贪吃蛇的代码可以帮助初学者理解编程的基本概念和逻辑。下面是一个简单的贪吃蛇编程代码示例,使用Python编写:
import pygame import sys import random # 初始化游戏 pygame.init() # 定义窗口大小和游戏速度 window_width = 800 window_height = 600 game_speed = 15 # 定义贪吃蛇头部的颜色和大小 head_color = (0, 255, 0) head_size = (20, 20) # 定义食物的颜色和大小 food_color = (255, 0, 0) food_size = (20, 20) # 创建窗口 window = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("贪吃蛇") # 设置定时器 clock = pygame.time.Clock() # 定义贪吃蛇的初始位置和移动方向 snake_position = [100, 50] snake_body = [[100, 50], [90, 50], [80, 50]] snake_direction = "RIGHT" # 定义食物的初始位置 food_position = [random.randrange(1, (window_width // 20)) * 20, random.randrange(1, (window_height // 20)) * 20] food_spawn = True # 游戏主循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: snake_direction = "RIGHT" elif event.key == pygame.K_LEFT: snake_direction = "LEFT" elif event.key == pygame.K_UP: snake_direction = "UP" elif event.key == pygame.K_DOWN: snake_direction = "DOWN" # 更新贪吃蛇的位置 if snake_direction == "RIGHT": snake_position[0] += 20 elif snake_direction == "LEFT": snake_position[0] -= 20 elif snake_direction == "UP": snake_position[1] -= 20 elif snake_direction == "DOWN": snake_position[1] += 20 # 增加贪吃蛇的身体长度 snake_body.insert(0, list(snake_position)) if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]: food_spawn = False else: snake_body.pop() # 生成新的食物 if not food_spawn: food_position = [random.randrange(1, (window_width // 20)) * 20, random.randrange(1, (window_height // 20)) * 20] food_spawn = True # 绘制游戏界面 window.fill((0, 0, 0)) for pos in snake_body: pygame.draw.rect(window, head_color, pygame.Rect( pos[0], pos[1], head_size[0], head_size[1])) pygame.draw.rect(window, food_color, pygame.Rect( food_position[0], food_position[1], food_size[0], food_size[1])) # 检测碰撞 if snake_position[0] < 0 or snake_position[0] >= window_width or snake_position[1] < 0 or snake_position[1] >= window_height: pygame.quit() sys.exit() for block in snake_body[1:]: if snake_position[0] == block[0] and snake_position[1] == block[1]: pygame.quit() sys.exit() # 更新游戏界面 pygame.display.update() clock.tick(game_speed)这段代码使用了pygame库来创建游戏窗口、处理事件、绘制图形等操作。通过键盘事件来控制贪吃蛇的移动方向,每次更新贪吃蛇的位置时,都会检测是否吃到了食物并生成新的食物。同时,还需要检测贪吃蛇是否撞墙或者撞到自己的身体,如果发生碰撞则游戏结束。
这只是一个简单的贪吃蛇编程代码示例,可以根据自己的需求进行扩展和优化。编写贪吃蛇的代码可以锻炼编程思维和逻辑能力,同时也可以帮助初学者熟悉编程语言的基本语法和常用库的使用。
1年前