1. 程式人生 > >【原創】pygame學習筆記(1)----基本的線,矩形,圓形,弧形繪製

【原創】pygame學習筆記(1)----基本的線,矩形,圓形,弧形繪製

PYgame的內容

(1)這個module很有意思

(2)書本至少來源於《Python遊戲程式設計入門》

(3)官方權威說明:https://www.pygame.org/docs/

 

下面的嘗試把各種圖形在一個程式裡繪製

注意點:

(1)特別注意,比如引用color=0,0,200這種

       要麼是pygame.draw.line(screen,color,position,position2,width)

       要麼是pygame.draw.line(screen,(0,0,200),position,position2,width)

      特別注意括號

# -*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import sys
import math

white=255,255,255
blue=0,0,200
color=255,255,0  #yellow
position =300,250
radius=100
width=5
position2=300+radius,250

pygame.init()

screen=pygame.display.set_mode((600,500)) #注意,位置和顏色作為陣列,都必須括起來作為引數!實際是雙層括號
caption=pygame.display.set_caption("draw draw draw")
myfont=pygame.font.Font(None,100)
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((0,0,200))
	
	#display text
	screen.blit(textImage,(0,100))
	
	#draw a cycle
	pygame.draw.circle(screen,color,position,radius,width)
	
	#draw a line
	pygame.draw.line(screen,color,position,position2,width)
	
	#draw 2 rect
	position_r1=250,200,100,100
	position_r2=400,200,100,100
	pygame.draw.rect(screen,color,position_r1,width)
	pygame.draw.rect(screen,color,position_r2,0)

	#draw arc
	position_r3=150,150,200,200
	start_angle=math.radians(90)
	end_angle=math.radians(270)	
	pygame.draw.arc(screen,color,position_r3,start_angle,end_angle,width)
	
	pygame.display.update()