猫史档案馆


【Python作品分享】贪吃蛇(1)【作品秀】

用户:au空间au空间查看:0 回复:1 评论:0 创建时间:2021-06-12T15:21:10


【作品展示】

center_image

 

【作品介绍】

D

 

【作品源代码】

import pygame,random,sys

pygame.init()
screen = pygame.display.set_mode((喵0, 480))
direction = 'right' # 
changeDirection = direction
clock = pygame.time.Clock()
snakePosition = [100, 100] # 
snakeSements = [[100,100], [80, 100], [60, 100]] # 
rPosition = [300,300] # 
rSpawned = 1 # 
#dayiwan=pygame.image.load("大伊万.jpg")
while True:
    # 
    for i in range(20,喵0,20):
        pygame.draw.line(screen,(0,200,100),(i,0),(i,480))
    for i in range(20,480,20):
        pygame.draw.line(screen,(0,200,100),(0,i),(喵0,i))
    
    # 
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif e.type == pygame.KEYDOWN:
            if e.key  == pygame.K_RIGHT:
                changeDirection = 'right'
                
            if e.key  == pygame.K_LEFT:
                changeDirection = 'left'
                
            if e.key  == pygame.K_UP:
                changeDirection = 'up'
                
            if e.key  == pygame.K_DOWN:
                changeDirection = 'down'
                
                
    # 
    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 == 'up':
        snakePosition[1] = snakePosition[1] - 20
    if direction == "down":
        snakePosition[1] += 20
    if direction == "left":
        snakePosition[0] -= 20
    if direction == "right":
        snakePosition[0] += 20

    # 
    snakeSements.insert(0,list(snakePosition))
    if snakePosition[0] == rPosition[0] and snakePosition[1] == rPosition[1]:
        # 
        rSpawned = 0
    else:
        # 
        snakeSements.pop()
    #
    if rSpawned == 0:
        rPosition = [random.randint(1,31) * 20, random.randint(1,23) * 20]
        rSpawned = 1
    screen.fill((0,0,0))
    #
    for position in snakeSements:
        pygame.draw.rect(screen,(255,255,255),(position[0],position[1],20,20))
    #
    if snakePosition[0]>620 or snakePosition[1]>480 or snakePosition[0]<0 or snakePosition[0]<0:
        screen.blit(dayiwan,(0,0))
    #
    pygame.draw.rect(screen,(131,68,31),(rPosition[0],rPosition[1],20,20))
    print(rSpawned)
    print(snakeSements)
    clock.tick(10)
    pygame.display.update()
    
















                

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
Python-happyyyyPython-happyyyy

大伊万可还行

点赞0


评论