day11總結(jié)

一缘挽、pygame事件

 pygame.init()
    screen=pygame.display.set_mode((600,400))
    screen.fill((255,255,255))

設(shè)置游戲標(biāo)題
pygame.display.set_caption('游戲事件')
pygame.display.flip()

  while True:
        每次循環(huán)檢測(cè)有沒有事件發(fā)生
        for event in pygame.event.get():
            不同類型的事件對(duì)應(yīng)的type值是不同
            if event.type==pygame.QUIT:
                exit()

   鼠標(biāo)按下相關(guān)
   pos屬性湿酸,獲取鼠標(biāo)事件產(chǎn)生的位置
            if event.type==pygame.MOUSEBUTTONDOWN:
                print('鼠標(biāo)按下',event.pos)
            if event.type==pygame.MOUSEBUTTONUP:
                print('鼠標(biāo)彈起',event.pos)
            if event.type==pygame.MOUSEMOTION:
                print('鼠標(biāo)移動(dòng)',event.pos)

          #鍵盤相關(guān)事件
            #key屬性:被按的按鍵對(duì)應(yīng)的值的Asscall碼
            if event.type==pygame.KEYDOWN:
                print('鍵盤按下',chr(event.key))
            if event.type==pygame.KEYUP:
                print('鍵盤彈起',chr(event.key))

鼠標(biāo)事件的運(yùn)用


import pygame
from random import randint
def rand_color():
    """
    #產(chǎn)生隨機(jī)顏色

    :return:
    """
    return (randint(0,255),randint(0,255),randint(0,255))

def draw_ball(screen,pos):
    pygame.draw.circle(screen, rand_color(), event.pos, randint(10, 20))

    # 只要屏幕上的內(nèi)容有更新,都需要調(diào)用下面兩個(gè)方法中的一個(gè)
    # pygame.display.flip()
    pygame.display.update()

# 寫一個(gè)函數(shù)枣氧,判斷指定的點(diǎn)是否在指定的矩形范圍內(nèi)
def is_in_rect(point,rect):
    x,y=point
    rx,ry,rw,rh = rect
    if (rx<=x<=rx+rw) and (ry<=y<=ry+rh):
        return True
    return False

# 寫函數(shù)拦宣,畫按鈕
def draw_buttom(screen,bth_color,title_color):
    """矩形框"""
    pygame.draw.rect(screen, bth_color, (100, 100, 100, 60))
    """文字"""
    font = pygame.font.SysFont('Times', 30)
    title = font.render('clicke', True, title_color)
    screen.blit(title, (120, 120))




# 按鈕坐標(biāo)
if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    pygame.display.set_caption('鼠標(biāo)事件')

    #畫個(gè)按鈕
    draw_buttom(screen,(0,255,0),(255,0,0))
    # """矩形框"""
    # pygame.draw.rect(screen,(0,255,0),(100,100,100,60))
    # """文字"""
    # font=pygame.font.SysFont('Times',30)
    # title=font.render('clicke',True,(255,0,0))
    # screen.blit(title,(120,120))



    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            #退出
            if event.type==pygame.QUIT:
                exit()

            if event.type==pygame.MOUSEBUTTONDOWN:
                #
                #
                if is_in_rect(event.pos,(100,100,100,60)):
                    draw_buttom(screen,(0,100,0),(100,0,0))
                    pygame.display.update()
            if event.type==pygame.MOUSEBUTTONUP:

                if is_in_rect(event.pos,(100,100,100,60)):
                    draw_buttom(screen,(0,255,0),(255,0,0))
                    pygame.display.update()
            if event.type==pygame.MOUSEMOTION:
                screen.fill((255,255,255))
                draw_buttom(screen,(0,255,0),(255,0,0))
                draw_ball(screen,event.pos)

三截粗、要求:屏幕上顯示圖片,鼠標(biāo)按下就動(dòng)鸵隧,松開就停

import pygame

# 寫函數(shù)绸罗,判斷點(diǎn)是否在范圍內(nèi)
def is_in_rect(pos,rect):
    x,y=pos
    rx,ry,rw,rh=rect
    if (rx<=x<=rx+rw) and (ry<=y<=ry+rh):
        return True
    return False

pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))
pygame.display.set_caption('鼠標(biāo)拖動(dòng)事件')


image = pygame.image.load('./lufei.jpg')
image_x=100
image_y=100
screen.blit(image, (image_x,image_y ))
pygame.display.flip()

#用來存儲(chǔ)圖片是否可以移動(dòng)
is_move=False

while True:
    for event in pygame.event.get():
        #退出
        if event.type==pygame.QUIT:
            exit()

        # 鼠標(biāo)按下,狀態(tài)可以移動(dòng)
        if event.type==pygame.MOUSEBUTTONDOWN:
            w,h=image.get_size()
            if is_in_rect(event.pos,(image_x,image_y,w,h)):
                is_move=True
        # 鼠標(biāo)彈起,讓狀態(tài)變?yōu)椴豢蓜?dòng)
        if event.type==pygame.MOUSEBUTTONUP:
            is_move=False

            #鼠標(biāo)移動(dòng)事件
            if event.type==pygame.MOUSEMOTION:
                screen.fill((255,255,255))
                x ,y = event.pos
                image_w,image_h=image.get_size()
                # 保證鼠標(biāo)在圖片中心
                image_x=x-image_h/2
                image_y=y-image_w/2
                screen.blit(image,(image_x,image_y))
                pygame.display.update()

四豆瘫、畫球

import pygame

def draw_ball(place,color,pos):
    """
    畫球
    :param place:
    :return:
    """
    pygame.draw.circle(place,color,pos,20)

Up=273
Down=274
Left=276
Right=275


if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    pygame.display.flip()

    # 保存初始坐標(biāo)
    ball_x=100
    ball_y=100
    x_speed=1
    y_speed=0

    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()

            if event.type==pygame.KEYDOWN:
                if event.key==Up:
                    x_speed=0
                    y_speed=-1
                elif event.key==Down:
                    x_speed=0
                    y_speed=1
                elif event.key==Right:
                    x_speed = 1
                    y_speed = 0
                elif event.key==Left:
                    x_speed = -1
                    y_speed = 0

        # 刷新屏幕
        screen.fill((255,255,255))
        ball_x+=x_speed
        ball_y+=y_speed
        if ball_x+20>=600 or ball_x<=20 or ball_y+20>=400 or ball_y<=20:
            print('游戲結(jié)束')
            break
            # ball_x=600-20
            # x_speed -= 1
        #
        # if ball_x==20:
        #     x_speed+=1
        draw_ball(screen,(255,0,0),(ball_x,ball_y))
        pygame.display.update()

五珊蟀、多球一起動(dòng)


import pygame
import random
random_color=random.randint(0,255),random.randint(0,255),random.randint(0,255)

if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((600,400))
    pygame.display.flip()

    # all_balls中保存多個(gè)球,每個(gè)球要保存半徑外驱、位置育灸、圓心坐標(biāo)、顏色等信息
    all_balls=[
        {
                'r': random.randint(10,20),
                'pos':(100,100),
                'color':random_color,
                'x_speed':random.randint(-1,1),
                'y_speed':random.randint(-1,1)
        } ,
        {
            'r': random.randint(10, 20),
            'pos': (300, 300),
            'color': random_color,
            'x_speed': random.randint(-1, 1),
            'y_speed': random.randint(-1, 1)
        }
    ]
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()
            # 點(diǎn)擊畫一個(gè)
            if event.type==pygame.MOUSEBUTTONDOWN:
                ball={
                    'r': random.randint(10, 20),
                    'pos': event.pos,
                    'color': random_color,
                    'x_speed': random.randint(-1, 1),
                    'y_speed': random.randint(-1, 1)
                }

              #保存球
                all_balls.append(ball)
        #刷新界面
        pygame.time.delay(10)
        screen.fill((255,255,255))
        for ball_dict in all_balls:
            #取出球原來的x坐標(biāo)和y坐標(biāo)昵宇,以及他們的速度
            x,y=ball_dict['pos']
            x_speed=ball_dict['x_speed']
            y_speed = ball_dict['y_speed']
            x+=x_speed
            y+=y_speed
            pygame.draw.circle(screen,ball_dict['color'],(x,y),ball_dict['r'])
            #更新球?qū)?yīng)的新的坐標(biāo)
            ball_dict['pos']=x,y
        pygame.display.update()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末磅崭,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子瓦哎,更是在濱河造成了極大的恐慌砸喻,老刑警劉巖柔逼,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異恩够,居然都是意外死亡卒落,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門蜂桶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來儡毕,“玉大人,你說我怎么就攤上這事扑媚⊙澹” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵疆股,是天一觀的道長(zhǎng)费坊。 經(jīng)常有香客問我,道長(zhǎng)旬痹,這世上最難降的妖魔是什么附井? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮两残,結(jié)果婚禮上永毅,老公的妹妹穿的比我還像新娘。我一直安慰自己人弓,他們只是感情好沼死,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著崔赌,像睡著了一般意蛀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上健芭,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天县钥,我揣著相機(jī)與錄音,去河邊找鬼慈迈。 笑死魁蒜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的吩翻。 我是一名探鬼主播兜看,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼狭瞎!你這毒婦竟也來了细移?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤熊锭,失蹤者是張志新(化名)和其女友劉穎弧轧,沒想到半個(gè)月后雪侥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡精绎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年速缨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片代乃。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡旬牲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出搁吓,到底是詐尸還是另有隱情原茅,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布堕仔,位于F島的核電站擂橘,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏摩骨。R本人自食惡果不足惜通贞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望恼五。 院中可真熱鬧昌罩,春花似錦、人聲如沸唤冈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽你虹。三九已至,卻和暖如春彤避,著一層夾襖步出監(jiān)牢的瞬間傅物,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來泰國打工琉预, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留董饰,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓圆米,卻偏偏與公主長(zhǎng)得像卒暂,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子娄帖,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 01 pygame事件 02 鼠標(biāo)事件的運(yùn)用 03 鼠標(biāo)事件的應(yīng)用2 要求:先在屏幕上顯示一張圖片也祠,鼠標(biāo)按下移動(dòng)的...
    跟我念一遍閱讀 356評(píng)論 0 2
  • 1.pygame事件 QUIT:關(guān)閉按鈕被點(diǎn)擊事件MOUSEBUTTONDOWN:鼠標(biāo)按下MOUSEBUTTONU...
    曉曉的忍兒閱讀 408評(píng)論 0 11
  • 01.pygame事件 02.鼠標(biāo)事件的應(yīng)用 03.鼠標(biāo)事件的應(yīng)用2 04.動(dòng)畫效果 05.ballGame 06...
    zhazhaK丶閱讀 315評(píng)論 0 3
  • 新加坡給人的感覺是一座年輕而充滿活力的城市。如果讓我概括城市的規(guī)劃的話近速,應(yīng)該是“小而精致诈嘿,多而不亂”堪旧。 ...
    清清的水閱讀 461評(píng)論 3 1
  • 三江景色可美[鼓掌][鼓掌][鼓掌]
    寒冰0601閱讀 122評(píng)論 0 0