99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁大數(shù)據(jù)時代一起來用 Python 做個是男人就堅持100秒游戲
一起來用 Python 做個是男人就堅持100秒游戲
2021-11-16
收藏
一起來用 Python 做個是男人就堅持100秒游戲

作者:某某白米飯

來源:Python 技術(shù)

相信大家在初中電腦課上都偷偷玩過 Flash 游戲--是男人就堅持 100 秒,在游戲中無數(shù)的小球隨機(jī)運(yùn)動,玩家用鼠標(biāo)控制大球,當(dāng)大球碰撞到小球后,游戲結(jié)束,顯示堅持的時間。今天我們一起來開發(fā)這個小游戲吧。

一起來用 Python 做個是男人就堅持100秒游戲

步驟分布:

  1. 設(shè)置屏幕大小和標(biāo)題
  2. 小球繪制、移動
  3. 大球繪制、鼠標(biāo)控制大球
  4. 大球碰撞到小球后游戲結(jié)束

設(shè)置屏幕大小和標(biāo)題

import pygame W = 600 H = 500  # 初始化pygame模塊 pygame.init() # 設(shè)置窗口大小 screen = pygame.display.set_mode((W,H)) # 設(shè)置窗口標(biāo)題 pygame.display.set_caption('是男人就堅持100秒') 

繪制小球、移動

小球是圓形的,圓的半徑?jīng)Q定了小球的大小并且在小球移動的時它的 X 坐標(biāo)和 Y 坐標(biāo)一直時在變動的,所以設(shè)置 X 坐標(biāo)、Y 坐標(biāo)、X 方向移動速度、Y 方向移動速度變量。小球每次移動的坐標(biāo)都是 X 坐標(biāo) + X 方向移動速度、Y 坐標(biāo) + Y 方向移動速度。time.sleep(0.001) 可以調(diào)整小球的移動的時間,時間約大移動越慢。當(dāng)小球碰到左右邊界的時候需要調(diào)整 X 方向移動速度、Y 方向移動速度為負(fù)的

class Ball: x = None # x坐標(biāo) y = None # y坐標(biāo) speed_x = None # x方向移動的速度 speed_y = None # y方向移動的速度 radius = None # 小半徑 color = None # 顏色 def __init__(self, x, y, speed_x, speed_y, radius, color): """
        初始化
        :param x: X坐標(biāo)
        :param y: Y坐標(biāo)
        :param speed_x: X軸方向速度
        :param speed_y: Y軸方向速度
        :param radius: 半徑
        :param color: 顏色
        """ self.x = x
        self.y = y
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.radius = radius
        self.color = color def draw(self, screen): """
        繪制小球
        :param screen: 窗口
        :return:
        """ pygame.draw.circle(screen, self.color, [self.x, self.y], self.radius) def move(self, screen): """
        小球移動
        :param screen: 窗口
        :return:
        """ self.x += self.speed_x
        self.y += self.speed_y # 左右邊界 if self.x > W - self.radius or self.x < self.radius:
            self.speed_x = -self.speed_y # 上下邊界 if self.y > H - self.radius or self.y < self.radius:
            self.vy = -self.vy # 移動頻率 time.sleep(0.001)
        
        self.draw(screen)

balls = [] def create_ball(screen): """
    創(chuàng)建小球
    :param screen:
    :return:
    """ x = random.randint(0, W)
    y = random.randint(0, H)
    speed_x = random.randint(-5, 5)
    speed_y = random.randint(-5, 5)
    r = 3 color = 'white' b = Ball(x, y, speed_x, speed_y, r, color)
    
    balls.append(b)
    
    b.draw(screen)

大球的繪制和鼠標(biāo)控制大球

大球主要的屬性有半徑、顏色,移動的速度和方向都是跟隨鼠標(biāo)運(yùn)動的,捕獲鼠標(biāo)的位置設(shè)置大球的 X、Y 坐標(biāo)

class Player: radius = None color = None x = 1000 y = 1000 def __init__(self, radius, color): """
        初始化
        :param radius: 半徑
        :param color: 顏色
        """ self.radius = radius
        self.color = color def move(self, screen): """
        大球移動
        :return:
        """ # 鼠標(biāo)檢測 if pygame.mouse.get_focused(): # 獲取光標(biāo)位置, x, y = pygame.mouse.get_pos()

            mouse = pygame.mouse.get_pressed()

            pygame.draw.circle(screen, self.color, [x, y], self.radius)
            self.x = x
            self.y = y

大球碰撞到小球后游戲結(jié)束

當(dāng)大球碰撞到小球后游戲就結(jié)束了,計算大球的坐標(biāo)減去小球的坐標(biāo)小于兩球的半徑之和就表示它們碰撞了

# 小球每次移動后計算碰撞結(jié)果 for ball in balls: ball.move(screen) if abs(p.x - ball.x) < 13 and abs(p.y - ball.y) < 13: is_loop = False #結(jié)束程序循環(huán)標(biāo)志 break 

顯示 GAME OVER 字樣和游戲的時間

def show_text(screen, text, pos, color, font_bold=False, font_size=18, font_italic=False): """
    顯示文字
    :param screen: 窗口
    :param text: 文字
    :param pos: 坐標(biāo)
    :param color: 顏色
    :param font_bold: 是否粗體
    :param font_size: 大小
    :param font_italic: 是否斜體
    :return:
    """ cur_font = pygame.font.SysFont('Courier', 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)

show_text(screen, "Game over!", (120, 180), "green", True, 60)
show_text(screen, text_time, (220, 270), "green", True, 30)

游戲效果

一起來用 Python 做個是男人就堅持100秒游戲

總結(jié)

本文使用了 Python 是實現(xiàn)了一個簡單的是男人就堅持 100 秒的小游戲,有興趣的小伙伴可以對游戲進(jìn)一步擴(kuò)展,比如過幾秒鐘加幾個小球等等。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }