首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python贪吃蛇源代码

python贪吃蛇源代码

作者头像
py3study
发布2020-01-08 18:12:39
5.1K0
发布2020-01-08 18:12:39
举报
文章被收录于专栏:python3python3
import pygame, sys, random
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()

WINDOWWIDTH = 400
WINDOWHEIGHT = 400
rectLength = 18

windowSurface = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT),0,32)
pygame.display.set_caption('Snake')

BLACK = (0,0,0)
GREEN = (0,255,0)

snakeRect = []
for i in range(7,10):
    snakeRect.append(pygame.Rect(i*(rectLength+2)+1,0+1,rectLength,rectLength))
food = pygame.Rect(5*(rectLength+2),5*(rectLength+2),rectLength+2,rectLength+2)   
moveLeft = True
moveRight = False
moveUp = False
moveDown = False

direction = 1
foodImage = pygame.image.load('cherry.png')
pygame.mixer.music.load('background.mid')
pygame.mixer.music.play(-1,0.0)

pickUpSound = pygame.mixer.Sound('pickup.wav')
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT and moveRight==False:
                moveLeft = True
                moveRight = False
                moveUp = False
                moveDown = False
            if event.key == K_RIGHT and moveLeft==False:
                moveLeft = False
                moveRight = True
                moveUp = False
                moveDown = False
            if event.key == K_UP and moveDown==False:
                moveLeft = False
                moveRight = False
                moveUp = True
                moveDown = False
            if event.key == K_DOWN and moveUp==False:
                moveLeft = False
                moveRight = False
                moveUp = False
                moveDown = True
    head = pygame.Rect(snakeRect[0].left,snakeRect[0].top,snakeRect[0].width,snakeRect[0].height)
    if moveLeft == True:
        head.right = head.left-2
    if moveRight == True:
        head.left = head.right+2
    if moveUp == True:
        head.bottom = head.top-2
    if moveDown == True:
        head.top = head.bottom+2
    snakeRect.insert(0,head);
    if head.right<0 or head.left>WINDOWWIDTH or head.bottom<0 or head.top>WINDOWHEIGHT:
        break
    if food.left == snakeRect[0].left-1 and food.top == snakeRect[0].top-1:
        food.left = random.randint(0,WINDOWWIDTH/20-1)*(rectLength+2)
        food.top = random.randint(0,WINDOWHEIGHT/20-1)*(rectLength+2)
        pickUpSound.play()
    else:
         snakeRect.pop(len(snakeRect)-1)
    windowSurface.fill(BLACK)
    for i in range(len(snakeRect)):
        pygame.draw.rect(windowSurface,GREEN,snakeRect[i])
    windowSurface.blit(foodImage,food)
    if food.left == 0 and food.right == 0:
        i = 3
    pygame.display.update()
    mainClock.tick(10)    
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档