Day19
一、 事件
import pygame
import color
import random
游戲中的事件
1、 鼠標相關的事件
鼠標事件要關注事件發生的位置:event.pos ---- 鼠標的坐標,元組
2、 鍵盤相關的事件
鍵盤事件要關注哪個鍵被按了: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:
# 鼠標按下要做什么,就將代碼寫在這個if語句中
print('鼠標摁下',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('鼠標彈起')
is_move = False
elif event.type == pygame.MOUSEMOTION:
# 鼠標移動要做什么,就將代碼寫在這個if語句中
if is_move:
print('鼠標移動')
pass
# 鍵盤事件
if event.type == pygame.KEYDOWN:
print('按鍵被按下')
print(event.key, chr(event.key))
elif event.type == pygame.KEYUP:
print('按鍵彈起')
if __name__ == '__main__':
main()
二、 按鈕
import pygame
import color
import random
class Button:
def __init__(self, x, y, width,height,text,background_color,txt_color):
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.background_color = background_color
self.txt_color = txt_color
def add_btn(window):
pygame.draw.rect(window, color.Color.gray, (100, 100, 100, 60))
font = pygame.font.SysFont('Times', 30)
text = font.render('add', True, color.Color.yellow)
w, h = text.get_size()
x = 100 / 2 - w / 2 + 100
y = 60 / 2 - h / 2 + 100
window.blit(text, (x, y))
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():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mx, my = event.pos
if (100 <= mx <= 100 + 100) and (100 <= my <= 100 + 60):
print('add')
if __name__ == '__main__':
main()
三、 移動
import pygame
import color
import random
class Direction:
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.move_direction = Direction.DOWN
def disappear(self, window):
pygame.draw.circle(window,color.Color.white, (self.center_x, self.center_y), self.radius)
def show(self, window):
pygame.draw.circle(window, self.bg_color, (self.center_x, self.center_y), self.radius)
def move(self, window):
self.disappear(window)
if self.move_direction == Direction.DOWN:
self.center_y += 1
elif self.move_direction == Direction.UP:
self.center_y -= 1
elif self.move_direction == Direction.RIGHT:
self.center_x += 1
else:
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()
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 == Direction.DOWN or event.key == Direction.UP or event.key == Direction.RIGHT or event.key == Direction.LEFT :
ball.move_direction = event.key
ball.is_move = True
elif event.type == pygame.KEYUP:
if event.key == Direction.DOWN or event.key == Direction.UP or event.key == Direction.RIGHT or event.key == Direction.LEFT :
ball.is_move = False
if __name__ == '__main__':
main()
四、 線程-耗時操作
import time
from datetime import datetime
# python多線程技術對應的模塊
import threading
默認情況下,一個進程有且只有一個線程,這個線程叫主線程
threading 模塊中的Thread類就是線程類,這個類的對象就是線程對象,一個線程對象對應一個子線程
需要一個子線程就創建一個Thread類的對象
def download(file):
print('%s開始下載' % file, datetime.now())
# sleep(時間) --- 程序執行到這個位置等待指定的時候再接著往后面執行
time.sleep(3)
print('%s下載結束' % file, datetime.now())
def main():
print('程序開始')
print(datetime.now())
# 1、 在主線程中下載
download('qwerty')
# 2、 在三個子線程中同時下載三個
"""
hread(target, args) --- 創建子線程對象
targeet --- Function,需要傳一個函數
args --- tuple,target對應的函數的參數
當通過創建好的子線程對象調用start方法的時候,會自動在子線程中調用target對應的函數,
并且將args中值作為實參。
"""
t1 = threading.Thread(target=download, args=('湮滅',))
t2 = threading.Thread(target=download, args=('海王',))
t3 = threading.Thread(target=download, args=('死侍',))
# 開始執行t1對應的子線程中的任務(實質就是在子線程中調用target對應的函數)
t1.start()
t2.start()
t3.start()
print(datetime.now())
if __name__ == '__main__':
main()
五、 Thread子類
import threading
import time as time1
from datetime import time
import requests
可以通過寫一個類繼承Thread類,來創建屬于自己的線程類
1、 聲明類繼承Thread
2、 重寫run方法。這個方法中的人任務就是需要在子線程中執行的任務
3、 需要線程對象的時候,創建當前聲明的類的對象;然后通過start方法在子線程中去執行run方法中的任務
class DownloadThread(threading.Thread):
"""下載類"""
def __init__(self, file):
super().__init__()
self.file = file
def run(self):
print('開始下載:'+ self.file)
print('run:',threading.current_thread())
print('run方法中的代碼')
def main():
print(threading.current_thread())
# 注意:如果直接用對象調用run方法,run方法中的任務會在主線程執行
# t1.run()
# 調用start的時候會自動在子線程中調用run方法
t1 = DownloadThread('湮滅')
t2 = DownloadThread('死侍')
t1.start()
t2.start()
if __name__ == '__main__':
main()
六、 join方法
import threading
import time as time1
from datetime import time
import requests
class DownloadThread(threading.Thread):
"""下載類"""
def __init__(self, file):
super().__init__()
self.file = file
def run(self):
print('開始下載:' + self.file)
print('run:', threading.current_thread())
print('run方法中的代碼')
class Credit_thread:
t1 = DownloadThread('湮滅')
t2 = DownloadThread('死侍')
t1.start()
t2.start()
# 線程對象調用join方法,會導致join后的代碼會在線程中的任務結束后才執行
t1.join()
t1.join()
print('下載結束')
def main():
t0 = threading
if __name__ == '__main__':
main()