贪吃蛇编程简易代码是什么
其他 58
-
贪吃蛇是一款经典的游戏,它可以通过编程实现。下面是一个简易的贪吃蛇编程代码示例:
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("贪吃蛇游戏") # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) # 定义蛇的初始位置和速度 snake_block_size = 20 snake_speed = 15 x1 = window_width / 2 y1 = window_height / 2 x1_change = 0 y1_change = 0 # 定义食物的初始位置 x_food = round(random.randrange(0, window_width - snake_block_size) / 20) * 20 y_food = round(random.randrange(0, window_height - snake_block_size) / 20) * 20 # 定义蛇的长度 snake_length = 1 snake_list = [] # 定义得分 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: 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 # 更新蛇的位置 x1 += x1_change y1 += y1_change # 绘制背景 window.fill(black) # 绘制食物 pygame.draw.rect(window, red, [x_food, y_food, 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) > snake_length: del snake_list[0] # 检测蛇与食物的碰撞 for x in snake_list[:-1]: if x == snake_head: game_over = True # 绘制蛇 for x in snake_list: pygame.draw.rect(window, white, [x[0], x[1], snake_block_size, snake_block_size]) # 更新得分 if x1 == x_food and y1 == y_food: x_food = round(random.randrange(0, window_width - snake_block_size) / 20) * 20 y_food = round(random.randrange(0, window_height - snake_block_size) / 20) * 20 snake_length += 1 score += 10 # 更新窗口 pygame.display.update() # 控制蛇的速度 clock = pygame.time.Clock() clock.tick(snake_speed) # 游戏结束,退出程序 pygame.quit()以上是一个简易的贪吃蛇游戏编程代码示例,使用Python的pygame库实现。在这个示例中,我们通过不断更新蛇的位置、检测蛇与食物的碰撞、控制蛇的速度等方式实现了一个基本的贪吃蛇游戏。你可以根据自己的需求对代码进行修改和优化,添加更多的功能和特性。希望这个示例能对你有所帮助!
1年前 -
以下是一个简易的贪吃蛇游戏的编程代码示例:
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 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: 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, 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() gameLoop()这段代码使用了Pygame库,创建了一个简单的贪吃蛇游戏。游戏窗口的大小为800×600像素,贪吃蛇的速度为30帧。游戏中的贪吃蛇通过键盘上的箭头键控制移动,吃到食物后身体长度增加。如果贪吃蛇碰到墙壁或自己的身体,游戏结束。玩家可以选择重新开始或退出游戏。
这段代码的主要功能包括:
- 初始化Pygame和游戏窗口。
- 定义了游戏中使用的颜色。
- 设置贪吃蛇的大小、速度、字体样式。
- 定义了绘制贪吃蛇和文字信息的函数。
- 实现了游戏主循环,包括处理用户输入、贪吃蛇移动、碰撞检测、食物生成等逻辑。
请注意,这只是一个简易的贪吃蛇游戏代码示例,可能还有其他实现方式和优化方法。
1年前 -
贪吃蛇是一款经典的游戏,在编程中也经常被用来作为练手的项目。下面是一个简易的贪吃蛇游戏的编程代码示例,使用Python语言实现。
import pygame import random # 初始化游戏 pygame.init() # 设置游戏窗口的宽度和高度 window_width = 800 window_height = 600 # 设置贪吃蛇和食物的大小 snake_size = 20 food_size = 20 # 设置贪吃蛇的初始位置 snake_x = window_width / 2 snake_y = window_height / 2 # 设置贪吃蛇的初始速度 snake_speed = 10 # 设置游戏窗口的背景颜色 window_bg_color = (255, 255, 255) # 设置贪吃蛇的颜色 snake_color = (0, 255, 0) # 设置食物的颜色 food_color = (255, 0, 0) # 创建游戏窗口 window = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("贪吃蛇游戏") # 创建时钟对象,用于控制游戏循环的频率 clock = pygame.time.Clock() # 初始化贪吃蛇的移动方向,默认向右移动 snake_direction = "right" # 初始化贪吃蛇的身体,使用列表表示,初始长度为1 snake_body = [{'x': snake_x, 'y': snake_y}] # 初始化食物的位置 food_x = round(random.randrange(0, window_width - food_size) / 20) * 20 food_y = round(random.randrange(0, window_height - food_size) / 20) * 20 # 游戏主循环 while True: # 处理退出事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) # 处理键盘事件,改变贪吃蛇的移动方向 keys = pygame.key.get_pressed() for key in keys: if keys[pygame.K_LEFT]: snake_direction = "left" elif keys[pygame.K_RIGHT]: snake_direction = "right" elif keys[pygame.K_UP]: snake_direction = "up" elif keys[pygame.K_DOWN]: snake_direction = "down" # 根据移动方向改变贪吃蛇的位置 if snake_direction == "left": snake_x -= snake_speed elif snake_direction == "right": snake_x += snake_speed elif snake_direction == "up": snake_y -= snake_speed elif snake_direction == "down": snake_y += snake_speed # 更新贪吃蛇的位置 snake_body.insert(0, {'x': snake_x, 'y': snake_y}) # 判断贪吃蛇是否吃到了食物 if snake_x == food_x and snake_y == food_y: # 食物被吃掉后,重新生成一个食物 food_x = round(random.randrange(0, window_width - food_size) / 20) * 20 food_y = round(random.randrange(0, window_height - food_size) / 20) * 20 else: # 贪吃蛇没有吃到食物时,删除贪吃蛇的尾部,实现贪吃蛇的移动效果 snake_body.pop() # 绘制游戏窗口的背景 window.fill(window_bg_color) # 绘制贪吃蛇 for body_part in snake_body: pygame.draw.rect(window, snake_color, pygame.Rect(body_part['x'], body_part['y'], snake_size, snake_size)) # 绘制食物 pygame.draw.rect(window, food_color, pygame.Rect(food_x, food_y, food_size, food_size)) # 更新游戏窗口 pygame.display.flip() # 控制游戏循环的频率 clock.tick(20)以上是一个简易的贪吃蛇游戏的编程代码示例。通过使用pygame库来实现游戏窗口的创建、事件的处理、图形的绘制等功能。在游戏的主循环中,根据键盘事件来改变贪吃蛇的移动方向,然后根据移动方向来更新贪吃蛇的位置。同时判断贪吃蛇是否吃到了食物,并根据结果来更新食物的位置和贪吃蛇的长度。最后通过绘制函数来绘制游戏窗口的背景、贪吃蛇和食物等图形。
1年前