淺談Python Pygame圖像的基本使用
游戲離不開坐標(biāo),我們來康康pygame中坐標(biāo)是如何設(shè)立的吧~
窗口左上角坐標(biāo)(0,0),橫軸正向向右,縱軸正向向下
實(shí)際效果碰到邊框就返回(其實(shí)是小球碰撞實(shí)驗(yàn),我不愛用正經(jīng)的小球,所以…)
import pygame,syspygame.init()size = width, height = 600, 400speed = [1,1]BLACK = 0, 0, 0s = pygame.display.set_mode(size)pygame.display.set_caption('hi 滑稽')ball = pygame.image.load('img/361.png')ballrect = ball.get_rect()while True: for event in pygame.event.get():if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed[0], speed[1]) if ballrect.left < 0 or ballrect.right > width:speed[0] = - speed[0] if ballrect.top < 0 or ballrect.bottom > height:speed[1] = - speed[1] s.fill(BLACK) s.blit(ball, ballrect) pygame.display.update()代碼說明
碰撞原理
方法說明
方法 說明 pygame.image.load(filename) 將filename路徑下的圖像載入游戲,支持JPG、PNG、GIF(非動(dòng)畫)等13種常用圖片格式 get_rect() 返回一個(gè)覆蓋圖像的矩形Rect對(duì)象 move(x,y) 矩形移動(dòng)一個(gè)偏移量(x,y),即在橫軸方向移動(dòng)x像素,縱軸方向移動(dòng)y像素,xy為整數(shù) fill(color) 顯示窗口背景填充為color顏色,采用RGB色彩體系 blit(src, dest) 將一個(gè)圖像繪制在另一個(gè)圖像上,即將src繪制到dest位置上。載入圖片
pygame.init()size = width, height = 600, 400 #設(shè)置了寬高,也可以在pygame.display.set_mode()設(shè)置speed = [1,1] #速度BLACK = 0, 0, 0 #顏色黑色s = pygame.display.set_mode(size)pygame.display.set_caption('hi 滑稽')ball = pygame.image.load('img/361.png') #注意圖片路徑ballrect = ball.get_rect()
surface對(duì)象和Rect對(duì)象
Rect對(duì)象屬性
Rect對(duì)象有一些重要屬性,例如:top,bottom,left,right表示上下左右width,height表示寬度、高度。
移動(dòng)
ballrect = ballrect.move(speed[0], speed[1]) # x1 if ballrect.left < 0 or ballrect.right > width: # x2speed[0] = - speed[0] if ballrect.top < 0 or ballrect.bottom > height:speed[1] = - speed[1]
x1:矩形移動(dòng)一個(gè)偏移量(x,y),即在橫軸方向移動(dòng)x像素,縱軸方向移動(dòng)y像素,xy為整數(shù)x2:遇到左右兩側(cè),橫向速度取反;遇到上下兩側(cè),縱向速度取反。
到此這篇關(guān)于淺談Python Pygame圖像的基本使用的文章就介紹到這了,更多相關(guān)Pygame圖像的基本使用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析3. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法4. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題5. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測(cè)可用)6. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明7. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法8. CSS hack用法案例詳解9. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)10. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向
