一個(gè)網(wǎng)站的圖片一會(huì)用
https://new.qq.com/omn/20190117/20190117A0P6J0.html?pgv_ref=aio2015&ptlang=2052
"""author = drh"""
import pygame
from color import Color
from random import randint
class Button:
def __init__(self, x, y, width, height, text='', background_color=Color.red, text_color=Color.white):
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.background_color = background_color
self.text_color = text_color
self.font_size = 30
def show(self, window):
pygame.draw.rect(window, self.background_color, (self.x, self.y, self.width, self.height))
font = pygame.font.SysFont('Times', self.font_size)
text = font.render(self.text, True, self.text_color)
w, h = text.get_size()
x = self.width / 2 - w / 2 + self.x
y = self.height / 2 - h / 2 + self.y
window.blit(text, (x, y))
def is_cliecked(self, pos):
x, y = pos
return (self.x <= x <= self.x + self.width) and (self.y <= y <= self.y + self.height)
def main():
pygame.init()
window = pygame.display.set_mode((400, 600))
pygame.display.set_caption('事件')
window.fill(Color.white)
# add_btn(window)
add_btn = Button(100, 100, 100, 50, 'del')
add_btn.show(window)
btn2 = Button(100, 250, 100, 60, 'Score', background_color=Color.yellow, text_color=Color.black)
btn2.show(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 add_btn.is_cliecked(event.pos):
print('刪除!')
continue
if btn2.is_cliecked(event.pos):
# print('hello')
btn2.text = str(randint(0, 100))
btn2.show(window)
pygame.display.update()
continue
if name == 'main':
main()
"""author = drh"""
import pygame
from color import Color
from random import randint
window_width = 400
window_height = 600
class Direction:
"""方向類(lèi)"""
UP = 273
DOWN = 274
RIGHT = 275
LEFT = 276
class Ball:
def __init__(self, center_x, center_y, radius, bg_color=Color.random_color()):
self.center_x = center_x
self.center_y = center_y
self.radius = radius
self.bg_color = bg_color
self.is_move = True # 是否移動(dòng)
self.move_direction = Direction.DOWN
self.speed = 5
def disappear(self, window):
"""
球從指定界面消失
:param window:
:return:
"""
pygame.draw.circle(window, Color.white, (self.center_x, self.center_y), self.radius)
def show(self, window):
"""
小球顯示
:param window:
:return:
"""
pygame.draw.circle(window, self.bg_color, (self.center_x, self.center_y), self.radius)
def move(self, window):
"""
小球移動(dòng)
:param window:
:return:
"""
# 讓移動(dòng)前的球消失
self.disappear(window)
if self.move_direction == Direction.DOWN:
self.center_y += self.speed
elif self.move_direction == Direction.UP:
self.center_y -= self.speed
elif self.move_direction == Direction.LEFT:
self.center_x -= self.speed
else:
self.center_x += self.speed
# 移動(dòng)后重新顯示球
self.show(window)
@classmethod
def creat_enemy_ball(cls):
r = randint(10, 25)
x = randint(r, int(window_width - r))
y = randint(r, int(window_height - r))
enemy = cls(x, y, r, Color.random_color())
enemy.is_move = False
return enemy
def main():
pygame.init()
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('事件')
window.fill(Color.white)
# 先顯示一個(gè)的球
ball = Ball(100, 100, 30)
ball.show(window)
pygame.display.flip()
# 計(jì)時(shí)
time = 0
# 所有被吃的球
all_enemy = []
while True:
time += 1
# 每隔100個(gè)運(yùn)行單位移動(dòng)一次
if time % 100 == 0:
if ball.is_move:
# 讓球動(dòng)起來(lái)
ball.move(window)
pygame.display.update()
if time == 10000:
time = 0
enemy = Ball.creat_enemy_ball()
all_enemy.append(enemy)
enemy.show(window)
for event in pygame.event.get():
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.LEFT or event.key == Direction.RIGHT:
# ball.is_move = True
ball.move_direction = event.key
elif event.type == pygame.KEYUP:
if event.key == Direction.DOWN or event.key == Direction.UP or event.key == Direction.LEFT or event.key == Direction.RIGHT:
# ball.is_move = False
pass
if name == 'main':
main()
"""author =drh"""
import pygame
from color import Color
from random import randint
window_height = 600
window_width = 400
class Ball:
def __init__(self, center_x, center_y, radius, bg_color=Color.random_color()):
self.center_x = center_x
self.center_y = center_y
self.radius = radius
self.bg_color = bg_color
self.is_move = True
self.y_speed = 6
def move(self, window):
self.disapper(window)
new_y = self.center_y + self.y_speed
if new_y >= window_height - self.radius:
new_y = window_height - self.radius
self.y_speed *= -1
if new_y <= self.radius:
new_y = self.radius
self.y_speed *= -1
self.center_y = new_y
self.show(window)
def disapper(self, window):
pygame.draw.circle(window, 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 main():
pygame.init()
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('事件')
window.fill(Color.white)
ball = Ball(100, 100, 20)
ball.show(window)
pygame.display.flip()
time = 0
while True:
time += 1
if time % 1000 ==0:
if ball.is_move:
ball.move(window)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if name == 'main':
main()
"""author = drh"""
import time
from datetime import datetime
# python多線(xiàn)程技術(shù)對(duì)應(yīng)的模塊
import threading
"""
默認(rèn)情況下反粥,一個(gè)進(jìn)程有且只有一個(gè)線(xiàn)程漓藕,這個(gè)線(xiàn)程叫主線(xiàn)程
threading模塊中的Thread類(lèi)就是線(xiàn)程類(lèi),這個(gè)類(lèi)的對(duì)象就是線(xiàn)程對(duì)象活烙,一個(gè)線(xiàn)程對(duì)象對(duì)應(yīng)一個(gè)子線(xiàn)程碉纺。
(需要一個(gè)子線(xiàn)程就創(chuàng)建一個(gè)Thread類(lèi)的對(duì)象)
"""
def download(file):
print('%s開(kāi)始下載' % file, datetime.now())
# sleep(時(shí)間) - 程序執(zhí)行到這個(gè)位置等待指定的時(shí)候再接著往后面執(zhí)行
time.sleep(10)
print('%s下載結(jié)束' % file, datetime.now())
def main():
print('程序開(kāi)始')
# print(datetime.now())
# 1.在主線(xiàn)程中下載三個(gè)電影 (總耗時(shí)30s)
# download('槍王之王.mp4')
# download('開(kāi)國(guó)大典')
# download('黃金國(guó).mp4')
# 2.在三個(gè)子線(xiàn)程中同時(shí)下載三個(gè)電影
"""
Thread(target,args) - 創(chuàng)建子線(xiàn)程對(duì)象
說(shuō)明:
target - Function船万,需要傳一個(gè)函數(shù)(這個(gè)函數(shù)中的內(nèi)容會(huì)在子線(xiàn)程中執(zhí)行)
args - 元祖,target對(duì)應(yīng)的函數(shù)的參數(shù)
當(dāng)通過(guò)創(chuàng)建好的子線(xiàn)程對(duì)象調(diào)用start方法的時(shí)候,會(huì)自動(dòng)在子線(xiàn)程中調(diào)用target對(duì)應(yīng)的函數(shù), 并且將args中值作為實(shí)參
"""
# 創(chuàng)建線(xiàn)程對(duì)象
t1 = threading.Thread(target=download, args=('槍王之王.mp4',))
t2 = threading.Thread(target=download, args=('開(kāi)國(guó)大典.mp4',))
t3 = threading.Thread(target=download, args=('黃金國(guó).mp4',))
# 開(kāi)始執(zhí)行t1對(duì)應(yīng)的子線(xiàn)程中的任務(wù)(實(shí)質(zhì)就是在子線(xiàn)程中調(diào)用target對(duì)應(yīng)的函數(shù))
t1.start()
t2.start()
t3.start()
print('=============')
if name == 'main':
main()
"""author = drh"""
import threading
import time as time1
from datetime import time
"""
可以通過(guò)寫(xiě)一個(gè)類(lèi)繼承Thread類(lèi)骨田,來(lái)創(chuàng)建屬于自己的線(xiàn)程類(lèi)耿导。
1.聲明類(lèi)繼承Thread
2.重寫(xiě)run方法。這個(gè)方法中的任務(wù)就是需要在子線(xiàn)程中執(zhí)行的任務(wù)
3.需要線(xiàn)程對(duì)象的時(shí)候态贤,創(chuàng)建當(dāng)前聲明的類(lèi)的對(duì)象舱呻;然后通過(guò)start方法在子線(xiàn)程中去執(zhí)行run方法中的任務(wù)
"""
class DownloadThread(threading.Thread):
"""下載類(lèi)"""
def __init__(self, file):
super().__init__()
self.file = file
def run(self):
print('開(kāi)始下載:'+self.file)
print('run:', threading.current_thread())
time1.sleep(10)
print('%s下載結(jié)束' % self.file)
def main():
# 獲取當(dāng)前線(xiàn)程
print(threading.current_thread())
t1 = DownloadThread('沉默的羔羊.mp4')
t2 = DownloadThread('恐怖游輪.mp4')
# 調(diào)用start的時(shí)候會(huì)自動(dòng)在子線(xiàn)程中調(diào)用run方法
t1.start()
t2.start()
# 注意:如果直接用對(duì)象調(diào)用run方法,run方法中的任務(wù)會(huì)在主線(xiàn)程執(zhí)行
# t1.run()
if __name__ == '__main__':
main()
"""author = drh"""
from threading import Thread
import requests
import re
import time
from random import randint
class DownloadThread2(Thread):
"""下載類(lèi)"""
def __init__(self, file, time):
super().__init__()
self.file = file
self.time = time
def run(self):
print('開(kāi)始下載:'+self.file)
# t = randint(5, 10)
time.sleep(self.time)
print('%s下載結(jié)束, 總共耗時(shí):%ds' % (self.file, self.time))
class DownloadImageThread(Thread):
def __init__(self, url):
super().__init__()
self.url = url
def run(self):
# 開(kāi)始下載
file_name = re.split(r'/', self.url)[-1]
print(file_name)
print('%s開(kāi)始下載' % file_name)
response = requests.get(self.url)
content = response.content
with open('images/'+file_name, 'bw') as f:
f.write(content)
print('%s下載結(jié)束' % file_name)
def creat_thread():
t1 = DownloadThread2('電影1', 6)
t2 = DownloadThread2('電影2', 4)
t1.start()
t2.start()
# 線(xiàn)程對(duì)象調(diào)用join方法悠汽,會(huì)導(dǎo)致join后的代碼會(huì)在線(xiàn)程中的任務(wù)結(jié)束后才執(zhí)行
t1.join()
t2.join()
print('電影下載結(jié)束!')
def main():
# t1 = DownloadImageThread('https://image.haha.mx/2015/12/04/middle/2082175_c5c3cc05eb73e4023149e663475d3ab4_1449192201.gif')
# t1.start()
#
# t2 = DownloadImageThread('http://img4.imgtn.bdimg.com/it/u=534897622,845095650&fm=26&gp=0.jpg')
# t2.start()
t0 = Thread(target=creat_thread)
t0.start()
print('========')
for x in range(100):
time.sleep(1)
print(x)
if name == 'main':
main()
"""author = drh"""
單獨(dú)封裝了一個(gè)color(顏色)類(lèi):方便上面調(diào)用
from random import randint
class Color:
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 255)
blue = (0, 0, 255)
yellow = (255, 255, 0)
gray = (155, 155, 155)
@staticmethod
def random_color():
return randint(0, 255), randint(0, 255), randint(0, 255)