1. 程式人生 > 實用技巧 >Python學習實踐一

Python學習實踐一

貪吃蛇小遊戲

pycharm——pygame

game.py

import pygame  # 導包
from game_items import *  # 導類和變數

class Game(object):  # 這裡的object類,意為頂級/基礎類。
    def __init__(self):
        self.main_window = pygame.display.set_mode((640, 480))
        self.main_name = pygame.display.set_caption('貪吃蛇')

        self.score_label 
= Label() # 得分標籤 self.tip_label = Label(24, False) # 暫停&遊戲結束的標籤 self.is_game_over = True # 遊戲是否結束 self.is_pause = False # 遊戲是否暫停 self.food = Food() self.snake = Snake() def start(self): clock = pygame.time.Clock() # 遊戲時鐘 while
True: self.main_window.fill(BACKGROUND_COLOR) # 事件監聽 for event in pygame.event.get(): if event.type == pygame.QUIT: # 退出 return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE:
return elif event.key == pygame.K_SPACE: if self.is_game_over: self.reset_game() else: self.is_pause = not self.is_pause if not self.is_pause and not self.is_game_over: if event.type == FOOD_UPDATE_EVENT: self.food.random_rect() elif event.type == SNAKE_UPDATE_EVENT: self.is_game_over = not self.snake.update() elif event.type == pygame.KEYDOWN: if event.key in (pygame.K_RIGHT, pygame.K_DOWN, pygame.K_LEFT, pygame.K_UP): self.snake.change_dir(event.key) # 繪製得分標籤 self.score_label.draw('分數: %d' % self.snake.score, self.main_window) # 繪製暫停&遊戲結束標籤 if self.is_game_over: self.tip_label.draw('遊戲結束,按空格從新開始遊戲。', self.main_window) elif self.is_pause: self.tip_label.draw('遊戲暫停,按空格鍵繼續。', self.main_window) # 更新顯示內容(不能缺少) else: if self.snake.has_eat(self.food): self.food.random_rect() self.food.draw(self.main_window) self.snake.draw(self.main_window) pygame.display.update() clock.tick(60) # 重新整理頻率(幀數) def reset_game(self): """重置遊戲分數""" self.is_pause = False self.is_game_over = False self.snake.reset_snake() self.food.random_rect() if __name__ == '__main__': pygame.init() # 初始化pygame模組 #遊戲程式碼 Game().start() pygame.quit() # 釋放pygame模組

game_items.py

import pygame
import random

BACKGROUND_COLOR = (232, 232, 232)
SCORE_TEXT_COLOR = (192, 192, 192)
TIP_TEXT_COLOR = (64, 64, 64)
SCREEN_RECT = pygame.Rect(0, 0, 640, 480)
RECT_SIZE = 20
FOOD_UPDATE_EVENT = pygame.USEREVENT  # 食物更新事件
SNAKE_UPDATE_EVENT = pygame.USEREVENT + 1  # 蛇更新事件

class Label(object):
    def __init__(self, size=48, is_score=True):
        """
        標籤類
        :param is_score 表示是否顯示分數
        """
        self.fond = pygame.font.SysFont('simhei', size)  # 返回一個fond物件
        self.is_score = is_score

    def draw(self, text, window):
        # 渲染字型
        color =SCORE_TEXT_COLOR if self.is_score else TIP_TEXT_COLOR
        text_surface = self.fond.render(text, True, color)  # self.fond 就是__init__內的物件屬性 self.fond
        # 獲取文字的矩形 一
        # text_rect = text_surface.get_rect()  # 憑藉get_rect獲取文字矩陣區域
        # window_rect = window.get_rect()
        # text_rect.y = window_rect.height - text_rect.height
        # text_rect.x = window_rect.width - text_rect.width
        # 獲取文字的矩形 二
        text_rect = text_surface.get_rect()
        window_rect = window.get_rect()
        if self.is_score:
            text_rect.bottomleft = window_rect.bottomleft  # mid+方位/top+方位/bottom+方位
        else:
            text_rect.center = window_rect.center
        # 繪製文字內容到視窗
        window.blit(text_surface, text_rect)

class Food(object):
    def __init__(self):
        self.color = (255, 0, 0)
        self.score = 10
        self.rect = (0, 0, RECT_SIZE, RECT_SIZE)  # 食物位置,座標以及寬高

        self.random_rect()

    def draw(self, window):
        pygame.draw.rect(window, self.color, self.rect)

        if self.rect.w < RECT_SIZE:  # 食物放大
            self.rect.inflate_ip(2, 2)

    def random_rect(self):
        col = SCREEN_RECT.w / RECT_SIZE - 1
        row = SCREEN_RECT.h / RECT_SIZE - 1

        x = random.randint(0, col) * RECT_SIZE
        y = random.randint(0, row) * RECT_SIZE

        self.rect = pygame.Rect(x, y, RECT_SIZE, RECT_SIZE)

        self.rect.inflate_ip(-RECT_SIZE, -RECT_SIZE)  # 寬高修改為零

        pygame.time.set_timer(FOOD_UPDATE_EVENT, 30000)

class Snake(object):
    def __init__(self):
        """初始化資料"""
        self.dir = pygame.K_RIGHT
        self.score = 0
        self.time_interval = 500
        self.color = (64, 64, 64)
        self.body_list = []

        self.reset_snake()

    def reset_snake(self):
        self.dir = pygame.K_RIGHT
        self.score = 0
        self.time_interval = 500

        self.body_list.clear()
        for _ in range(3):
            self.add_node()

    def add_node(self):
        if self.body_list:
            head = self.body_list[0].copy()
        else:
            head = pygame.Rect(-RECT_SIZE, 0, RECT_SIZE, RECT_SIZE)

        if self.dir == pygame.K_LEFT:
            head.x -= 20
        elif self.dir == pygame.K_RIGHT:
            head.x += 20
        elif self.dir == pygame.K_UP:
            head.y -= 20
        elif self.dir == pygame.K_DOWN:
            head.y += 20

        self.body_list.insert(0, head)

        pygame.time.set_timer(SNAKE_UPDATE_EVENT, self.time_interval)

    def draw(self, window):
        for idx, rect in enumerate(self.body_list):
            # 這裡idx如果是頭部 idx==0 為True 邊框為1;如果是False就預設實心
            # 由於初始化方向向右,所以生成的格子是向右三個
            pygame.draw.rect(window, self.color, rect.inflate(-2, -2), idx == 0)
    # 看似格子在移動,實際是此消彼長,蛇並沒有移動

    def update(self):
        # 備份上一步
        body_list_copy = self.body_list.copy()
        # 移動身體
        self.add_node()
        self.body_list.pop()  # 預設刪除最後一個
        # 是否死亡
        if self.is_dead():
            self.body_list = body_list_copy
            return False

        return True


    def change_dir(self, to_dir):
        hor_dirs = (pygame.K_LEFT, pygame.K_RIGHT)
        var_dirs = (pygame.K_UP, pygame.K_DOWN)
        if ((self.dir in hor_dirs and to_dir not in hor_dirs) or
                (self.dir in var_dirs and to_dir not in var_dirs)):

            self.dir = to_dir

    def has_eat(self, food):
        if self.body_list[0].contains(food.rect):
            self.score += food.score
            if self.time_interval > 100:
                self.time_interval -= 50
            self.add_node()

            return True
        return False

    def is_dead(self):
        head = self.body_list[0]

        if not SCREEN_RECT.contains(head):
            return True

        for body in self.body_list[1:]:
            if head.contains(body):
                return True
        return False

主要學習pygame內的操作

視窗操作、繪製文字、渲染影象、擺放位置

事件的監聽、畫面的重新整理與顯示、反饋操作

存在不足,請多指教