国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

《Hello World》python學習之pygame畫圖模塊

2019-11-08 03:00:47
字體:
來源:轉載
供稿:網友

python學習之pygame畫圖模塊

1 安裝

cmd中執行命令:pip install pygame 20170217-1.JPG 如果已經安裝,則會告知“Requirement already satisified”。

2 功能

2.1 新建窗口

代碼

import pygamepygame.init()screen = pygame.display.set_mode([640, 480])running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit()

執行:

20170217-2.JPG 生成一個640*480大小的黑色窗口。

2.2 畫圖

代碼

import pygame, syspygame.init()screen = pygame.display.set_mode([640,480])screen.fill([255,255,255]) # fill screen with white pygame.draw.circle(screen, [255,0,0],[100,100], 30, 0) # draw red circlepygame.display.flip()running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit()

執行

20170217-3.JPG

代碼screen.fill([255,255,255])將畫布設置為了白色。

注意:除了pygame可以畫出很多形狀,如aalines,arc,circle,ellipse,lines,polygon…且可以對其大小顏色等屬性進行設置。

2.3 單個像素

可以用大量很小的圓或矩形形成一個像素點。

import pygame, sysimport math pygame.init()screen = pygame.display.set_mode([640,480])screen.fill([255, 255, 255])running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False for x in range(0, 640): y = int(math.sin(x/640.0 * 4 * math.pi) * 200 + 240) # sine wave formula # draw the wave using small rectangles pygame.draw.rect(screen, [0,0,0],[x, y, 1, 1], 1) # x, y is the location of each rectangle # in the wave pygame.display.flip() pygame.time.delay(30)pygame.quit()

執行

20170217-4.JPG

2.4 圖像

在屏幕上畫圖是一種方法,另一種是將照片添加在屏幕上。

import pygame, syspygame.init()screen = pygame.display.set_mode([640,480])screen.fill([255, 255, 255])my_ball = pygame.image.load("beach_ball.png") # load the image from a file screen.blit(my_ball, [50, 50]) # draw or 'blit' it to the screenpygame.display.flip()running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit()

2.5 動畫

在新的位置畫出圖形,再把原來的圖形擦掉。

import pygame, syspygame.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]) # draw the beach ballpygame.display.flip()pygame.time.delay(2000)screen.blit(my_ball, [150, 50]) # draw it again, somewhere else# "erase" the first beach ballimagepygame.draw.rect(screen, [255,255,255], [50, 50, 90, 90], 0) pygame.display.flip()running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit()

2.6 在2D空間中反彈

import pygame, syspygame.init()screen = pygame.display.set_mode([640,480])screen.fill([255, 255, 255])my_ball = pygame.image.load('beach_ball.png')x = 50y = 50x_speed = 10 # now we have both x_speedy_speed = 10 # and y_speedrunning = 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 # move the ball hirizontally y = y + y_speed # and vertically if x > screen.get_width() - 90 or x < 0: # bounce off the sides x_speed = - x_speed if y > screen.get_height() - 90 or y < 0: # bounce off the top, bottom y_speed = -y_speed screen.blit(my_ball, [x, y]) pygame.display.flip()pygame.quit()

20170217-5.JPG


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金华市| 彰化市| 辰溪县| 兴仁县| 隆化县| 富顺县| 临泉县| 怀安县| 石家庄市| 喀喇沁旗| 甘洛县| 潮安县| 沙河市| 东至县| 霸州市| 措美县| 杭锦旗| 怀来县| 蚌埠市| 祁阳县| 武定县| 外汇| 应城市| 乐东| 涿州市| 九寨沟县| 防城港市| 从江县| 徐汇区| 海伦市| 永城市| 河池市| 蛟河市| 通州区| 石林| 株洲市| 即墨市| 南郑县| 饶平县| 乐昌市| 邵武市|