導語
??
“
🍺First Blood——Double Kill——Trible Kill🍻”熟悉的音效!
很早之前的游戲黑馬——貪吃蛇大作戰,借著大家對諾基亞貪吃蛇的懷念對規則進行小改變后
一次又一次喚醒了大家對它的懷舊與熱愛,不用猜就知道今天給大家介紹什么游戲了吧~
大家好,我是木木子吖,每天都給大家收集最好玩的游戲!
關注我,從此你再也不用去費力找游戲(白嫖專案原始碼哦)了,最好玩的游戲點擊即玩!
今天推薦的游戲是貪吃蛇大作戰小程式游戲哦👇
寫完這款《貪吃蛇》小游戲,下一個系列就是做一款AI版本的自動貪吃蛇啦!有時間給大家慢
慢升級,嘿嘿!

正文
一、準備中
▲游戲玩法
1、控制方向鍵走位你的小蛇,吃掉地圖上彩色的小點,就會變長哦,
2、小心!小蛇碰到界面壁的時候游戲立馬結束了哦!
3、一局定勝負,和小伙伴們比誰更長吖!
▲環境安裝
本文用到的運行環境:Python3.7、Pycharm社區版2020、Pygame游戲模塊部分自帶模塊直
接匯入不需要安裝,(如果需要安裝包軟體、激活碼或者遇到問題的話可以私信我哈!)
模塊安裝:pip install -i https://pypi.douban.com/simple/ +模塊名
二、代碼演示
import random
import pygame
import sys
from pygame.locals import *
# 螢屏大小
Window_Width = 800
Window_Height = 500
# 重繪頻率
Display_Clock = 17
# 一塊蛇身大小
Cell_Size = 20
assert Window_Width % Cell_Size == 0
assert Window_Height % Cell_Size == 0
Cell_W = int(Window_Width/Cell_Size)
Cell_H = int(Window_Height/Cell_Size)
# 背景顏色
Background_Color = (0, 0, 0)
# 關閉游戲界面
def close_game():
pygame.quit()
sys.exit()
# 檢測玩家的按鍵
def Check_PressKey():
if len(pygame.event.get(QUIT)) > 0:
close_game()
KeyUp_Events = pygame.event.get(KEYUP)
if len(KeyUp_Events) == 0:
return None
elif KeyUp_Events[0].key == K_ESCAPE:
close_game()
return KeyUp_Events[0].key
# 顯示當前得分
def Show_Score(score):
score_Content = Main_Font.render('得分:%s' % (score), True, (255, 255, 255))
score_Rect = score_Content.get_rect()
score_Rect.topleft = (Window_Width-120, 10)
Main_Display.blit(score_Content, score_Rect)
# 獲得果實位置
def Get_Apple_Location(snake_Coords):
flag = True
while flag:
apple_location = {'x': random.randint(0, Cell_W-1), 'y': random.randint(0, Cell_H-1)}
if apple_location not in snake_Coords:
flag = False
return apple_location
# 顯示果實
def Show_Apple(coord):
x = coord['x'] * Cell_Size
y = coord['y'] * Cell_Size
apple_Rect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(Main_Display, (255, 0, 0), apple_Rect)
# 顯示蛇
def Show_Snake(coords):
x = coords[0]['x'] * Cell_Size
y = coords[0]['y'] * Cell_Size
Snake_head_Rect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(Main_Display, (0, 80, 255), Snake_head_Rect)
Snake_head_Inner_Rect = pygame.Rect(x+4, y+4, Cell_Size-8, Cell_Size-8)
pygame.draw.rect(Main_Display, (0, 80, 255), Snake_head_Inner_Rect)
for coord in coords[1:]:
x = coord['x'] * Cell_Size
y = coord['y'] * Cell_Size
Snake_part_Rect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(Main_Display, (0, 155, 0), Snake_part_Rect)
Snake_part_Inner_Rect = pygame.Rect(x+4, y+4, Cell_Size-8, Cell_Size-8)
pygame.draw.rect(Main_Display, (0, 255, 0), Snake_part_Inner_Rect)
# 畫網格
def draw_Grid():
# 垂直方向
for x in range(0, Window_Width, Cell_Size):
pygame.draw.line(Main_Display, (40, 40, 40), (x, 0), (x, Window_Height))
# 水平方向
for y in range(0, Window_Height, Cell_Size):
pygame.draw.line(Main_Display, (40, 40, 40), (0, y), (Window_Width, y))
# 顯示開始界面
def Show_Start_Interface():
title_Font = pygame.font.Font('simkai.ttf', 100)
title_content = title_Font.render('貪吃蛇', True, (255, 255, 255), (0, 0, 160))
angle = 0
while True:
Main_Display.fill(Background_Color)
rotated_title = pygame.transform.rotate(title_content, angle)
rotated_title_Rect = rotated_title.get_rect()
rotated_title_Rect.center = (Window_Width/2, Window_Height/2)
Main_Display.blit(rotated_title, rotated_title_Rect)
pressKey_content = Main_Font.render('按任意鍵開始游戲!', True, (255, 255, 255))
pressKey_Rect = pressKey_content.get_rect()
pressKey_Rect.topleft = (Window_Width-200, Window_Height-30)
Main_Display.blit(pressKey_content, pressKey_Rect)
if Check_PressKey():
# 清除事件佇列
pygame.event.get()
return
pygame.display.update()
Snake_Clock.tick(Display_Clock)
angle -= 5
# 顯示結束界面
def Show_End_Interface():
title_Font = pygame.font.Font('simkai.ttf', 100)
title_game = title_Font.render('Game', True, (233, 150, 122))
title_over = title_Font.render('Over', True, (233, 150, 122))
game_Rect = title_game.get_rect()
over_Rect = title_over.get_rect()
game_Rect.midtop = (Window_Width/2, 70)
over_Rect.midtop = (Window_Width/2, game_Rect.height+70+25)
Main_Display.blit(title_game, game_Rect)
Main_Display.blit(title_over, over_Rect)
pressKey_content = Main_Font.render('按任意鍵開始游戲!', True, (255, 255, 255))
pressKey_Rect = pressKey_content.get_rect()
pressKey_Rect.topleft = (Window_Width-200, Window_Height-30)
Main_Display.blit(pressKey_content, pressKey_Rect)
pygame.display.update()
pygame.time.wait(500)
# 清除事件佇列
Check_PressKey()
while True:
if Check_PressKey():
pygame.event.get()
return
# 運行游戲
def Run_Game():
# 蛇出生地
start_x = random.randint(5, Cell_W-6)
start_y = random.randint(5, Cell_H-6)
snake_Coords = [{'x': start_x, 'y': start_y},
{'x': start_x-1, 'y': start_y},
{'x': start_x-2, 'y': start_y}]
direction = 'right'
apple_location = Get_Apple_Location(snake_Coords)
while True:
for event in pygame.event.get():
if event.type == QUIT:
close_game()
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:
close_game()
# 碰到墻壁或者自己則游戲結束
if (snake_Coords[Head_index]['x'] == -1) or (snake_Coords[Head_index]['x'] == Cell_W) or \
(snake_Coords[Head_index]['y'] == -1) or (snake_Coords[Head_index]['y'] == Cell_H):
return
if snake_Coords[Head_index] in snake_Coords[1:]:
return
if (snake_Coords[Head_index]['x'] == apple_location['x']) and (snake_Coords[Head_index]['y'] == apple_location['y']):
apple_location = Get_Apple_Location(snake_Coords)
else:
del snake_Coords[-1]
if direction == 'up':
newHead = {'x': snake_Coords[Head_index]['x'],
'y': snake_Coords[Head_index]['y']-1}
elif direction == 'down':
newHead = {'x': snake_Coords[Head_index]['x'],
'y': snake_Coords[Head_index]['y']+1}
elif direction == 'left':
newHead = {'x': snake_Coords[Head_index]['x']-1,
'y': snake_Coords[Head_index]['y']}
elif direction == 'right':
newHead = {'x': snake_Coords[Head_index]['x']+1,
'y': snake_Coords[Head_index]['y']}
snake_Coords.insert(0, newHead)
Main_Display.fill(Background_Color)
draw_Grid()
Show_Snake(snake_Coords)
Show_Apple(apple_location)
Show_Score(len(snake_Coords)-3)
pygame.display.update()
Snake_Clock.tick(Display_Clock)
# 主函式
def main():
global Main_Display, Main_Font, Snake_Clock
pygame.init()
Snake_Clock = pygame.time.Clock()
Main_Display = pygame.display.set_mode((Window_Width, Window_Height))
Main_Font = pygame.font.Font('simkai.ttf', 18)
pygame.display.set_caption('貪吃蛇大作戰——By 顧木子吖')
Show_Start_Interface()
while True:
Run_Game()
Show_End_Interface()
if __name__ == '__main__':
main()
?
三、效果展示
1)開始界面

2)游戲截圖
(控制的速度可以自己除錯的哈!給你們的原始碼我是加速了的,有速度才更好玩兒!)

3)游戲結束

總結
嘿!這個貪吃蛇比較簡單,所以視頻的話效果我就不錄制了哈!下一款《AI版本的貪吃蛇蛇
🐍》應該會錄制視頻,哈哈哈哈!需要完整的原始碼的或者之前我寫的那些文章都可以找我的
哈!都在且免費的!
原始碼基地——滴滴我即可!
滴滴我或木子主頁(pc端)自取啦!
👍往期推薦——
專案1.0 碼住雪景漫天飄雪小程式
【Python碼住雪景小程式】雪景人像最強攻略:讓你一下美10倍、美醉了(中國人不騙中國人)
專案1.1 GIF制作神奇(斗羅大陸為例)
??????【Python神器】推薦這款傻瓜式GIF制作工具,以后別再說不會了(好用到爆~)
專案1.0 超級瑪麗
程式員自制游戲:超級瑪麗100%真實版,能把你玩哭了~【附原始碼】
專案1.1 掃雷
Pygame實戰:據說這是史上最難掃雷游戲,沒有之一,你們感受下......
🎄文章匯總——
專案1.0 Python—2021 |已有文章匯總 | 持續更新,直接看這篇就夠了
(更多內容+原始碼都在文章匯總哦!!歡迎閱讀~)
?
![]()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/390498.html
標籤:其他
上一篇:1000行Python代碼實作俄羅斯方塊/掃雷/五子棋/貪吃蛇
下一篇:撲克牌游戲——C語言
