pygame贪吃蛇创造墙壁
时间: 2023-07-10 07:16:25 浏览: 52
要在pygame贪吃蛇中创建墙壁,可以按照以下步骤进行:
1. 定义一个列表来表示墙壁的位置,例如:
`walls = [(0, 0), (10, 0), (20, 0), ..., (590, 590)]`
这个列表表示将墙壁放置在网格中间的每个10像素的位置上,墙壁大小可以根据实际需要进行调整。
2. 在游戏循环中,在绘制蛇之前,先绘制墙壁。可以使用pygame.draw.rect()方法来绘制矩形墙壁,例如:
```
for wall in walls:
pygame.draw.rect(screen, (255, 255, 255), (wall[0], wall[1], 10, 10))
```
这个代码会在屏幕上绘制一个白色的10x10像素的矩形来表示墙壁。
3. 在蛇移动时,需要检查蛇头是否碰到了墙壁。可以通过检查蛇头坐标是否在墙壁列表中来实现,例如:
```
if snake_head in walls:
game_over = True
```
如果蛇头碰到了墙壁,游戏结束。
注意,如果墙壁与蛇身重叠,也会导致游戏结束,因此需要在绘制墙壁时避免与蛇身重叠。这可以通过在墙壁列表中排除蛇身所在的位置来实现。
相关问题
pygame贪吃蛇代码
以下是使用pygame编写的贪吃蛇游戏的简单示例代码:
```python
import pygame
import random
# 游戏窗口大小
WIDTH = 640
HEIGHT = 480
# 蛇身格子大小
GRID_SIZE = 20
# 颜色定义
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 初始化pygame
pygame.init()
# 创建游戏窗口
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")
# 定义蛇的初始位置和移动方向
snake_head = [WIDTH // 2, HEIGHT // 2]
snake_body = [[snake_head[0], snake_head[1] + GRID_SIZE],
[snake_head[0], snake_head[1] + 2 * GRID_SIZE],
[snake_head[0], snake_head[1] + 3 * GRID_SIZE]]
snake_direction = "UP"
# 定义食物的初始位置
food_pos = [random.randrange(1, WIDTH // GRID_SIZE) * GRID_SIZE,
random.randrange(1, HEIGHT // GRID_SIZE) * GRID_SIZE]
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame贪吃蛇游戏制作
以下是使用Python和Pygame制作的经典贪吃蛇游戏的示例代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 设置屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("贪吃蛇游戏")
# 定义蛇的初始位置和大小
snake_block_size = 20
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(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size])
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [screen_width / 6, screen_height / 3])
def game_loop():
game_over = False
game_close = False
# 初始化蛇的位置和移动方向
x1 = screen_width / 2
y1 = screen_height / 2
x1_change = 0
y1_change = 0
# 初始化蛇的身体
snake_List = []
Length_of_snake = 1
# 初始化食物的位置
foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
while not game_over:
while game_close:
screen.fill(BLACK)
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:
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 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
screen.fill(BLACK)
pygame.draw.rect(screen, RED, [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, screen_width - snake_block_size) / 20.0) * 20.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
Length_of_snake += 1
# 设置游戏速度
pygame.time.Clock().tick(snake_speed)
pygame.quit()
game_loop()
```
你可以将以上代码保存为一个名为`snake_game.py`的文件,并在终端中运行该文件,即可开始游戏。