與小卡特一起學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 第14章 物件Python物件
- 與小卡特一起學python 第19章 聲音Python
- 與小卡特一起學python 第20章 使用pyqtPythonQT
- 與小卡特一起學python 第10章 遊戲時間到了 pygame安裝及素材圖片準備Python遊戲GAM
- 與小卡特一起學python 第13章 函式-積木Python函式
- 與小卡特一起學python 第3章 基本數學運算Python
- 與小卡特一起學python 第21章 列印格式化與字串Python字串
- 與小卡特一起學python 第1章 出發吧 課後練習題Python
- 與小卡特一起學python 第4章 資料的型別Python型別
- 與小卡特一起學python 第11章 巢狀與可變迴圈Python巢狀
- 與小卡特一起學python 第22章 檔案輸入與輸出Python
- 與小卡特一起學python 第14章 物件 動手試一試Python物件
- 與小卡特一起學python 第9章 全都為了你-註釋Python
- 與小卡特一起學python 第6章 gui-圖形使用者介面 測試和動手試一試PythonGUI
- 與小卡特一起學Python 第15章 模組 及動手試一試Python
- 與小卡特一起學python 第17章動畫精靈和碰撞檢測Python動畫
- 與小卡特一起學python 第18章 一種新的輸入-事件Python事件
- 與小卡特一起學python 第8章 動手試一試原始碼Python原始碼
- 與小卡特一起學python 第6章 gui-圖形使用者介面 6-1-2-3-4-5 gui使用PythonGUI
- 與小卡特一起學python 第10章 遊戲時間到了 程式碼清單Python遊戲
- 與小卡特一起學python 第13章 函式-積木 動手試一試Python函式
- 與小卡特一起學python 第1章 出發吧 1-2猜數遊戲Python遊戲
- 與小卡特一起學python 第8章 轉圈圈 FOR迴圈和條件迴圈Python
- 與小卡特一起學python 第12章 收集起來,列表與字典 動手試一試Python
- 與小卡特一起學python 第2章 記住記憶體和變數 2-1練習Python記憶體變數
- 與小卡特一起學python 第11章 巢狀與可變迴圈 動手試一試Python巢狀
- 與小卡特一起學python 第5章 輸入 測試題和動手試一試Python
- 與小卡特一起學python 第7章 判斷再判斷 7-1-2-3-6-7Python
- 與小卡特一起學python 第1章 出發吧 1-1練習我們第一個真正的程式Python
- 與小卡特一起學python 第5章 輸入 5-1,2,3,4 input()輸入函式Python函式
- 學習筆記之JAVA圖形設計卷I AWT——第3章 圖 形 (轉)筆記Java
- 第15.16.17章學習筆記筆記
- 與小卡特一起學python 第2章 記住記憶體和變數 課後 動手試一試Python記憶體變數
- python-pygame學習筆記PythonGAM筆記
- Java學習第1章Java
- python 圖形初學Python
- pygame學習資料GAM
- 與 Linux 一起學習:學習打字Linux