写个Python贪吃蛇的游戏程序
时间: 2023-09-10 09:05:45 浏览: 109
Python编写一个贪吃蛇游戏
### 回答1:
可以!这里有一个简单的Python贪吃蛇游戏程序的例子: import random
import curses
s = curses.initscr()
curses.curs_set(0)
sh, sw = s.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)
snk_x = sw/4
snk_y = sh/2
snake = [
[snk_y, snk_x],
[snk_y, snk_x-1],
[snk_y, snk_x-2]
]
food = [sh/2, sw/2]
w.addch(food[0], food[1], curses.ACS_PI)
key = curses.KEY_RIGHT
while True:
next_key = w.getch()
key = key if next_key == -1 else next_key
if snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in snake[1:]:
curses.endwin()
quit()
new_head = [snake[0][0], snake[0][1]]
if key == curses.KEY_DOWN:
new_head[0] += 1
if key == curses.KEY_UP:
new_head[0] -= 1
if key == curses.KEY_LEFT:
new_head[1] -= 1
if key == curses.KEY_RIGHT:
new_head[1] += 1
snake.insert(0, new_head)
if snake[0] == food:
food = None
while food is None:
nf = [
random.randint(1, sh-1),
random.randint(1, sw-1)
]
food = nf if nf not in snake else None
w.addch(food[0], food[1], curses.ACS_PI)
else:
tail = snake.pop()
w.addch(tail[0], tail[1], ' ')
w.addch(snake[0][0], snake[0][1], curses.ACS_CKBOARD)
### 回答2:
Python贪吃蛇游戏程序的实现是一个很有趣的项目。下面是一种可能的实现方式:
```python
import pygame
import time
# 初始化游戏
pygame.init()
# 设置游戏窗口大小和标题
width, height = 800, 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("贪吃蛇游戏")
# 定义颜色
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
# 定义蛇的初始位置和大小
snake_pos = [[100, 50], [90, 50], [80, 50]]
snake_size = 10
# 定义食物的初始位置和大小
food_pos = [random.randrange(1, (width//10)) * 10, random.randrange(1, (height//10)) * 10]
food_size = 10
# 定义分数
score = 0
# 定义蛇的移动速度
snake_speed = 15
snake_direction = "RIGHT"
change_to = snake_direction
# 控制游戏是否结束变量
game_over = False
# 游戏主循环
while not game_over:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
change_to = "RIGHT"
if event.key == pygame.K_UP:
change_to = "UP"
if event.key == pygame.K_DOWN:
change_to = "DOWN"
if event.key == pygame.K_LEFT:
change_to = "LEFT"
# 更新蛇的方向
if change_to == "RIGHT" and snake_direction != "LEFT":
snake_direction = "RIGHT"
if change_to == "UP" and snake_direction != "DOWN":
snake_direction = "UP"
if change_to == "DOWN" and snake_direction != "UP":
snake_direction = "DOWN"
if change_to == "LEFT" and snake_direction != "RIGHT":
snake_direction = "LEFT"
# 根据蛇的方向更新蛇的位置
if snake_direction == "RIGHT":
snake_pos[0] += snake_size
if snake_direction == "UP":
snake_pos[1] -= snake_size
if snake_direction == "DOWN":
snake_pos[1] += snake_size
if snake_direction == "LEFT":
snake_pos[0] -= snake_size
# 更新游戏窗口
window.fill(BLACK)
for pos in snake_pos:
pygame.draw.rect(window, GREEN, pygame.Rect(pos[0], pos[1], snake_size, snake_size))
# 检测蛇是否碰到自己或窗口边界,是则游戏结束
if snake_pos[0] < 0 or snake_pos[0] > width-10:
game_over = True
if snake_pos[1] < 0 or snake_pos[1] > height-10:
game_over = True
if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
score += 1
# 生成新的食物位置
food_pos = [random.randrange(1, (width//10)) * 10, random.randrange(1, (height//10)) * 10]
else:
snake_pos.pop()
# 绘制食物
pygame.draw.rect(window, WHITE, pygame.Rect(food_pos[0], food_pos[1], food_size, food_size))
# 刷新游戏窗口
pygame.display.flip()
# 控制游戏更新速度
pygame.time.Clock().tick(snake_speed)
# 游戏结束,显示分数
print("游戏结束,分数为:" + str(score))
# 退出游戏
pygame.quit()
```
这个程序实现了一个简单的贪吃蛇游戏。游戏窗口的大小为800x600,蛇的初始长度为3个方块,食物随机生成。玩家可以通过方向键来改变蛇的移动方向,当蛇吃到食物时,分数增加一点,并生成新的食物。当蛇碰到自己身体或窗口边界时,游戏结束,最终显示玩家的分数。
### 回答3:
写一个贪吃蛇游戏程序可以通过使用Python编程语言来实现。下面是一个基本的贪吃蛇游戏程序的示例:
```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("贪吃蛇游戏")
# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
red = (255, 0, 0)
# 设置蛇的起始位置和大小
snake_x = window_width / 2
snake_y = window_height / 2
snake_size = 10
snake_speed = 10
# 设置蛇的移动方向,初始向右移动
snake_x_change = 0
snake_y_change = 0
# 设置食物的位置和大小
food_x = round(random.randrange(0, window_width - snake_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - snake_size) / 10.0) * 10.0
food_size = 10
# 设置分数
score = 0
# 设置游戏结束的标志
game_over = False
# 游戏循环
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake_x_change = -snake_speed
snake_y_change = 0
elif event.key == pygame.K_RIGHT:
snake_x_change = snake_speed
snake_y_change = 0
elif event.key == pygame.K_UP:
snake_y_change = -snake_speed
snake_x_change = 0
elif event.key == pygame.K_DOWN:
snake_y_change = snake_speed
snake_x_change = 0
snake_x += snake_x_change
snake_y += snake_y_change
# 绘制窗口
window.fill(black)
pygame.draw.rect(window, green, [food_x, food_y, food_size, food_size])
pygame.draw.rect(window, white, [snake_x, snake_y, snake_size, snake_size])
# 判断蛇是否吃到了食物
if snake_x == food_x and snake_y == food_y:
score += 1
food_x = round(random.randrange(0, window_width - snake_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - snake_size) / 10.0) * 10.0
pygame.display.update()
pygame.quit()
```
这是一个简单的贪吃蛇游戏程序,它使用了pygame库来创建游戏窗口,并使用基本的游戏循环来实现游戏的运行。游戏中,蛇会不断移动,当蛇碰到边界时,游戏结束。蛇可以通过键盘的上下左右键来改变移动方向。当蛇吃到食物时,分数会增加,并重新生成一个食物。游戏窗口会持续刷新来显示游戏的画面。
阅读全文