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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python贪吃蛇源码下载_Python贪吃蛇源代码

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

Python貪吃蛇源代碼

Python代碼狂人 Python代碼大全

程序運行截圖如下:

import pygame as pg

from random import randint

import sys

from pygame.locals import *

FPS = 6 # 畫面幀數,代表蛇的移動速率

window_width = 600

window_height = 500

cellsize = 20

cell_width = int(window_width / cellsize)

cell_height = int(window_height / cellsize)

BGcolor = (0, 0, 0)

BLUE = (0, 0, 255)

RED = (255, 0, 0)

apple_color = (255, 0, 0)

snake_color = (0, 150, 0)

GREEN = (0, 255, 0)

WHITE = (255, 255, 255)

DARKGRAY = (40, 40, 40)

UP = "up"

DOWN = "down"

LEFT = "left"

RIGHT = "right"

HEAD = 0

def main(): # 有函數

global FPSclock, window, BASICFONT

pg.init()

FPSclock = pg.time.Clock()

window = pg.display.set_mode((window_width, window_height))

BASICFONT = pg.font.Font("freesansbold.ttf", 18)

pg.display.set_caption("貪吃蛇")

showStartScreen()

while True:

runGame()

showGameOverScreen()

def runGame(): # 運行游戲函數

startx = randint(5, cell_width - 6)

starty = randint(5, cell_height - 6)

snakeCoords = [{"x": startx, "y": starty}, {"x": startx - 1, "y": starty}, {"x": startx - 2, "y": starty}]

direction = RIGHT

apple = getRandomLocation()

while True:

for event in pg.event.get():

if event.type == QUIT:

terminate()

elif event.type == KEYDOWN:

if event.key == K_LEFT and direction != RIGHT:

direction = LEFT

elif event.key == K_RIGHT and direction != LEFT:

direction = RIGHT

elif event.key == K_UP and direction != DOWN:

direction = UP

elif event.key == K_DOWN and direction != UP:

direction = DOWN

elif event.key == K_ESCAPE:

terminate()

if snakeCoords[HEAD]["x"] == -1 or snakeCoords[HEAD]["x"] == cell_width or snakeCoords[HEAD]["y"] == -1 or \

snakeCoords[HEAD]["y"] == cell_height:

return

for snakeBody in snakeCoords[1:]:

if snakeBody["x"] == snakeCoords[HEAD]["x"] and snakeBody["y"] == snakeCoords[HEAD]["y"]:

return

if snakeCoords[HEAD]["x"] == apple["x"] and snakeCoords[HEAD]["y"] == apple["y"]:

apple = getRandomLocation()

else:

del snakeCoords[-1]

if direction == UP:

newHead = {"x": snakeCoords[HEAD]["x"], "y": snakeCoords[HEAD]["y"] - 1}

elif direction == DOWN:

newHead = {"x": snakeCoords[HEAD]["x"], "y": snakeCoords[HEAD]["y"] + 1}

elif direction == LEFT:

newHead = {"x": snakeCoords[HEAD]["x"] - 1, "y": snakeCoords[HEAD]["y"]}

elif direction == RIGHT:

newHead = {"x": snakeCoords[HEAD]["x"] + 1, "y": snakeCoords[HEAD]["y"]}

snakeCoords.insert(0, newHead)

window.fill(BGcolor)

drawGrid()

drawSnake(snakeCoords)

drawApple(apple)

drawScore(len(snakeCoords) - 3)

pg.display.update()

FPSclock.tick(FPS)

def drawPressKeyMsg(): # 游戲開始提示信息

pressKeySurf = BASICFONT.render("press a key to play", True, BLUE)

pressKeyRect = pressKeySurf.get_rect()

pressKeyRect.topleft = (window_width - 200, window_height - 30)

window.blit(pressKeySurf, pressKeyRect)

def checkForKeyPress(): # 檢查是否觸發按鍵

if len(pg.event.get(QUIT)) > 0:

terminate()

keyUpEvents = pg.event.get(KEYUP)

if len(keyUpEvents) == 0:

return None

if keyUpEvents[0].key == K_ESCAPE:

terminate()

return keyUpEvents[0].key

def showStartScreen(): # 開始畫面

window.fill(BGcolor)

titleFont = pg.font.Font("freesansbold.ttf", 100)

titleSurf = titleFont.render("snake!", True, RED)

titleRect = titleSurf.get_rect()

titleRect.center = (window_width / 2, window_height / 2)

window.blit(titleSurf, titleRect)

drawPressKeyMsg()

pg.display.update()

while True:

if checkForKeyPress():

pg.event.get()

return

def terminate(): # 退出

pg.quit()

sys.exit()

def getRandomLocation(): # 出現位置

return {"x": randint(0, cell_width - 1), "y": randint(0, cell_height - 1)}

def showGameOverScreen(): # 游戲結束

gameOverFont = pg.font.Font("freesansbold.tff", 150)

gameSurf = gameOverFont.render("Game", True, WHITE)

overSurf = gameOverFont.render("over", True, WHITE)

gameRect = gameSurf.get_rect()

overRect = overSurf.get_rect()

gameRect.midtop = (window_width / 2, 10)

overRect.midtop = (window_width / 2, gameRect.height10 + 25)

window.blit(gameSurf, gameRect)

window.blit(overSurf, overRect)

drawPressKeyMsg()

pg.display.update()

pg.time.wait(500)

checkForKeyPress()

while True:

if checkForKeyPress():

pg.event.get()

return

def drawScore(score): # 顯示分數

scoreSurf = BASICFONT.render("Score:%s" % (score), True, WHITE)

scoreRect = scoreSurf.get_rect()

scoreRect.topleft = (window_width - 120, 10)

window.blit(scoreSurf, scoreRect)

def drawSnake(snakeCoords): # 畫蛇

for coord in snakeCoords:

x = coord["x"] * cellsize

y = coord["y"] * cellsize

snakeSegmentRect = pg.Rect(x, y, cellsize, cellsize)

pg.draw.rect(window, snake_color, snakeSegmentRect)

snakeInnerSegmentRect = pg.Rect(x + 4, y + 4, cellsize - 8, cellsize - 8)

pg.draw.rect(window, GREEN, snakeInnerSegmentRect)

def drawApple(coord):

x = coord["x"] * cellsize

y = coord["y"] * cellsize

appleRect = pg.Rect(x, y, cellsize, cellsize)

pg.draw.rect(window, apple_color, appleRect)

def drawGrid(): # 畫方格

for x in range(0, window_width, cellsize):

pg.draw.line(window, DARKGRAY, (x, 0), (x, window_height))

for y in range(0, window_height, cellsize):

pg.draw.line(window, DARKGRAY, (0, y), (window_width, y))

if __name__ == "__main__":

main()

程序調試過程中遇到問題,歡迎在本文后留言。

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。