用python写个贪吃蛇小程序吧
时间: 2023-09-04 08:04:03 浏览: 109
### 回答1:
好的,这是一个简单的贪吃蛇小游戏程序的示例:
```python
import turtle
import time
# 设置窗口大小
turtle.setup(width=600, height=600)
# 创建画布
wn = turtle.Screen()
wn.title("贪吃蛇游戏")
# 创建蛇
snake = turtle.Turtle()
snake.shape("square")
snake.color("black")
snake.penup()
# 设置食物
food = turtle.Turtle()
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)
# 设置初始方向
snake.direction = "stop"
# 定义移动函数
def move():
if snake.direction == "up":
y = snake.ycor()
snake.sety(y + 20)
if snake.direction == "down":
y = snake.ycor()
snake.sety(y - 20)
if snake.direction == "left":
x = snake.xcor()
snake.setx(x - 20)
if snake.direction == "right":
x = snake.xcor()
snake.setx(x + 20)
# 定义按键响应函数
def go_up():
snake.direction = "up"
def go_down():
snake.direction = "down"
def go_left():
snake.direction = "left"
def go_right():
snake.direction = "right"
# 绑定按键
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
# 设置焦点
wn.listen()
# 游戏循环
while True:
wn.update()
# 判断是否吃到食物
if snake.distance(food) < 20:
x = snake.xcor()
y = snake.ycor()
food.goto(x, y)
# 移动
move()
# 暂停
time.sleep(0.1)
# 结束游戏
wn.mainloop()
```
请注意,这是一个简单的示例,可能需要进一步添加游戏规则、界
### 回答2:
贪吃蛇是一款经典的游戏,我们可以使用Python语言编写一个简单的贪吃蛇小程序。下面是一个示例代码:
```python
import pygame
import random
# 游戏窗口的大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# 贪吃蛇的尺寸和速度
SNAKE_SIZE = 20
SNAKE_SPEED = 10
# 初始化pygame和游戏窗口
pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("贪吃蛇游戏")
clock = pygame.time.Clock()
# 定义颜色
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 创建贪吃蛇
def create_snake():
snake = [[100, 50], [90, 50], [80, 50]]
return snake
# 创建食物
def create_food():
food = [random.randrange(1, (WINDOW_WIDTH//SNAKE_SIZE)) * SNAKE_SIZE,
random.randrange(1, (WINDOW_HEIGHT//SNAKE_SIZE)) * SNAKE_SIZE]
return food
# 游戏主循环
def game_loop():
game_over = False
game_quit = False
snake = create_snake()
food = create_food()
score = 0
direction = 'RIGHT'
change_direction = direction
while not game_quit:
while game_over == True:
# 游戏结束后的操作
window.fill(WHITE)
font = pygame.font.SysFont(None, 50)
score_text = font.render("Score: " + str(score), True, GREEN)
window.blit(score_text, [WINDOW_WIDTH / 2 - 75, WINDOW_HEIGHT / 2 - 50])
game_over_text = font.render("Game Over!", True, RED)
window.blit(game_over_text, [WINDOW_WIDTH / 2 - 100, WINDOW_HEIGHT / 2])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
change_direction = 'LEFT'
elif event.key == pygame.K_RIGHT:
change_direction = 'RIGHT'
elif event.key == pygame.K_UP:
change_direction = 'UP'
elif event.key == pygame.K_DOWN:
change_direction = 'DOWN'
# 判断方向是否相反
if change_direction == 'LEFT' and direction != 'RIGHT':
direction = change_direction
elif change_direction == 'RIGHT' and direction != 'LEFT':
direction = change_direction
elif change_direction == 'UP' and direction != 'DOWN':
direction = change_direction
elif change_direction == 'DOWN' and direction != 'UP':
direction = change_direction
# 移动贪吃蛇
if direction == 'LEFT':
snake[0][0] -= SNAKE_SPEED
elif direction == 'RIGHT':
snake[0][0] += SNAKE_SPEED
elif direction == 'UP':
snake[0][1] -= SNAKE_SPEED
elif direction == 'DOWN':
snake[0][1] += SNAKE_SPEED
# 判断是否吃到食物
if snake[0] == food:
score += 1
food = create_food()
else:
snake.pop()
# 判断游戏是否结束
if snake[0][0] < 0 or snake[0][0] >= WINDOW_WIDTH or snake[0][1] < 0 or snake[0][1] >= WINDOW_HEIGHT:
game_over = True
break
if snake[0] in snake[1:]:
game_over = True
break
# 更新游戏窗口
window.fill(WHITE)
for pos in snake:
pygame.draw.rect(window, GREEN, (pos[0], pos[1], SNAKE_SIZE, SNAKE_SIZE))
pygame.draw.rect(window, RED, (food[0], food[1], SNAKE_SIZE, SNAKE_SIZE))
pygame.display.update()
clock.tick(30)
pygame.quit()
# 运行游戏
game_loop()
```
这是一个基本的贪吃蛇游戏程序,游戏窗口的大小为800x600,贪吃蛇的尺寸和速度可以根据需要进行调整。在游戏中,玩家通过控制贪吃蛇的移动方向,尽可能地吃掉食物,并避免撞墙或撞到自己的身体。游戏结束后会显示得分和游戏结束提示。希望对你有帮助!
### 回答3:
贪吃蛇是一种经典的游戏,可以用Python来编写一个简单的贪吃蛇小程序。
首先,我们需要导入`turtle`库来实现游戏的图形界面。然后,我们定义一个`Snake`类来表示贪吃蛇,其中包括贪吃蛇的移动、吃食物、撞墙等方法。我们还需要定义一个`Food`类来表示食物,并随机生成食物的位置。
在`Snake`类的移动方法中,我们需要通过监听键盘事件来控制贪吃蛇的方向。当贪吃蛇吃到食物时,会增加身体长度,并生成新的食物。当贪吃蛇撞到墙或自己的身体时,游戏结束。
最后,我们可以在`main`函数中创建一个`Snake`实例,并在循环中不断更新贪吃蛇的位置,同时检测游戏是否结束。
下面是一个简单的贪吃蛇小程序示例代码:
```python
import turtle
import random
class Snake:
def __init__(self):
self.head = turtle.Turtle()
self.head.shape("square")
self.head.color("green")
self.head.penup()
self.direction = "stop"
def move(self):
if self.direction == "up":
self.head.sety(self.head.ycor() + 20)
elif self.direction == "down":
self.head.sety(self.head.ycor() - 20)
elif self.direction == "left":
self.head.setx(self.head.xcor() - 20)
elif self.direction == "right":
self.head.setx(self.head.xcor() + 20)
def go_up(self):
self.direction = "up"
def go_down(self):
self.direction = "down"
def go_left(self):
self.direction = "left"
def go_right(self):
self.direction = "right"
class Food:
def __init__(self):
self.food = turtle.Turtle()
self.food.shape("circle")
self.food.color("red")
self.food.penup()
self.food.goto(random.randint(-280, 280), random.randint(-280, 280))
def main():
wn = turtle.Screen()
wn.title("贪吃蛇小游戏")
wn.bgcolor("black")
wn.setup(width=600, height=600)
wn.tracer(0)
snake = Snake()
food = Food()
wn.listen()
wn.onkeypress(snake.go_up, "w")
wn.onkeypress(snake.go_down, "s")
wn.onkeypress(snake.go_left, "a")
wn.onkeypress(snake.go_right, "d")
while True:
wn.update()
snake.move()
if snake.head.distance(food.food) < 20:
food.food.goto(random.randint(-280, 280), random.randint(-280, 280))
# 贪吃蛇身体增加的代码
# 撞墙或自身的判断,游戏结束的代码
if __name__ == "__main__":
main()
```
以上是一个简单的贪吃蛇小程序的实现,可以根据需求进行改进和优化,添加更多的功能,如计分、难度调节等。
阅读全文