與小卡特一起學python 第16章 圖形 Pygame學習
#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()
#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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python-pygame學習筆記PythonGAM筆記
- python 圖形初學Python
- pygame 教學 匯入圖片GAM
- Python學習第6天Python
- 與 MDN 一起學習 JavaScriptJavaScript
- pygame 教學 2 —— 匯入圖片GAM
- 圖形學3D渲染管線學習3D
- 圖形學學習筆記二:觀測變換筆記
- 【java學習】GUI 圖形程式設計JavaGUI程式設計
- 【數學】組合數學 - 卡特蘭數
- (003)我們一起學Python;鞏固練習,寫個小遊戲Python遊戲
- Python學習路線圖Python
- jmeter學習指南之結果分析-圖形圖表JMeter
- 自學Python筆記-pygame模組《外星人入侵》練習篇Python筆記GAM
- 圖形學 旋轉與投影矩陣—2矩陣
- 圖形學 旋轉與投影矩陣-3矩陣
- 圖形學 旋轉與投影矩陣—1矩陣
- Python學習之Web開發及圖形使用者介面模組!PythonWeb
- 深度學習和圖形學渲染的結合和應用深度學習
- 運籌學練習Python精解——圖與網路Python
- 一起學習RustRust
- 1.學習python思路圖Python
- python學習_思維導圖Python
- Python點陣圖索引學習Python索引
- 加入開源學習社群一起學習 GoGo
- Python學習手冊(第4版)PDF版Python
- 【CG】圖形學相關
- 小迪安全第12天資訊收集學習
- python學習筆記:第6天 小資料池和編碼轉換Python筆記
- Python 排序---sort與sorted學習Python排序
- python學習:變數與字串Python變數字串
- 前端週刊第62期:學習學習再學習前端
- 深度學習與圖神經網路學習分享:Graph Embedding 圖嵌入深度學習神經網路
- 小波變換與深度學習深度學習
- hover 背後的數學和圖形學
- 圖論與圖學習(二):圖演算法圖論演算法
- FPGA 學習之路:verilog學習第5天FPGA
- python學習筆記:第8天 檔案操作Python筆記
- Python學習筆記:第3天 字串的操作Python筆記字串