python贪吃蛇完整代码

大家好,今天我们要解答,python贪吃蛇代码和运行结果 python贪吃蛇完整代码不用库,现在让我们一起来看看吧!

今天我分享一个代码,贪吃蛇的代码,废话不多说,直接上原码

import pygame, sys

from pygame.locals import *

()

fpsClock = .Clock()

WINDOW = pygame.display.set_mode((400, 300))

pygame.display.set_caption('贪吃蛇')

BLACK = pygame.Color(0, 0, 0)

WHITE = pygame.Color(255, 255, 255)

RED = pygame.Color(255, 0, 0)

snake_position = [100, 50]

snake_body = [[100, 50], [90, 50], [80, 50]]

food_position = [300, 150]

food_spawn = True

direction = 'RIGHT'

change_to = direction

def game_over():

()

()

while True:

for event in ():

if == QUIT:

game_over()

elif == KEYDOWN:

if == K_RIGHT or == ord('d'):

change_to = 'RIGHT'

if == K_LEFT or == ord('a'):

change_to = 'LEFT'

if == K_UP or == ord('w'):

change_to = 'UP'

if == K_DOWN or == ord('s'):

change_to = 'DOWN'

if == K_ESCAPE:

(pygame.event.Event(QUIT))

if change_to == 'RIGHT' and not direction == 'LEFT':

direction = 'RIGHT'

if change_to == 'LEFT' and not direction == 'RIGHT':

direction = 'LEFT'

if change_to == 'UP' and not direction == 'DOWN':

direction = 'UP'

if change_to == 'DOWN' and not direction == 'UP':

direction = 'DOWN'

if direction == 'RIGHT':

snake_position[0] += 10

if direction == 'LEFT':

snake_position[0] -= 10

if direction == 'UP':

snake_position[1] -= 10

if direction == 'DOWN':

snake_position[1] += 10

snake_body.insert(0, list(snake_position))

if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:

food_spawn = False

else:

()

if not food_spawn:

food_position = [pygame.randint(0, 39) * 10, pygame.randint(0, 29) * 10]

food_spawn = True

(BLACK)

for pos in snake_body:

(WINDOW, RED, (

pos[0], pos[1], 10, 10))

(WINDOW, WHITE, (

food_position[0], food_position[1], 10, 10))

if snake_position[0] >= 400 or snake_position[0] < 0:

game_over()

if snake_position[1] >= 300 or snake_position[1] < 0:

game_over()

for block in snake_body[1:]:

if snake_position[0] == block[0] and snake_position[1] == block[1]:

game_over()

pygame.display.update()

(12)

 
原文地址1:https://blog.youkuaiyun.com/2401_82970449/article/details/136121222
参考资料:python中用turtle画一个圆形 https://blog.youkuaiyun.com/SXIAOYAN_/article/details/140061099

以下是一个简单的贪吃蛇代码示例,使用 Python 语言编写: ```python import pygame import random # 初始化 Pygame pygame.init() # 设置游戏界面尺寸、背景颜色、标题 screen = pygame.display.set_mode((600, 600)) pygame.display.set_caption('贪吃蛇') # 定义颜色 BLACK = pygame.Color(0, 0, 0) WHITE = pygame.Color(255, 255, 255) RED = pygame.Color(255, 0, 0) GREEN = pygame.Color(0, 255, 0) # 定义游戏结束函数 def game_over(): font = pygame.font.SysFont(None, 48) text = font.render('游戏结束', True, RED) text_rect = text.get_rect() text_rect.centerx = screen.get_rect().centerx text_rect.centery = screen.get_rect().centery - 24 screen.blit(text, text_rect) pygame.display.flip() pygame.time.delay(3000) pygame.quit() sys.exit() # 定义主函数 def main(): # 初始化贪吃蛇和食物 snake_positions = [(200, 200), (210, 200), (220, 200)] snake_direction = 'right' food_position = (random.randint(0, 590), random.randint(0, 590)) food_exist = True # 设置游戏帧率 clock = pygame.time.Clock() # 开始游戏循环 while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT and snake_direction != 'right': snake_direction = 'left' elif event.key == pygame.K_RIGHT and snake_direction != 'left': snake_direction = 'right' elif event.key == pygame.K_UP and snake_direction != 'down': snake_direction = 'up' elif event.key == pygame.K_DOWN and snake_direction != 'up': snake_direction = 'down' # 移动贪吃蛇 if snake_direction == 'right': new_head = (snake_positions[0][0] + 10, snake_positions[0][1]) elif snake_direction == 'left': new_head = (snake_positions[0][0] - 10, snake_positions[0][1]) elif snake_direction == 'up': new_head = (snake_positions[0][0], snake_positions[0][1] - 10) elif snake_direction == 'down': new_head = (snake_positions[0][0], snake_positions[0][1] + 10) snake_positions.insert(0, new_head) # 判断贪吃蛇是否吃到食物 if snake_positions[0] == food_position: food_exist = False else: snake_positions.pop() # 重新生成食物 if not food_exist: food_position = (random.randint(0, 590), random.randint(0, 590)) food_exist = True # 绘制游戏界面 screen.fill(BLACK) for position in snake_positions: pygame.draw.rect(screen, GREEN, pygame.Rect(position[0], position[1], 10, 10)) pygame.draw.rect(screen, WHITE, pygame.Rect(food_position[0], food_position[1], 10, 10)) # 判断贪吃蛇是否死亡 if snake_positions[0][0] < 0 or snake_positions[0][0] > 590 or snake_positions[0][1] < 0 or snake_positions[0][1] > 590: game_over() for position in snake_positions[1:]: if snake_positions[0] == position: game_over() # 更新游戏界面 pygame.display.update() # 控制游戏帧率 clock.tick(10) # 启动游戏 if __name__ == '__main__': main() ``` 这是一个基本的贪吃蛇代码示例,可以实现贪吃蛇的基本功能。当然,你可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值