欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pygame實戰(zhàn)練習(xí)之一百層游戲

 更新時間:2021年09月24日 16:43:41   作者:顧木子吖  
跳上一百層想必是很多人童年時期的經(jīng)典游戲,我們依舊能記得抱個老人機娛樂的場景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫一個簡單的跳上一百層小游戲的相關(guān)資料,需要的朋友可以參考下

導(dǎo)語

哈嘍哈嘍!大家好!我是木木子,又到了每日游戲更新環(huán)節(jié)!

8月30日,對暑假還意猶未盡的孩子們收到了一份“開學(xué)大禮”:

圖片

通知要求,嚴格限制向未成年人提供網(wǎng)絡(luò)游戲服務(wù)的時間!啊這~禁了網(wǎng)游,那這波單機游戲的就是一波收割大勝利。

​安排!家里的孩子沒游戲玩了怎么辦?當然是由我提供新鮮熱乎的游戲代碼!

圖片

正文

給整了一個一百層的闖關(guān)游戲!能完美通關(guān)的話孩子得玩很長一段時間,那我就解放了??!

主要分為二部分:

​
import pygame
from pygame.locals import *
from sys import exit
 
FOUR_NEIGH = {"left": (0, -1), "right": (0, 1), "up": (-1, 0), "down": (1, 0)}
EIGHT_NEIGH = list(FOUR_NEIGH.values()) + [(1, 1), (1, -1), (-1, 1), (-1, -1)]
DIRECTION = {pygame.K_UP: "up", pygame.K_LEFT: "left", pygame.K_RIGHT: "right", pygame.K_DOWN: "down"}
 
 
def hex2rgb(color):
    b = color % 256
    color = color >> 8
    g = color % 256
    color = color >> 8
    r = color % 256
    return (r, g, b)
 
 
class Game(object):
    def __init__(self, title, size, fps=30):
        self.size = size
        pygame.init()
        self.screen = pygame.display.set_mode(size, 0, 32)
        pygame.display.set_caption(title)
        self.keys = {}
        self.keys_up = {}
        self.clicks = {}
        self.timer = pygame.time.Clock()
        self.fps = fps
        self.score = 0
        self.end = False
        self.fullscreen = False
        self.last_time = pygame.time.get_ticks()
        self.is_pause = False
        self.is_draw = True
        self.score_font = pygame.font.SysFont("Calibri", 130, True)
 
    def bind_key(self, key, action):
        if isinstance(key, list):
            for k in key:
                self.keys[k] = action
        elif isinstance(key, int):
            self.keys[key] = action
 
    def bind_key_up(self, key, action):
        if isinstance(key, list):
            for k in key:
                self.keys_up[k] = action
        elif isinstance(key, int):
            self.keys_up[key] = action
 
    def bind_click(self, button, action):
        self.clicks[button] = action
 
    def pause(self, key):
        self.is_pause = not self.is_pause
 
    def set_fps(self, fps):
        self.fps = fps
 
    def handle_input(self, event):
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key in self.keys.keys():
                self.keys[event.key](event.key)
            if event.key == pygame.K_F11:                           # F11全屏
                self.fullscreen = not self.fullscreen
                if self.fullscreen:
                    self.screen = pygame.display.set_mode(self.size, pygame.FULLSCREEN, 32)
                else:
                    self.screen = pygame.display.set_mode(self.size, 0, 32)
        if event.type == pygame.KEYUP:
            if event.key in self.keys_up.keys():
                self.keys_up[event.key](event.key)
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button in self.clicks.keys():
                self.clicks[event.button](*event.pos)
 
    def run(self):
        while True:
            for event in pygame.event.get():
                self.handle_input(event)
            self.timer.tick(self.fps)
 
            self.update(pygame.time.get_ticks())
            self.draw(pygame.time.get_ticks())
 
    def draw_score(self, color, rect=None):
        score = self.score_font.render(str(self.score), True, color)
        if rect is None:
            r = self.screen.get_rect()
            rect = score.get_rect(center=r.center)
        self.screen.blit(score, rect)
 
    def is_end(self):
        return self.end
 
    def update(self, current_time):
        pass
 
    def draw(self, current_time):
        pass
 
 
class Test(Game):
    def __init__(self, title, size, fps=30):
        super(Test, self).__init__(title, size, fps)
        self.bind_key(pygame.K_RETURN, self.press_enter)
 
    def press_enter(self):
        print("press enter")
 
    def draw(self, current_time):
        pass
 
 
def press_space(key):
    print("press space.")
 
 
def click(x, y):
    print(x, y)
 
 
def main():
    print(hex2rgb(0xfcf040))
    game = Test("game", (640, 480))
    game.bind_key(pygame.K_SPACE, press_space)
    game.bind_click(1, click)
    game.run()
其二:
 
import pygame
import game
from random import choice, randint
 
SCORE = 0
SOLID = 1
FRAGILE = 2
DEADLY = 3
BELT_LEFT = 4
BELT_RIGHT = 5
BODY = 6
 
GAME_ROW = 40
GAME_COL = 28
OBS_WIDTH = GAME_COL // 4
SIDE = 13
SCREEN_WIDTH = SIDE*GAME_COL
SCREEN_HEIGHT = SIDE*GAME_ROW
COLOR = {SOLID: 0x00ffff, FRAGILE: 0xff5500, DEADLY: 0xff2222, SCORE: 0xcccccc,
        BELT_LEFT: 0xffff44, BELT_RIGHT: 0xff99ff, BODY: 0x00ff00}
CHOICE = [SOLID, SOLID, SOLID, FRAGILE, FRAGILE, BELT_LEFT, BELT_RIGHT, DEADLY]
 
 
class Barrier(object):
    def __init__(self, screen, opt=None):
        self.screen = screen
        if opt is None:
            self.type = choice(CHOICE)
        else:
            self.type = opt
        self.frag_touch = False
        self.frag_time = 12
        self.score = False
        self.belt_dire = 0
        self.belt_dire = pygame.K_LEFT if self.type == BELT_LEFT else pygame.K_RIGHT
        left = randint(0, SCREEN_WIDTH - 7 * SIDE - 1)
        top = SCREEN_HEIGHT - SIDE - 1
        self.rect = pygame.Rect(left, top, 7*SIDE, SIDE)
 
    def rise(self):
        if self.frag_touch:
            self.frag_time -= 1
        if self.frag_time == 0:
            return False
        self.rect.top -= 2
        return self.rect.top >= 0
 
    def draw_side(self, x, y):
        if self.type == SOLID:
            rect = pygame.Rect(x, y, SIDE, SIDE)
            self.screen.fill(COLOR[SOLID], rect)
        elif self.type == FRAGILE:
            rect = pygame.Rect(x+2, y, SIDE-4, SIDE)
            self.screen.fill(COLOR[FRAGILE], rect)
        elif self.type == BELT_LEFT or self.type == BELT_RIGHT:
            rect = pygame.Rect(x, y, SIDE, SIDE)
            pygame.draw.circle(self.screen, COLOR[self.type], rect.center, SIDE // 2 + 1)
        elif self.type == DEADLY:
            p1 = (x + SIDE//2 + 1, y)
            p2 = (x, y + SIDE)
            p3 = (x + SIDE, y + SIDE)
            points = [p1, p2, p3]
            pygame.draw.polygon(self.screen, COLOR[DEADLY], points)
 
    def draw(self):
        for i in range(7):
            self.draw_side(i*SIDE+self.rect.left, self.rect.top)
 
 
class Hell(game.Game):
    def __init__(self, title, size, fps=60):
        super(Hell, self).__init__(title, size, fps)
        self.last = 6 * SIDE
        self.dire = 0
        self.barrier = [Barrier(self.screen, SOLID)]
        self.body = pygame.Rect(self.barrier[0].rect.center[0], 200, SIDE, SIDE)
 
        self.bind_key([pygame.K_LEFT, pygame.K_RIGHT], self.move)
        self.bind_key_up([pygame.K_LEFT, pygame.K_RIGHT], self.unmove)
        self.bind_key(pygame.K_SPACE, self.pause)
 
    def move(self, key):
        self.dire = key
 
    def unmove(self, key):
        self.dire = 0
 
    def show_end(self):
        self.draw(0, end=True)
        self.end = True
 
    def move_man(self, dire):
        if dire == 0:
            return True
        rect = self.body.copy()
        if dire == pygame.K_LEFT:
            rect.left -= 1
        else:
            rect.left += 1
        if rect.left < 0 or rect.left + SIDE >= SCREEN_WIDTH:
            return False
        for ba in self.barrier:
            if rect.colliderect(ba.rect):
                return False
        self.body = rect
        return True
 
    def get_score(self, ba):
        if self.body.top > ba.rect.top and not ba.score:
            self.score += 1
            ba.score = True
 
    def to_hell(self):
        self.body.top += 2
        for ba in self.barrier:
            if not self.body.colliderect(ba.rect):
                self.get_score(ba)
                continue
            if ba.type == DEADLY:
                self.show_end()
                return
            self.body.top = ba.rect.top - SIDE - 2
            if ba.type == FRAGILE:
                ba.frag_touch = True
            elif ba.type == BELT_LEFT or ba.type == BELT_RIGHT:
                # self.body.left += ba.belt_dire
                self.move_man(ba.belt_dire)
            break
 
        top = self.body.top
        if top < 0 or top+SIDE >= SCREEN_HEIGHT:
            self.show_end()
 
    def create_barrier(self):
        solid = list(filter(lambda ba: ba.type == SOLID, self.barrier))
        if len(solid) < 1:
            self.barrier.append(Barrier(self.screen, SOLID))
        else:
            self.barrier.append(Barrier(self.screen))
        self.last = randint(3, 5) * SIDE
 
    def update(self, current_time):
        if self.end or self.is_pause:
            return
        self.last -= 1
        if self.last == 0:
            self.create_barrier()
 
        for ba in self.barrier:
            if not ba.rise():
                if ba.type == FRAGILE and ba.rect.top > 0:
                    self.score += 1
                self.barrier.remove(ba)
 
        self.move_man(self.dire)
        self.move_man(self.dire)
        self.to_hell()
 
    def draw(self, current_time, end=False):
        if self.end or self.is_pause:
            return
        self.screen.fill(0x000000)
        self.draw_score((0x3c, 0x3c, 0x3c))
        for ba in self.barrier:
            ba.draw()
        if not end:
            self.screen.fill(COLOR[BODY], self.body)
        else:
            self.screen.fill(COLOR[DEADLY], self.body)
        pygame.display.update()
 
 
if __name__ == '__main__':
    hell = Hell("一百層", (SCREEN_WIDTH, SCREEN_HEIGHT))
    hell.run()
​

效果圖如下:

總結(jié)

我懂!我懂!我現(xiàn)在話不多說都是直接上代碼滴!

記得三連哦~mua 你們的支持是我最大的動力!

到此這篇關(guān)于Pygame實戰(zhàn)練習(xí)之一百層游戲的文章就介紹到這了,更多相關(guān)Pygame 一百層游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python reshape的用法及多個二維數(shù)組合并為三維數(shù)組的實例

    Python reshape的用法及多個二維數(shù)組合并為三維數(shù)組的實例

    今天小編就為大家分享一篇Python reshape的用法及多個二維數(shù)組合并為三維數(shù)組的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python基本知識之datetime模塊詳解

    Python基本知識之datetime模塊詳解

    這篇文章主要給大家介紹了關(guān)于Python基本知識之datetime模塊的相關(guān)資料,Python內(nèi)置的時間模塊datetime包含下面的模塊包含六個類和兩個常數(shù),提供了用于處理日期和時間的類和對應(yīng)的方法,一般用于處理年、月、日、時、分、秒的統(tǒng)計和計算等需求,需要的朋友可以參考下
    2023-08-08
  • python3 os進行嵌套操作的實例講解

    python3 os進行嵌套操作的實例講解

    在本篇文章里小編給大家整理了關(guān)于python3 os進行嵌套操作的實例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-11-11
  • python 自動軌跡繪制的實例代碼

    python 自動軌跡繪制的實例代碼

    今天小編就為大家分享一篇python 自動軌跡繪制的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python實現(xiàn)的堆排序算法原理與用法實例分析

    Python實現(xiàn)的堆排序算法原理與用法實例分析

    這篇文章主要介紹了Python實現(xiàn)的堆排序算法,簡單描述了堆排序的原理,并結(jié)合實例形式分析了Python實現(xiàn)堆排序的相關(guān)操作技巧,代碼中備有較為詳細的注釋便于理解,需要的朋友可以參考下
    2017-11-11
  • 使用python實現(xiàn)正則匹配檢索遠端FTP目錄下的文件

    使用python實現(xiàn)正則匹配檢索遠端FTP目錄下的文件

    這篇文章主要介紹了使用python實現(xiàn)正則匹配檢索遠端FTP目錄下的文件的方法,非常的簡單實用,需要的小伙伴參考下
    2015-03-03
  • 解析Python 偏函數(shù)用法全方位實現(xiàn)

    解析Python 偏函數(shù)用法全方位實現(xiàn)

    這篇文章主要介紹了解析Python 偏函數(shù)用法全方位實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • pytorch 改變tensor尺寸的實現(xiàn)

    pytorch 改變tensor尺寸的實現(xiàn)

    今天小編就為大家分享一篇pytorch 改變tensor尺寸的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Django中日期時間型字段進行年月日時分秒分組統(tǒng)計

    Django中日期時間型字段進行年月日時分秒分組統(tǒng)計

    這篇文章主要介紹了Django中日期時間型字段進行年月日時分秒分組統(tǒng)計,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python實現(xiàn)GUI學(xué)生管理系統(tǒng)的示例代碼

    Python實現(xiàn)GUI學(xué)生管理系統(tǒng)的示例代碼

    這篇文章主要為大家介紹了如何留Python語言實現(xiàn)簡易的GUI學(xué)生管理系統(tǒng),文中的示例代碼講解詳細,對我們學(xué)習(xí)Python有一定幫助,需要的可以參考下
    2022-06-06

最新評論