PY交易(一)使用Pygame

PY交易(一)使用Pygame

也可以來我的部落格看哦崔斯特的部落格專欄

1、使用Pygame

import pygame #匯入Pygame

from pygame import * #匯入Pygame中的所有常量

pygame。init() #初始化Pygame

screen = pygame。display。set_mode((600,500)) #建立視窗

myfont = pygame。font。Font(None,60) #建立字型物件

white = 255,255,255

blue = 0,0,255

textImage = myfont。render(“hello Pygame”,True,white)

screen。fill(blue)

screen。blit(textImage,(100,100))

pygame。display。update()

首先在記憶體中建立文字平面,然後再將文本當作一個影象來繪製。

執行程式,出現如下視窗,發現馬上就關閉了,需要一個延遲。

PY交易(一)使用Pygame

PY交易(一)使用Pygame

2、迴圈

import pygame,sys #匯入Pygame

from pygame import * #匯入Pygame中的所有常量

white = 255,255,255

blue = 0,0,255

pygame。init() #初始化Pygame

screen = pygame。display。set_mode((600,500)) #建立視窗

myfont = pygame。font。Font(None,60) #建立字型物件

textImage = myfont。render(“hello Pygame”,True,white)

while True: #加入迴圈

for event in pygame。event。get():

if event。type in (QUIT,KEYDOWN):

sys。exit()

screen。fill(blue)

screen。blit(textImage,(100,100))

pygame。display。update()

加入while迴圈,只要while條件為真,就會一直執行下去。

現在執行看看,還會自動關閉嗎?

3、繪製圓

主要是使用 pygame。draw。circle()

import pygame,sys #匯入Pygame

from pygame import * #匯入Pygame中的所有常量

pygame。init() #初始化Pygame

screen = pygame。display。set_mode((600,500)) #建立視窗

pygame。display。set_caption(“畫圓”)

while True:

for event in pygame。event。get():

if event。type in (QUIT,KEYDOWN):

sys。exit()

screen。fill((0,0,200))

#畫圓

color = 255,255,0

position = 300,250

radius = 100

width = 10

pygame。draw。circle(screen,color,position,radius,width)

pygame。display。update()

PY交易(一)使用Pygame

PY交易(一)使用Pygame

4、繪製矩形

透過多個引數來呼叫pygame。draw。rect()函式,在while迴圈之外的記錄矩形的位置,並且建立一對速度變數。

import pygame,sys #匯入Pygame

from pygame import * #匯入Pygame中的所有常量

pygame。init() #初始化Pygame

screen = pygame。display。set_mode((600,500)) #建立視窗

pygame。display。set_caption(“畫矩形”)

pos_x = 300

pos_y = 250

vel_x = 2

vel_y = 1

while True:

for event in pygame。event。get():

if event。type in (QUIT,KEYDOWN):

sys。exit()

screen。fill((0,0,200))

#移動矩形

pos_x += vel_x

pos_y += vel_y

#讓矩形在螢幕中

if pos_x > 500 or pos_x < 0:

vel_x = -vel_x

if pos_y > 400 or pos_y < 0:

vel_y = -vel_y

#畫矩形

color = 255,255,0

width = 0

pos = pos_x,pos_y,100,100

pygame。draw。rect(screen,color,pos,width)

pygame。display。update()

執行程式後,發現有一個矩形在視窗中跳來跳去,好像還有某種規律。。。

PY交易(一)使用Pygame

PY交易(一)使用Pygame

5、繪製線條

使用pygame。draw。line()函式來繪製直線,必須提供線條起點和終點

import pygame,sys #匯入Pygame

from pygame import * #匯入Pygame中的所有常量

pygame。init() #初始化Pygame

screen = pygame。display。set_mode((600,500)) #建立視窗

pygame。display。set_caption(“畫直線”)

while True:

for event in pygame。event。get():

if event。type in (QUIT,KEYDOWN):

sys。exit()

screen。fill((0,80,0))

#畫直線

color = 100,255,200

width = 8

pygame。draw。line(screen,color,(100,100),(500,400),width)

pygame。display。update()

PY交易(一)使用Pygame

PY交易(一)使用Pygame

6、繪製弧線

弧線是圓的一部分,使用pygame。draw。arc()函式來繪製

要將角度轉換弧度,使用math。radians()函式

import math,pygame,sys

from pygame。locals import *

pygame。init()

screen = pygame。display。set_mode((600,500))

pygame。display。set_caption(“畫弧線”)

while True:

for event in pygame。event。get():

if event。type in (QUIT,KEYDOWN):

sys。exit()

screen。fill((0,0,200))

#畫弧線

color = 255,0,255

position = 200,150,200,200

start_angle = math。radians(0)

end_angle = math。radians(180)

width = 8

pygame。draw。arc(screen,color,position,start_angle,end_angle,width)

pygame。display。update()

PY交易(一)使用Pygame

PY交易(一)使用Pygame

好啦,第一期的PY交易就結束了,小夥伴有什麼想說的呢?評論中見

推薦閱讀:

http://www。

pygame。org/news

本文參考Jonathan S。Harbour創作的《More Python Programming for the Absolute Beginner》More Python Programming for the Absolute Beginner

======================================================================為什麼會玩Pygame呢?其實只是我個人的愛好,從小就喜歡玩遊戲,更想自己去創作遊戲。第一期只是介紹了一些基礎,下一步就會來編寫一個小遊戲啦。

PY交易(一)使用Pygame

PY交易(一)使用Pygame