日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python贪吃蛇简单代码_Python贪吃蛇简单的代码

發(fā)布時間:2023/12/31 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python贪吃蛇简单代码_Python贪吃蛇简单的代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

該樓層疑似違規(guī)已被系統(tǒng)折疊 隱藏此樓查看此樓

在自學(xué)Python的過程中在網(wǎng)上查詢資料時發(fā)現(xiàn)了一些好玩的東西,python的游戲庫模塊,它可以自己弄一個小游戲來玩玩,然后我在網(wǎng)上找了一些游戲的代碼,,自己改了一些,弄出了一個簡單貪吃蛇,代碼也是照著敲的只是稍微的改了一下

import pygame, sys, random, time

from pygame.locals import * # 從pygame模塊導(dǎo)入常用的函數(shù)和常量

# 定義顏色變量

black_colour = pygame.Color(28, 56, 20)

white_colour = pygame.Color(255, 144, 20)

red_colour = pygame.Color(255,34 , 20)

grey_colour = pygame.Color(150, 150, 150)

# 定義游戲結(jié)束函數(shù)

def GameOver(gamesurface):

# 設(shè)置提示字體的格式

GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)

# 設(shè)置提示字體的顏色

GameOver_colour = GameOver_font.render('GameOver', True, grey_colour)#只能英文

# 設(shè)置提示位置

GameOver_location = GameOver_colour.get_rect()

GameOver_location.midtop = (310, 200)

# 綁定以上設(shè)置到句柄

gamesurface.blit(GameOver_colour, GameOver_location)

# 提示運行信息

pygame.display.flip()

# 休眠5秒

time.sleep(5)

# 退出游戲

pygame.quit()

# 退出程序

sys.exit()

# 定義主函數(shù)

def main():

# 初始化pygame,為使用硬件做準(zhǔn)備

pygame.init()

pygame.time.Clock()

ftpsClock = pygame.time.Clock()

# 創(chuàng)建一個窗口

gamesurface = pygame.display.set_mode((640, 480))

# 設(shè)置窗口的標(biāo)題

pygame.display.set_caption('tanchishe snake')

# 初始化變量

# 初始化貪吃蛇的起始位置

snakeposition = [100, 100]

# 初始化貪吃蛇的長度

snakelength = [[100, 100], [80, 100], [60, 100]]

# 初始化目標(biāo)方塊的位置

square_purpose = [300, 300]

# 初始化一個數(shù)來判斷目標(biāo)方塊是否存在

square_position = 1

# 初始化方向,用來使貪吃蛇移動

derection = "right"

change_derection = derection

# 進行游戲主循環(huán)

while True:

# 檢測按鍵等pygame事件

for event in pygame.event.get():

if event.type == QUIT:

# 接收到退出事件后,退出程序

pygame.quit()

sys.exit()

elif event.type == KEYDOWN:

# 判斷鍵盤事件,用w,s,a,d來表示上下左右

if event.key == K_RIGHT or event.key == ord('d'):

change_derection = "right"

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

change_derection = "left"

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

change_derection = "up"

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

change_derection = "down"

if event.key == K_ESCAPE:

pygame.event.post(pygame.event.Event(QUIT))

# 判斷移動的方向是否相反

if change_derection == 'left' and not derection == 'right':

derection = change_derection

if change_derection == 'right' and not derection == 'left':

derection = change_derection

if change_derection == 'up' and not derection == 'down':

derection = change_derection

if change_derection == 'down' and not derection == 'up':

derection = change_derection

# 根據(jù)方向,改變坐標(biāo)

if derection == 'left':

snakeposition[0] -= 20

if derection == 'right':

snakeposition[0] += 20

if derection == 'up':

snakeposition[1] -= 20

if derection == 'down':

snakeposition[1] += 20

# 增加蛇的長度

snakelength.insert(0, list(snakeposition))

# 判斷是否吃掉目標(biāo)方塊

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

square_position = 0

else:

snakelength.pop()

# 重新生成目標(biāo)方塊

if square_position == 0:

# 隨機生成x,y,擴大二十倍,在窗口范圍內(nèi)

x = random.randrange(1, 32)

y = random.randrange(1, 24)

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

square_position = 1

# 繪制pygame顯示層

gamesurface.fill(black_colour)

for position in snakelength:

pygame.draw.rect(gamesurface, white_colour, Rect(position[0], position[1], 20, 20))

pygame.draw.rect(gamesurface, red_colour, Rect(square_purpose[0], square_purpose[1], 20, 20))

# 刷新pygame顯示層

pygame.display.flip()

# 判斷是否死亡

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

GameOver(gamesurface)

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

GameOver(gamesurface)

for snakebody in snakelength[1:]:

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

GameOver(gamesurface)

# 控制游戲速度

ftpsClock.tick(8)

if __name__ == "__main__":

main()

總結(jié)

以上是生活随笔為你收集整理的python贪吃蛇简单代码_Python贪吃蛇简单的代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。