close

#貪食蛇
"""
SnakeBite - by PyMike
Released to the Public Domain
Created and documented on September 2nd, 2008 in 50 minutes.
"""

import os, random
import pygame
from pygame.locals import *
範圍= range
輸出= print
設標題= pygame.display.set_caption
設大小= pygame.display.set_mode
啟動= pygame.init
字型類=pygame.font.Font
事件取得= pygame.event.get
結束=pygame.quit
畫面預覽=pygame.display.flip
時間等待= pygame.time.wait

class 蛇(object):

    #產生一隻蛇
    def __init__(自己):
        自己.位置 = [160, 304] #頭的位置
        自己.身體 = []        #身體位置
        自己.長度 = 3         #長度
        自己.角度 = 0        #面向
        自己.存活 = True     #是否存活

    #更新畫面
    def 更新(自己):

        #產生一個新的位置
        自己.身體.insert(0, list(自己.位置))

        #確認身體長度沒超過陣列
        自己.身體 = 自己.身體[0:自己.長度]

        #移動
        if 自己.角度 == 0:
            自己.位置[1] -= 16
        if 自己.角度 == 90:
            自己.位置[0] -= 16
        if 自己.角度 == 180:
            自己.位置[1] += 16
        if 自己.角度 == 270:
            自己.位置[0] += 16

        #頭跟身體是否碰到
        for b in 自己.身體:
            if 自己.位置 == b:
                自己.存活 = False

        #是否撞牆
        if 自己.位置[0] not in range(320):
            自己.存活 = False
        if 自己.位置[1] not in range(320):
            自己.存活 = False

    #I mussssst be vissssible
    def 畫(自己, 資料):

        #畫頭
        資料.fill((0, 0, 255), (自己.位置[0], 自己.位置[1], 16, 16))

        #畫身體
        for b in 自己.身體:
            資料.fill((0, 0, 255), (b[0], b[1], 16, 16))

#產生食物
class 食物(object):

    #產生
    def __init__(自己):
        自己.換位置() #產生一個位置

    #隨機換位置
    def 換位置(自己):
        自己.位置 = [random.randrange(1, 18)*16, random.randrange(1, 18)*16]

    #輸出食物
    def 畫(自己, 資料):
        資料.fill((255, 0, 0), (自己.位置[0], 自己.位置[1], 16, 16))

#Main function
def main():
    
    #Call the SDL arg to center the window when it's inited, and then init pygame
    os.environ["SDL_VIDEO_CENTERED"] = "1"
    啟動()

    #產生視窗
    設標題("貪食蛇")
    畫面 = 設大小((320, 320))

    #產生物件
    小蛇 = 蛇()
    小食物 = 食物()
    font = 字型類(None, 32)

    while 1:

        #Get the key input from pygame's event module
        for e in 事件取得():

            #QUIT is the big red X button on the window bar
            if e.type == QUIT:
                結束()
                return

            #Check if a key was pressed
            if e.type == KEYDOWN:

                #Quit if the Escape key is pressed
                if e.key == K_ESCAPE:
                    結束()
                    return

                #切換蛇的角度
                if e.key == K_UP:
                    小蛇.角度 = 0
                if e.key == K_LEFT:
                    小蛇.角度 = 90
                if e.key == K_DOWN:
                    小蛇.角度 = 180
                if e.key == K_RIGHT:
                    小蛇.角度 = 270

        #更新
        小蛇.更新()

        #吃到食物 變長
        if 小蛇.位置 == 小食物.位置:
            小食物.換位置()
            小蛇.長度 += 1

        #死亡時發生的事
        if not 小蛇.存活:
            結束文字 = font.render("YOU LOSE", 1, (0, 0, 0))
            畫面.blit(結束文字, (160-結束文字.get_width()/2, 160-結束文字.get_height()/2))
            畫面預覽()
            時間等待(1000)
            小蛇 = 蛇()
            小食物 = 食物()

        #輸出畫面
        畫面.fill((255, 255, 255))
        小蛇.畫(畫面)
        小食物.畫(畫面)
        分數 = font.render("SCORE: %d" % (小蛇.長度-3), 1, (0, 0, 0))
        畫面.blit(分數, (5, 5))
        畫面預覽()

        #刷新
        時間等待(100)

#Run if executeed
if __name__ == "__main__":
    main()

以上是程式碼----------------------------------

 
以上是YOUTUBE---------------------------------------------

 

arrow
arrow
    全站熱搜

    dinocff10 發表在 痞客邦 留言(0) 人氣()