day19 pygame和多線程

import pygame
import color
import random

游戲中的事件
1、 鼠標(biāo)相關(guān)的事件
鼠標(biāo)事件要關(guān)注事件發(fā)生的位置:event.pos
2.鍵盤事件
鍵盤事件要關(guān)注哪個鍵被按了:event.key


def main():
    pygame.init()
    window = pygame.display.set_mode((400, 600))
    pygame.display.set_caption('事件')
    window.fill((color.Color.white))

    pygame.display.flip()
    is_move = False
    while True:
        for event in pygame.event.get():
            # 這兒的event是事件對象,通過事件對象的type值來判斷事件的類型
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 鼠標(biāo)按下要做什么,就將代碼寫在這個if語句中
                print('鼠標(biāo)摁下',event.pos)
                pygame.draw.circle(window,color.Color.rand_colore(),event.pos,random.randint(10,20))
                pygame.display.update()
                is_move = True
            elif event.type == pygame.MOUSEBUTTONUP:
                print('鼠標(biāo)彈起')
                is_move = False
            elif event.type == pygame.MOUSEMOTION:
                # 鼠標(biāo)移動要做什么,就將代碼寫在這個if語句中
                if is_move:
                    pygame.draw.circle(window, color.Color.rand_colore(), event.pos, random.randint(10, 70))
                    pygame.display.update()
            if event.type ==pygame.KEYDOWN:
                print('按鍵被按下')
                print(chr(event.key))
            elif event.type == pygame.KEYUP:
                print('按鍵彈起')
                print(chr(event.key))
def main():

    pygame.init()
    window = pygame.display.set_mode((400, 600))
    pygame.display.set_caption('事件')
    window.fill((color.Color.white))
    add_btn(window)
    pygame.display.flip()
    is_move = False
    while True:
        for event in pygame.event.get():
            # 這兒的event是事件對象,通過事件對象的type值來判斷事件的類型
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 鼠標(biāo)按下要做什么,就將代碼寫在這個if語句中
                print('鼠標(biāo)摁下',event.pos)
                mx , my =event.pos
                if (100<=mx<=100+100) and (100<=my<=100+60):
                    print('add')
                is_move = True
            elif event.type == pygame.MOUSEBUTTONUP:
                print('鼠標(biāo)彈起')
                is_move = False
            elif event.type == pygame.MOUSEMOTION:
                # 鼠標(biāo)移動要做什么,就將代碼寫在這個if語句中
                if is_move:
                    pygame.draw.circle(window, color.Color.rand_colore(), event.pos, random.randint(10, 70))
                    pygame.display.update()
class Diretion:
    UP = 273
    DOWN = 274
    RIGHT = 275
    LEFT = 276
class Ball:
    def __init__(self,center_x,center_y,radius,bg_color=color.Color.rand_colore()):
        self.center_x =center_x
        self.center_y =center_y
        self.radius =radius
        self.bg_color =bg_color
        self.is_move = False
        self.direction = Diretion.DOWN
    def show(self,window):
        pygame.draw.circle(window,self.bg_color,(self.center_x,self.center_y),self.radius)

    def disappear(self,window):
        pygame.draw.circle(window,color.Color.white,(self.center_x,self.center_y),self.radius)
    def move(self,window):
        if self.direction == Diretion.DOWN:
            self.disappear(window)
            self.center_y += 1
        if self.direction == Diretion.UP:
            self.disappear(window)
            self.center_y -= 1
        if self.direction == Diretion.RIGHT:
            self.disappear(window)
            self.center_x += 1
        if self.direction == Diretion.LEFT:
            self.disappear(window)
            self.center_x -= 1
        self.show(window)

def main():
    pygame.init()
    window = pygame.display.set_mode((400, 600))
    pygame.display.set_caption('事件')
    window.fill((color.Color.white))
    ball =Ball(100,100,30)
    ball.show(window)
    pygame.display.flip()
    is_move = False
    while True:
        if ball.is_move:
            ball.move(window)
            pygame.display.update()
        for event in pygame.event.get():
            # 這兒的event是事件對象,通過事件對象的type值來判斷事件的類型
            if event.type == pygame.QUIT:
                exit()
            elif event.type ==pygame.KEYDOWN:
                if event.key ==Diretion.DOWN or event.key ==Diretion.UP or event.key ==Diretion.RIGHT or event.key ==Diretion.LEFT:
                    ball.direction =event.key
                    ball.is_move = True
            elif event.type == pygame.KEYUP:
                if event.key ==Diretion.DOWN or event.key ==Diretion.UP or event.key ==Diretion.RIGHT or event.key ==Diretion.LEFT:
                    ball.is_move = False
import time
from _datetime import datetime
import  threading

Python中永threading模塊實現(xiàn)多線程,
一個thread類就是一個線程類,需要幾個線程就創(chuàng)建幾個thread類

def download(movie):
    print('%s開始下載....'%movie,datetime.now())
    time.sleep(10)
    print('下載完成',datetime.now())

def main():
    pass
    #同時創(chuàng)建三個下載任務(wù)
    '''
    Thread(target,args)
    target:Function,需要傳一個參數(shù)(這個函數(shù)的內(nèi)容會在子線程中執(zhí)行)
    args :元組,target對應(yīng)函數(shù)的參數(shù)
    當(dāng)通過創(chuàng)建好的子線程對象調(diào)用start方法的時候,會自動在子線程中調(diào)用target對應(yīng)的函數(shù),
    并且將args中的值作為實參傳給target
    '''
    print('開始執(zhí)行')
    print(datetime.now())
    t1 =threading.Thread(target=download,args=('雇傭兵',))
    t2 =threading.Thread(target=download,args=('開國大典',))
    t3 =threading.Thread(target=download,args=('黃金國',))
    t1.start()
    t2.start()
    t3.start()
    print('sdadsadasd')
    print('===============')
    print(datetime.now())
    print('===============')
import time
from _datetime import datetime
import  threading
'''
可以通過寫一個類繼承Thread類,來創(chuàng)建屬于自己的線程類
1.聲明類繼承Thread
2.重寫run方法
3.需要線程對象的時候,創(chuàng)建當(dāng)前聲明的子類的對象;然后通過start方法在子線程中執(zhí)行run方法的任務(wù)
'''
class DownloadThread(threading.Thread):
    '''下載類'''
    def __init__(self,file):
        super().__init__()
        self.file =file
    def run(self):
        print('開始下載%s'%self.file,threading.current_thread())

def main():
    print(threading.current_thread())
    t1 = DownloadThread('黃金甲')
    # 調(diào)用start方法的時候會自動在子線程中調(diào)用run方法
    '''如果直接用對象調(diào)用run方法,run方法中的任務(wù)會在主線程執(zhí)行'''
    t1.start()
    #線程對象調(diào)用join方法,會導(dǎo)致join后的代碼會在線程中的任務(wù)結(jié)束后才執(zhí)行
    #若要判斷子線程是否全部結(jié)束,可以將各子線程放在一個子線程中后調(diào)用join方法
    t1.join()
    print('線程結(jié)束')
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • """author = drh""" if name == 'main':main() """author = d...
    LittleBear_6c91閱讀 432評論 0 1
  • 線程 操作系統(tǒng)線程理論 線程概念的引入背景 進程 之前我們已經(jīng)了解了操作系統(tǒng)中進程的概念,程序并不能單獨運行,只有...
    go以恒閱讀 1,806評論 0 6
  • ??JavaScript 與 HTML 之間的交互是通過事件實現(xiàn)的。 ??事件,就是文檔或瀏覽器窗口中發(fā)生的一些特...
    霜天曉閱讀 3,715評論 1 11
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,689評論 1 32
  • 【threading模塊詳解】 模塊基本方法 該模塊定了的方法如下:threading.active_count(...
    奕劍聽雨閱讀 1,155評論 0 0

友情鏈接更多精彩內(nèi)容