Python 贪吃蛇小游戏
生活随笔
收集整理的這篇文章主要介紹了
Python 贪吃蛇小游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
初學python,跟著各種教程寫了個貪吃蛇小游戲。
部分代碼借鑒了以下視頻中的內容:
【智源學院】Python實戰案例 — 貪吃蛇 (pygame)_嗶哩嗶哩_bilibili
以下為代碼:
# 初始框架 import pygame import randomclass Point: # 將每個小方格定義為類似于坐標的形式col = 0row = 0def __init__(self, row, col):self.row = rowself.col = coldef copy(self):return Point(row=self.row, col=self.col)# 初始化 pygame.init() clock = pygame.time.Clock() W = 800 # 游戲部分窗口寬度 W_W = 1000 # 窗口總寬度 H = 600 ROW = 30 # 小方格行數 COL = 40 # 小方格列數 size = (W_W, H) window = pygame.display.set_mode(size) # 窗口大小 pygame.display.set_caption('貪吃蛇') game_continue = True # 控制開始和暫停的變量 dead = False # 控制是否游戲結束的變量 score = 0 # 計分# 定義坐標和顏色 head = Point(row=int(ROW / 2), col=int(COL / 2)) # 初始時蛇頭位置 head_color = (0, 128, 128) # 蛇頭顏色 bg_color = (255, 255, 255) # 背景顏色 bg_color_right = (0, 200, 200) # 右側顏色 snake_color = (200, 200, 200) # 蛇身顏色 font_color = (0, 0, 0) # 字體顏色 direct = 'left' # 開始時向左出發 snakes = [] # 蛇身坐標以鏈表形式存儲# 生成食物的函數 def gen_food():while True:pos = Point(row=random.randint(0, ROW - 1), col=random.randint(0, COL - 1))is_coll = False# 是否跟蛇頭撞上了if head.row == pos.row and head.col == pos.col:is_coll = True# 是否跟蛇身撞上了for snake_body in snakes:if snake_body.row == pos.row and snake_body.col == pos.col:is_coll = Truebreakif not is_coll:breakreturn pos# 在指定位置生成指定顏色的格子的函數 def rect(point, color):cell_width = W / COLcell_height = H / ROWleft = point.col * cell_width # 格子左上點的x坐標top = point.row * cell_height # ypygame.draw.rect(window, color, (left, top, cell_width, cell_height))# 開局前將已生成的蛇頭坐標作為存儲蛇身的鏈表的第一項 snakes.insert(0, head.copy()) # 開局前生成食物 food = gen_food() food_color = (255, 255, 0)# 游戲循環 game_quit = False while not game_quit:# 處理玩家輸入的操作for event in pygame.event.get():# print(event) # 需要調試測試時取消注釋,即可輸出所有檢測到的eventif event.type == pygame.QUIT:game_quit = Trueelif event.type == pygame.KEYDOWN:if event.key == 119 or event.key == 1073741906:if direct == 'left' or direct == 'right': # 只有蛇處于往左或往右運動的狀態時才允許讓蛇往上走direct = 'up'elif event.key == 115 or event.key == 1073741905:if direct == 'left' or direct == 'right':direct = 'down'elif event.key == 97 or event.key == 1073741904:if direct == 'up' or direct == 'down':direct = 'left'elif event.key == 100 or event.key == 1073741903:if direct == 'up' or direct == 'down':direct = 'right'elif event.key == 32: # 按空格暫停游戲game_continue = not game_continueelif event.type == pygame.MOUSEBUTTONDOWN:m_x = event.pos[0] # 鼠標位置x坐標m_y = event.pos[1]if 840 < m_x < 960 and 264 < m_y < 336: # 鼠標點擊繼續時繼續游戲game_continue = Trueelif 840 < m_x < 960 and 376 < m_y < 448: # 鼠標點擊暫停時暫停游戲game_continue = Falseelif 840 < m_x < 960 and 488 < m_y < 560: # 鼠標點擊退出時退出游戲game_quit = Trueif game_continue and not dead: # 處于游戲繼續狀態并且沒有結束時進行各種判斷及移動# 未暫停時蛇頭移動if direct == 'left':head.col -= 1elif direct == 'right':head.col += 1elif direct == 'up':head.row -= 1elif direct == 'down':head.row += 1# 檢測是否游戲結束# 1.撞墻if head.col < 0 or head.row < 0 or head.col >= COL or head.row >= ROW:dead = True# 2.撞自己for snake in snakes:if head.col == snake.col and head.row == snake.row:dead = Truebreak# 判斷是否吃到東西eat = head.row == food.row and head.col == food.col# 吃到東西的話加一分并重新產生食物if eat:score += 1food = gen_food()# 處理蛇身移動顯示# 1.把蛇頭坐標插入到鏈表snakes的頭部snakes.insert(0, head.copy())# 2.若沒吃到東西就把鏈表最后一項刪除if not eat:snakes.pop()# 渲染# 背景及顯示pygame.draw.rect(window, bg_color, (0, 0, W, H)) # 左側白色背景pygame.draw.rect(window, bg_color_right, (W, 0, 200, H)) # 右側藍色背景pygame.draw.rect(window, bg_color, (W + 40, 40, 120, 72)) # 五個白色矩形pygame.draw.rect(window, bg_color, (W + 40, 152, 120, 72))pygame.draw.rect(window, bg_color, (W + 40, 264, 120, 72))pygame.draw.rect(window, bg_color, (W + 40, 376, 120, 72))pygame.draw.rect(window, bg_color, (W + 40, 488, 120, 72))font_name = pygame.font.match_font('MicrosoftYaHei', 40) # 獲取字體文件font = pygame.font.Font(font_name, 25) # 設置方格內顯示文字的字體text_begin = font.render('繼續', True, font_color)text_rect = text_begin.get_rect(center=(900, 300))window.blit(text_begin, text_rect)text_begin = font.render('暫停', True, font_color)text_rect = text_begin.get_rect(center=(900, 412))window.blit(text_begin, text_rect)text_begin = font.render('退出', True, font_color)text_rect = text_begin.get_rect(center=(900, 524))window.blit(text_begin, text_rect)score_text = str(score) # 將分數轉換為str型變量text_begin = font.render(score_text, True, font_color)text_rect = text_begin.get_rect(center=(900, 76))window.blit(text_begin, text_rect)if not game_continue: # 顯示游戲已暫停text_begin = font.render('已暫停', True, font_color)text_rect = text_begin.get_rect(center=(900, 188))window.blit(text_begin, text_rect)if dead:text_begin = font.render('游戲結束', True, font_color)text_rect = text_begin.get_rect(center=(900, 188))window.blit(text_begin, text_rect)if not dead: # 使游戲結束后清空屏幕# 食物rect(food, food_color)# 蛇身for snake in snakes:rect(snake, snake_color)# 蛇頭rect(head, head_color)# 更新顯示pygame.display.flip()# 設置幀頻clock.tick(10)總結
以上是生活随笔為你收集整理的Python 贪吃蛇小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab小波包分析,小波分析及小波包
- 下一篇: 电影推荐系统 python简书_电影推荐