基於pygame實現貪吃蛇小遊戲示例
遊戲截圖
引入庫
import copy import random import sys import pygame
初始化模型
# 蛇模型 snake_list = [[10, 10]] # 食物的模型 x = random.randint(10, 490) y = random.randint(10, 490) food_point = [x, y] food_r, food_g, food_b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) food_color = pygame.Color(food_r, food_g, food_b) # 初始方向 move_up = False move_down = False move_left = False move_right = True # 初始分數 score=0 pygame.init() screen = pygame.display.set_mode((500, 500)) # 畫佈大小 title = pygame.display.set_caption('貪吃蛇') # 名字 clock = pygame.time.Clock() # 遊戲時鐘
獲取鍵盤事件
for event in pygame.event.get(): # 獲取鍵盤事件 if event.type == pygame.QUIT: running=False sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN or event.key == pygame.K_s: move_up = False move_down = True move_left = False move_right = False if event.key == pygame.K_UP or event.key == pygame.K_w: move_up = True move_down = False move_left = False move_right = False if event.key == pygame.K_LEFT or event.key == pygame.K_a: move_up = False move_down = False move_left = True move_right = False if event.key == pygame.K_RIGHT or event.key == pygame.K_d: move_up = False move_down = False move_left = False move_right = True if event.key == pygame.K_ESCAPE: # esc關閉 running=False sys.exit() if event.key ==pygame.K_SPACE and not running: running=True snake_list = [[10, 10]] score=0
移動貪吃蛇
# 身子移動 snake_len = len(snake_list) - 1 while snake_len > 0: snake_list[snake_len] = copy.deepcopy(snake_list[snake_len - 1]) snake_len -= 1 # 蛇頭移動 if move_up: snake_list[snake_len][1] -= 10 if snake_list[snake_len][1] < 0: snake_list[snake_len][1] = 500 if move_down: snake_list[snake_len][1] += 10 if snake_list[snake_len][1] > 500: snake_list[snake_len][1] = 0 if move_left: snake_list[snake_len][0] -= 10 if snake_list[snake_len][0] < 0: snake_list[snake_len][0] = 500 if move_right: snake_list[snake_len][0] += 10 if snake_list[snake_len][0] > 500: snake_list[snake_len][0] = 0
吃食物邏輯
# 蛇與食物碰撞檢測 if food_rect.collidepoint(snake_pos): snake_list.append(food_point) food_point = [random.randint(10, 490), random.randint(10, 490)] # 重新生成食物 food_r, food_g, food_b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) food_color = pygame.Color(food_r, food_g, food_b) score+=1 break
碰撞到自身邏輯
# 吃到自己 結束遊戲 snake_head_rect = snake_rect[0] # 蛇頭 count = len(snake_rect) while count > 1: if snake_head_rect.colliderect(snake_rect[count - 1]): # 檢測蛇頭與身子的所有點 running=False count -= 1
結束遊戲
if not running: show_text(screen, (20,200), 'GAME OVER!', (227, 29, 18), False, 100) show_text(screen, (120,300), 'press space to try again', (223, 223, 223), False, 30)
顯示文字函數
def show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False): cur_font = pygame.font.SysFont('宋體', font_size) cur_font.set_bold(font_bold) cur_font.set_italic(font_italic) text_fmt = cur_font.render(text, 1, color) screen.blit(text_fmt, pos)
完整代碼
import copy import random import sys import pygame def show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False): cur_font = pygame.font.SysFont('宋體', font_size) cur_font.set_bold(font_bold) cur_font.set_italic(font_italic) text_fmt = cur_font.render(text, 1, color) screen.blit(text_fmt, pos) # 蛇模型 snake_list = [[10, 10]] # 食物的模型 x = random.randint(10, 490) y = random.randint(10, 490) food_point = [x, y] food_r, food_g, food_b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) food_color = pygame.Color(food_r, food_g, food_b) # 初始方向 move_up = False move_down = False move_left = False move_right = True pygame.init() screen = pygame.display.set_mode((500, 500)) # 畫佈大小 title = pygame.display.set_caption('貪吃蛇') # 名字 clock = pygame.time.Clock() # 遊戲時鐘 running=True # 遊戲運行標志 score=0 while True: clock.tick(20) # 20fps screen.fill([255, 255, 255]) # 背景填充 for event in pygame.event.get(): # 獲取鍵盤事件 if event.type == pygame.QUIT: running=False sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN or event.key == pygame.K_s: move_up = False move_down = True move_left = False move_right = False if event.key == pygame.K_UP or event.key == pygame.K_w: move_up = True move_down = False move_left = False move_right = False if event.key == pygame.K_LEFT or event.key == pygame.K_a: move_up = False move_down = False move_left = True move_right = False if event.key == pygame.K_RIGHT or event.key == pygame.K_d: move_up = False move_down = False move_left = False move_right = True if event.key == pygame.K_ESCAPE: # esc關閉 running=False sys.exit() if event.key ==pygame.K_SPACE and not running: running=True snake_list = [[10, 10]] score=0 # 身子移動 snake_len = len(snake_list) - 1 while snake_len > 0: snake_list[snake_len] = copy.deepcopy(snake_list[snake_len - 1]) snake_len -= 1 # 蛇頭移動 if move_up: snake_list[snake_len][1] -= 10 if snake_list[snake_len][1] < 0: snake_list[snake_len][1] = 500 if move_down: snake_list[snake_len][1] += 10 if snake_list[snake_len][1] > 500: snake_list[snake_len][1] = 0 if move_left: snake_list[snake_len][0] -= 10 if snake_list[snake_len][0] < 0: snake_list[snake_len][0] = 500 if move_right: snake_list[snake_len][0] += 10 if snake_list[snake_len][0] > 500: snake_list[snake_len][0] = 0 if running: # 繪制得分 show_text(screen, (200,20), f'score: {score}', (0, 0, 0), False, 30) # 繪制食物 food_rect = pygame.draw.circle(screen, food_color, food_point, 15) # 繪制蛇 snake_rect = [] for snake_pos in snake_list: snake_rect.append(pygame.draw.circle(screen, food_color, snake_pos, 5)) # 蛇與食物碰撞檢測 if food_rect.collidepoint(snake_pos): snake_list.append(food_point) food_point = [random.randint(10, 490), random.randint(10, 490)] # 重新生成食物 food_r, food_g, food_b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) food_color = pygame.Color(food_r, food_g, food_b) score+=1 break # 吃到自己 結束遊戲 snake_head_rect = snake_rect[0] # 蛇頭 count = len(snake_rect) while count > 1: if snake_head_rect.colliderect(snake_rect[count - 1]): # 檢測蛇頭與身子的所有點 running=False count -= 1 if not running: show_text(screen, (20,200), 'GAME OVER!', (227, 29, 18), False, 100) show_text(screen, (120,300), 'press space to try again', (223, 223, 223), False, 30) pygame.display.update() # 繪制
到此這篇關於基於pygame實現貪吃蛇小遊戲示例的文章就介紹到這瞭,更多相關pygame貪吃蛇小遊戲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Python制作當年第一款手機遊戲-貪吃蛇遊戲(練習)
- python實戰之利用pygame實現貪吃蛇遊戲(二)
- python實現一個簡單的貪吃蛇遊戲附代碼
- python實現跨年表白神器–你值得擁有
- pygame實現貪吃蛇遊戲