找回密码
 中文实名注册
查看: 363|回复: 0

陈沭聿———贪吃蛇

[复制链接]

5

主题

69

帖子

2243

积分

金牌会员

Rank: 6Rank: 6

积分
2243
发表于 2023-3-19 10:31:30 | 显示全部楼层 |阅读模式
[Python] 纯文本查看 复制代码
import pygame
import sys
import random
from pygame.locals import *

pygame.init()
DISPLAY = pygame.display.set_mode((800, 800))
pygame.display.set_caption('贪吃蛇')



FPSCLOCK = pygame.time.Clock()
WHITE = pygame.Color(225, 225, 225)
COLORS =[(236,113,95),(243,168,60),(182,218,228),(128,128,128),(246,210,210),(225,225,100)]
x_1 = [300,200,100]
y_1 = [300,200,100]

snake_Head = [100, 100]
snake_Body = [[80, 100], [60, 100], [40, 100]]
direction = "right"
changeDirection = direction
food_Postion = [random.choice(x_1),random.choice(y_1)]
food_Total = 1

def drawSnake(snake_Body):
    for i in snake_Body:
        pygame.draw.rect(DISPLAY, random.choice(COLORS), Rect(i[0], i[1], 20, 20))

def drawFood(food_Postion):
    pygame.draw.rect(DISPLAY, random.choice(COLORS), Rect(
        food_Postion[0], food_Postion[1], 20, 20))
    
def gameover():
    pygame.quit()
    sys.exit()

game_flag = True
while game_flag:    
    DISPLAY.fill(WHITE)    
    drawSnake(snake_Body)    
    drawFood(food_Postion)    
    game_speed = 1+len(snake_Body)//3    
    pygame.display.flip()
    FPSCLOCK.tick(game_speed)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == KEYDOWN:
            if event.key == K_RIGHT or event.key == K_d:
                changeDirection = 'right'
            if event.key == K_LEFT or event.key == K_a:
                changeDirection = 'left'
            if event.key == K_UP or event.key == K_w:
                changeDirection = 'up'
            if event.key == K_DOWN or event.key == K_s:
                changeDirection = 'down'
            if event.key == KSCAN_ESCAPE:
                pygame.event.post(pygame.event.Event(QUIT))

    
    if changeDirection == 'right' and not direction == 'left':
        direction = changeDirection
    if changeDirection == 'left' and not direction == 'right':
        direction = changeDirection
    if changeDirection == 'up' and not direction == 'down':
        direction = changeDirection
    if changeDirection == 'down' and not direction == 'up':
        direction = changeDirection

    if direction == 'right':
        snake_Head[0] += 20
    if direction == 'left':
        snake_Head[0] -= 20
    if direction == 'up':
        snake_Head[1] -= 20
    if direction == 'down':
        snake_Head[1] += 20
    
    snake_Body.insert(0, list(snake_Head))

    if snake_Head[0] == food_Postion[0] and snake_Head[1] == food_Postion[1]:
        food_Total = 0
        
    else:
        snake_Body.pop()

    if food_Total == 0:
        x = random.randrange(1, 100)
        y = random.randrange(1, 100)
        food_Postion = [int(x * 20), int(y * 20)]
        food_Total = 1

    if snake_Head[0] > 800 or snake_Head[0] < 0:

        gameover()
    elif snake_Head[0] > 800 or snake_Head[0] < 0:
        gameover()

    for body in snake_Body[1:]:
        if snake_Head[0] == body[0] and snake_Head[1] == body[1]:
            gameover()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2024-11-21 18:19 , Processed in 0.039215 second(s), 29 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表