學習總結-《父與子的編程之旅》chapter 16


這一章中,主要講如何在屏幕上畫圖,比如直線、形狀、顏色,還會談到一 點動畫。主要基於pygame模塊。

動畫(反彈與翻轉):

import  sys,pygame
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
mytag = pygame.image.load("imagetag.jpg")
x = 0
y = 0
x_speed = 10
#y_speed = 3

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

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255, 255, 255], [x, y, 800, 500], 0)
    x = x + x_speed
# y = y + y_speed
    if x>screen.get_width()-350 or x <0:
        #x_speed = -x_speed
        x = -10
# if y>screen.get_height() -350 or y<0:
 # y_speed = -y_speed
    screen.blit(mytag,[x,y])
    pygame.display.flip()
pygame.quit()

畫線:

import pygame,sys
pygame.init()

dots = [[221,432],[43,264],[123,200],[12,300],[200,126]]

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
pygame.draw.lines(screen,[255,0,0],True,dots,2)
screen.set_at([43,264],[0,0,0])

pixel_color = screen.get_at([43,264])
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.display.flip()
    pygame.time.delay(3)
pygame.quit()
print(pixel_color)

帶顏色的現代藝術:

import  pygame,sys,random
from pygame.color import THECOLORS
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
for i in range (100):
    width = random.randint(0,250)
    heigh = random.randint(0,100)
    top = random.randint(0,400)
    left = random.randint(0,500)
    color_name_list = list(THECOLORS.keys()) #python3要先把字典轉化為列表 python2則 color_name = random.choice(THECOLORS.keys())
    color_name = random.choice(color_name_list)
    color = THECOLORS[color_name]
    line_width = random.randint(1,3)
    pygame.draw.rect(screen,color,[left,top,width,heigh],line_width)
#pygame.draw.rect(screen,[200,0,0],[100,50,200,200],10)
#在哪個表面畫,顏色RGB,位置,半徑單位像素,線寬
#pygame.display.flip()#顯示“下一屏”
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

pygame.quit()

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com