python實現貪吃蛇遊戲

2021-10-03 11:30:51 字數 3784 閱讀 5243

python實現貪吃蛇遊戲

pip install pygame
具體實現**:

#!/usr/bin/env python

import pygame,sys,time,random

from pygame.locals import *

# 定義顏色變數

redcolour = pygame.color(255,0,0)

blackcolour = pygame.color(0,0,0)

whitecolour = pygame.color(255,255,255)

greycolour = pygame.color(150,150,150)

# 定義gameover函式

def gameover(playsu***ce):

gameoverfont = pygame.font.font('arial.ttf',72)

gameoversurf = gameoverfont.render('game over', true, greycolour)

gameoverrect = gameoversurf.get_rect()

gameoverrect.midtop = (320, 10)

playsu***ce.blit(gameoversurf, gameoverrect)

pygame.display.flip()

time.sleep(5)

pygame.quit()

sys.exit()

# 定義main函式

def main():

# 初始化pygame

pygame.init()

fpsclock = pygame.time.clock()

# 建立pygame顯示層

playsu***ce = pygame.display.set_mode((640,480))

pygame.display.set_caption('raspberry snake')

# 初始化變數

snakeposition = [100,100]

snakesegments = [[100,100],[80,100],[60,100]]

raspberryposition = [300,300]

raspberryspawned = 1

direction = 'right'

changedirection = direction

while true:

# 檢測例如按鍵等pygame事件

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 == ord('d'):

changedirection = 'right'

if event.key == k_left or event.key == ord('a'):

changedirection = 'left'

if event.key == k_up or event.key == ord('w'):

changedirection = 'up'

if event.key == k_down or event.key == ord('s'):

changedirection = 'down'

if event.key == k_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':

snakeposition[0] += 20

if direction == 'left':

snakeposition[0] -= 20

if direction == 'up':

snakeposition[1] -= 20

if direction == 'down':

snakeposition[1] += 20

# 增加蛇的長度

snakesegments.insert(0,list(snakeposition))

# 判斷是否吃掉了樹莓

if snakeposition[0] == raspberryposition[0] and snakeposition[1] == raspberryposition[1]:

raspberryspawned = 0

else:

snakesegments.pop()

# 如果吃掉樹莓,則重新生成樹莓

if raspberryspawned == 0:

x = random.randrange(1,32)

y = random.randrange(1,24)

raspberryposition = [int(x*20),int(y*20)]

raspberryspawned = 1

# 繪製pygame顯示層

playsu***ce.fill(blackcolour)

for position in snakesegments:

pygame.draw.rect(playsu***ce,whitecolour,rect(position[0],position[1],20,20))

pygame.draw.rect(playsu***ce,redcolour,rect(raspberryposition[0], raspberryposition[1],20,20))

# 重新整理pygame顯示層

pygame.display.flip()

# 判斷是否死亡

if snakeposition[0] > 620 or snakeposition[0] < 0:

gameover(playsu***ce)

if snakeposition[1] > 460 or snakeposition[1] < 0:

for snakebody in snakesegments[1:]:

if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:

gameover(playsu***ce)

# 控制遊戲速度

fpsclock.tick(5)

if __name__ == "__main__":

main()

Python 貪吃蛇遊戲

coding utf 8 引入turtle庫,製作snake,food的動畫 from turtle import 引入random庫,使food隨機出現 from random import randrange 引入freegames庫,這個庫中封裝了許多元素 我們可以使用正方形元素表示food,...

Python 遊戲 貪吃蛇

貪吃蛇是個非常簡單的遊戲,適合練手。先來看一下我的遊戲截圖 玩法介紹 回車鍵 開始遊戲 空格鍵 暫停 繼續 方向鍵 或 wsad 鍵 控制移動方向。食物分紅 綠 藍三種,分別對應 10 分 20 分 30 分,每吃乙個食物增加對應分值,每增加 100 分速度加快一級,沒有設定關卡,我玩到 1100 ...

貪吃蛇遊戲

貪吃蛇遊戲 結構化程式設計 c語言程式設計 重要的的是結構化的程式設計思想 include include include include include include define field width 300 就做20個格仔的 define field height 300 define f...