import pygame
import color
import random
游戲中的事件
1小渊、 鼠標(biāo)相關(guān)的事件
鼠標(biāo)事件要關(guān)注事件發(fā)生的位置:event.pos
2.鍵盤(pán)事件
鍵盤(pán)事件要關(guān)注哪個(gè)鍵被按了: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是事件對(duì)象,通過(guò)事件對(duì)象的type值來(lái)判斷事件的類型
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠標(biāo)按下要做什么凑阶,就將代碼寫(xiě)在這個(gè)if語(yǔ)句中
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)移動(dòng)要做什么栗菜,就將代碼寫(xiě)在這個(gè)if語(yǔ)句中
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是事件對(duì)象,通過(guò)事件對(duì)象的type值來(lái)判斷事件的類型
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠標(biāo)按下要做什么,就將代碼寫(xiě)在這個(gè)if語(yǔ)句中
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)移動(dòng)要做什么庇茫,就將代碼寫(xiě)在這個(gè)if語(yǔ)句中
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是事件對(duì)象,通過(guò)事件對(duì)象的type值來(lái)判斷事件的類型
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模塊實(shí)現(xiàn)多線程螃成,
一個(gè)thread類就是一個(gè)線程類旦签,需要幾個(gè)線程就創(chuàng)建幾個(gè)thread類
def download(movie):
print('%s開(kāi)始下載....'%movie,datetime.now())
time.sleep(10)
print('下載完成',datetime.now())
def main():
pass
#同時(shí)創(chuàng)建三個(gè)下載任務(wù)
'''
Thread(target,args)
target:Function,需要傳一個(gè)參數(shù)(這個(gè)函數(shù)的內(nèi)容會(huì)在子線程中執(zhí)行)
args :元組寸宏,target對(duì)應(yīng)函數(shù)的參數(shù)
當(dāng)通過(guò)創(chuàng)建好的子線程對(duì)象調(diào)用start方法的時(shí)候宁炫,會(huì)自動(dòng)在子線程中調(diào)用target對(duì)應(yīng)的函數(shù),
并且將args中的值作為實(shí)參傳給target
'''
print('開(kāi)始執(zhí)行')
print(datetime.now())
t1 =threading.Thread(target=download,args=('雇傭兵',))
t2 =threading.Thread(target=download,args=('開(kāi)國(guó)大典',))
t3 =threading.Thread(target=download,args=('黃金國(guó)',))
t1.start()
t2.start()
t3.start()
print('sdadsadasd')
print('===============')
print(datetime.now())
print('===============')
import time
from _datetime import datetime
import threading
'''
可以通過(guò)寫(xiě)一個(gè)類繼承Thread類氮凝,來(lái)創(chuàng)建屬于自己的線程類
1.聲明類繼承Thread
2.重寫(xiě)run方法
3.需要線程對(duì)象的時(shí)候羔巢,創(chuàng)建當(dāng)前聲明的子類的對(duì)象;然后通過(guò)start方法在子線程中執(zhí)行run方法的任務(wù)
'''
class DownloadThread(threading.Thread):
'''下載類'''
def __init__(self,file):
super().__init__()
self.file =file
def run(self):
print('開(kāi)始下載%s'%self.file,threading.current_thread())
def main():
print(threading.current_thread())
t1 = DownloadThread('黃金甲')
# 調(diào)用start方法的時(shí)候會(huì)自動(dòng)在子線程中調(diào)用run方法
'''如果直接用對(duì)象調(diào)用run方法,run方法中的任務(wù)會(huì)在主線程執(zhí)行'''
t1.start()
#線程對(duì)象調(diào)用join方法竿秆,會(huì)導(dǎo)致join后的代碼會(huì)在線程中的任務(wù)結(jié)束后才執(zhí)行
#若要判斷子線程是否全部結(jié)束启摄,可以將各子線程放在一個(gè)子線程中后調(diào)用join方法
t1.join()
print('線程結(jié)束')