與小卡特一起學python 第16章 圖形 Pygame學習

yarking207發表於2016-04-07
#16章圖形
#16-1建立一個PYgame視窗
import pygame
pygame.init()
screen = pygame.display.set_mode([1024,768])

#16-2 使得視窗正常工作
import pygame
pygame.init()
screen = pygame.display.set_mode([640,480])
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#16-3 畫一個圓
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill ([255,255,255])#背景填充為白色
pygame.draw.circle(screen,[255,0,0],[100,100],30,0)#畫圓開始
pygame.draw.rect(screen,[0,255,0],[250,150,300,200],2)
pygame.display.flip()#

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()
#16-5 使用DRAW.RECT實現藝術創作
import pygame,sys,random
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)
    pygame.draw.rect(screen,[0,0,0],[left,top,width,heigh],1)
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#16-6 帶顏色的現代藝術
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);height = random.randint(0,100)
    top = random.randint(0,400);left = random.randint(0,500)
#   color_name = random.choice(THECOLORS.keys()) 書中的隨機顏色呼叫出錯,
    color_name = "yellow" #直接賦值則程式正常。查資料是因為python3的相容問題
    color = THECOLORS[color_name]
    line_width = random.randint(1,3)
    pygame.draw.rect(screen,color,[left,top,width,height],line_width)
pygame.display.flip()

#16-7  用大量很小的矩形畫曲線
import pygame,sys
import math #匯入數學函式,包括sin()
pygame.init()
screen = pygame.display.set_mode ([640,480])
screen.fill([255,255,255])
for x in range(0,640):#從左到右迴圈0-639
    y = int(math.sin(x/640.0 *4 * math.pi) * 200 + 240)#計算每個點的Y座標
    pygame.draw.rect(screen,[0,0,0],[x,y,1,1],1)#使用小矩形來畫點
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()



#16-8 一條完美的正弦曲線
import pygame,sys
import math #匯入數學函式,包括sin()
pygame.init()
screen = pygame.display.set_mode ([640,480])
screen.fill([255,255,255])
plotPoints = [] #定義列表
for x in range(0,640):#從左到右迴圈0-639
    y = int(math.sin(x/640.0 *4 * math.pi) * 200 + 240)#計算每個點的Y座標
    plotPoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotPoints,1)#使用小矩形來畫點,lines
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#16-9 連連看神秘的圖片

import pygame,sys
pygame.init()

dots = [[221,432],[225,331],[133,342],[141,310],
        [51,230],[74,217],[58,153],[114,164],
        [123,135],[176,190],[159,77],[193,93],
        [230,28],[267,93],[301,77],[284,190],
        [327,135],[336,164],[402,153],[386,217],
        [409,230],[319,310],[327,342],[233,331],
        [237,432]]

screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
pygame.draw.lines(screen,[255,0,0],True,dots,5)
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()


#16-10 在PYgame視窗中顯示沙灘球影像
import pygame,sys
pygame.init()
screen = pygame.display.set_mode ([640,480])
screen.fill([255,255,255])
my_ball = pygame.image.load("beach_ball.png")

screen.blit(my_ball,[50,50])

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


#16-11 移動沙灘球
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_ball = pygame.image.load('beach_ball.png')
screen.blit(my_ball,[50,50])
pygame.display.flip()
pygame.time.delay(2000)# 延時
screen.blit(my_ball,[150,50])#顯示的新位置
pygame.display.flip()#完成移動
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#16-12 移動沙灘球后把原來圖形擦掉
import pygame,sys
pygame.init()
screen = pygame.display.set_mode ([640,480])
screen.fill([255,255,255])
my_ball = pygame.image.load('beach_ball.png')
screen.blit(my_ball,[50,50])
pygame.display.flip()
pygame.time.delay(2000)
screen.blit(my_ball,[400,50])
pygame.draw.rect(screen,[255,255,255],[50,50,90,90],0)# #這一行畫個矩形覆蓋原沙灘球
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#16-13 更流暢的移動沙灘球影像
import pygame,sys
pygame.init()
screen = pygame.display.set_mode ([640,480])
screen.fill([255,255,255])
my_ball = pygame.image.load('beach_ball.png')
x = 50
y = 50
screen.blit(my_ball,[x,y])
pygame.display.flip()
for looper in range (1,100):#迴圈
    pygame.time.delay(20) #時間改為20
    pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
    x = x + 5
    screen.blit(my_ball,[x , y])
    pygame.display.flip()

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


#16-14 讓沙灘球在左右邊框反彈
import pygame,sys
pygame.init()
screen = pygame.display.set_mode ([640,480])
screen.fill ([255,255,255])
my_ball = pygame.image.load('beach_ball.png')
x = 50
y = 50
x_speed = 2 #speed 變數

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,90,90],0)
    x = x + x_speed
    if x > screen.get_width() - 90 or x < 0:
        x_speed = - x_speed #變方向
    screen.blit(my_ball,[x,y])
    pygame.display.flip()
pygame.quit()


#16-15 在2D空間中讓沙灘球反彈
import pygame,sys
pygame.init()
screen = pygame.display.set_mode([800,600])
screen.fill([255,255,255])
my_ball = pygame.image.load('beach_ball.png')
x = 50
y = 50
x_speed = 20
y_speed = 20 #增加垂直運動
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.time.delay(50)
    pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
    x = x + x_speed
    y = y + y_speed
    if x > screen.get_width() - 90 or x< 0:
        x_speed = -x_speed
    if y > screen.get_height() - 90 or y <0:#讓球在視窗頂邊或者底邊反彈
        y_speed = -y_speed
    screen.blit(my_ball,[x,y])
    pygame.display.flip()
pygame.quit()

#16-16 讓球翻轉
import  pygame,sys
pygame.init()
screen = pygame.display.set_mode([800,600])
screen.fill([255,255,255])
my_ball = pygame.image.load('beach_ball.png')
x = 50
y = 50
x_speed = 5
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.time.delay(50)
    pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
    y = y + x_speed
    if y > screen.get_height():#球開始落下height
        y = 0 #從頂部開始落下
    screen.blit(my_ball,[x,y])
    pygame.display.flip()
pygame.quit()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2077162/,如需轉載,請註明出處,否則將追究法律責任。

相關文章