用python编辑一个贪吃蛇游戏,提供代码
时间: 2023-09-11 16:05:14 浏览: 141
### 回答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)
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("你输了!再来一次? 按 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 >= 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_sn
### 回答2:
贪吃蛇游戏是经典的游戏之一,通过使用Python编程语言,可以轻松实现一个简单的贪吃蛇游戏。
以下是一个基于Python的贪吃蛇游戏的示例代码:
```python
import pygame
import time
import random
pygame.init()
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
dis_width = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.update()
pygame.display.set_caption('贪吃蛇')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 20
font_style = pygame.font.SysFont(None, 30)
score_font = pygame.font.SysFont(None, 50)
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("游戏结束,按Q-退出,C-重新开始", white)
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:
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, 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, 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()
```
运行以上代码,你就可以在Pygame窗口中玩贪吃蛇游戏了。使用方向键控制蛇的移动方向,目标是吃到食物,当蛇碰到边界或自身时游戏结束。按Q键退出游戏,按C键重新开始。
### 回答3:
以下是一个使用Python语言编写的基本版贪吃蛇游戏的示例代码:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置游戏界面尺寸和速度
screen_width = 600
screen_height = 400
cell_size = 20
snake_speed = 10
# 设置颜色
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇游戏')
# 初始化游戏变量
snake_position = [[100, 50], [90, 50], [80, 50]]
snake_body = pygame.Surface((cell_size, cell_size))
snake_body.fill(blue)
food_position = [random.randrange(1, (screen_width // cell_size)) * cell_size,
random.randrange(1, (screen_height // cell_size)) * cell_size]
food_image = pygame.Surface((cell_size, cell_size))
food_image.fill(red)
direction = 'RIGHT'
change_to = direction
score = 0
# 游戏结束函数
def game_over():
font = pygame.font.SysFont('arial', 54)
game_over_surface = font.render('Game Over', True, white)
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (screen_width / 2, screen_height / 4)
screen.fill(black)
screen.blit(game_over_surface, game_over_rect)
pygame.display.flip()
pygame.time.wait(5000)
pygame.quit()
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT or event.key == ord('d'):
change_to = 'RIGHT'
if event.key == pygame.K_LEFT or event.key == ord('a'):
change_to = 'LEFT'
if event.key == pygame.K_UP or event.key == ord('w'):
change_to = 'UP'
if event.key == pygame.K_DOWN or event.key == ord('s'):
change_to = 'DOWN'
# 根据按键更新方向
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
# 根据方向更新蛇头位置
if direction == 'RIGHT':
snake_position[0][0] += cell_size
if direction == 'LEFT':
snake_position[0][0] -= cell_size
if direction == 'UP':
snake_position[0][1] -= cell_size
if direction == 'DOWN':
snake_position[0][1] += cell_size
# 绘制蛇和食物
screen.fill(black)
for position in snake_position:
screen.blit(snake_body, pygame.Rect(position[0], position[1], cell_size, cell_size))
screen.blit(food_image, pygame.Rect(food_position[0], food_position[1], cell_size, cell_size))
# 吃到食物时更新蛇的长度和得分
if snake_position[0][0] == food_position[0] and snake_position[0][1] == food_position[1]:
score += 1
snake_position.append([0, 0])
food_position = [random.randrange(1, (screen_width // cell_size)) * cell_size,
random.randrange(1, (screen_height // cell_size)) * cell_size]
# 当蛇的头部与身体的其他部分发生碰撞时,游戏结束
for body_part in snake_position[1:]:
if snake_position[0][0] == body_part[0] and snake_position[0][1] == body_part[1]:
game_over()
pygame.display.flip()
pygame.time.Clock().tick(snake_speed)
```
这是一个非常基本的贪吃蛇游戏,可以根据个人需要进行进一步的游戏功能和界面的扩展。希望对你有帮助!
阅读全文