绘制一个圆
import sys
import pygame
from pygame.locals import *
# 初始化
pygame.init()
# 设置窗口大小、窗口文字
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Circles")
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()
运行结果:
绘制移动的矩形
import sys
import time
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Rectangles")
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()
time.sleep(0.01)
由于是动图,这里就不再展示,可以自己复制代码运行。
绘制一条直线
import sys
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Lines")
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
width = 10
pygame.draw.line(screen, color, (100, 100), (400, 400), width)
pygame.display.update()
运行结果如下:
绘制一条弧线
import math
import sys
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Arcs")
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 = 200, 150, 200, 200
# 用math.radians()函数将角度转换成弧度
start_angle = math.radians(0)
end_angle = math.radians(180)
width = 10
pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
pygame.display.update()
运行结果如下:
做一个Pie小游戏
import math
import pygame
import sys
from pygame.locals import *
# 初始化
pygame.init()
# 设置窗口大小、窗口文字以及字体
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("The Pie Game - Press 1,2,3,4")
myfont = pygame.font.Font(None, 60)
color = 200, 80, 60
width = 4
x = 300
y = 250
radius = 200
position = x-radius, y-radius, radius * 2, radius * 2
piece1 = False
piece2 = False
piece3 = False
piece4 = False
while True:
# 检查输入
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYUP:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_1:
piece1 = True
elif event.key == pygame.K_2:
piece2 = True
elif event.key == pygame.K_3:
piece3 = True
elif event.key == pygame.K_4:
piece4 = True
# 给屏幕填充颜色
screen.fill((0, 0, 200))
# 分割
textImage1 = myfont.render("1", True, color)
screen.blit(textImage1, (x + radius / 2 - 20, y - radius / 2 - 20))
textImage2 = myfont.render("2", True, color)
screen.blit(textImage2, (x - radius / 2 - 20, y - radius / 2 - 20))
textImage3 = myfont.render("3", True, color)
screen.blit(textImage3, (x - radius / 2 - 20, y + radius / 2 - 20))
textImage4 = myfont.render("4", True, color)
screen.blit(textImage4, (x + radius / 2 - 20, y + radius / 2 - 20))
# 画图
if piece1:
start_angle = math.radians(0)
end_angle = math.radians(90)
pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
pygame.draw.line(screen, color, (x, y), (x, y - radius), width)
pygame.draw.line(screen, color, (x, y), (x + radius, y), width)
if piece2:
start_angle = math.radians(90)
end_angle = math.radians(180)
pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
pygame.draw.line(screen, color, (x, y), (x, y - radius), width)
pygame.draw.line(screen, color, (x, y), (x - radius, y), width)
if piece3:
start_angle = math.radians(180)
end_angle = math.radians(270)
pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
pygame.draw.line(screen, color, (x, y), (x - radius, y), width)
pygame.draw.line(screen, color, (x, y), (x, y + radius), width)
if piece4:
start_angle = math.radians(270)
end_angle = math.radians(360)
pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
pygame.draw.line(screen, color, (x, y), (x, y + radius), width)
pygame.draw.line(screen, color, (x, y), (x + radius, y), width)
# 全部画完就变色
if piece1 and piece2 and piece3 and piece4:
color = 0, 255, 0
pygame.display.update()
运行结果的部分截图:
任务一:绘制一个椭圆
这里我想到的方法就是利用弧线函数,画两条弧线就可以构成一个椭圆:
import math
import sys
import pygame
from pygame.locals import *
pygame.init()
# 画椭圆
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Ovals")
# 上面的弧线从0到180
start_angle1 = math.radians(0)
end_angle1 = math.radians(180)
# 下面的弧线从180到360
start_angle2 = math.radians(180)
end_angle2 = math.radians(360)
color = 255, 0, 0
position = 200, 250, 200, 100
width = 8
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.fill((0, 0, 200))
pygame.draw.arc(screen, color, position, start_angle1, end_angle1, width)
pygame.draw.arc(screen, color, position, start_angle2, end_angle2, width)
pygame.display.update()
运行结果如下:
任务二:利用random.randint()函数随机绘制1000条直线
这里我遇到了几个问题,
1.如何绘制多条直线
2.randint()函数怎么用
第一个问题我是想到用for循环解决,但是一开始的结果确实一次性只能出现一条直线,然后我就想到pygame.draw.lines()函数,但是具体了解后发现这个函数只是把给出的坐标依次连接起来,无法满足我的要求,因此我就想到了用列表存储每条直线的起始位置和终止位置,然后我一开始的代码是这样的:
start_pos[i] = (randint(10, 300), randint(10, 300))
end_pos[i] = (randint(400, 600), randint(400, 500))
然后系统给我报了这么一个错:
IndexError: list assignment index out of range
通过查资料才知道是因为列表一开始为空,所以不能这么写,发生了地址越界的错误,因此需要用到列表中的insert()函数,将数据插入进去,最终我终于实现了。
第二个问题就是对于函数
random.randint(a, b)
意思是从[a, b]中随机取一个整数,注意区间!!!是[a, b],都是闭区间!我之前看网上有人说是[a, b),但是这是错的,b如果是整数,那么是可以取到的。
示例如下:
import random
for i in range(10):
print(random.randint(0, 2))
运行结果如下:
可以看到,2是可以取到的
解决完这两个问题后,就很容易了,源代码如下:
import time
from random import randint
import sys
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Lines")
# 将起始位置、终止位置以及颜色设置成列表形式
start_pos = []
end_pos = []
color = []
# 将列表中每个元素都设置成随机的值
for i in range(1000):
start_pos.insert(i, (randint(10, 300), randint(10, 300)))
end_pos.insert(i, (randint(400, 600), randint(400, 500)))
color.insert(i, (randint(0, 255), randint(0, 255), randint(0, 255)))
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
screen.fill((0, 0, 200))
width = 2
for i in range(1000):
pygame.draw.line(screen, color[i], start_pos[i], end_pos[i], width)
pygame.display.update()
运行结果如下:
任务三:修改移动矩形的代码,使其在碰到边界之后,马上变色
这个比较容易,只需要在代码中的触碰边界时,改变颜色即可:
import random
import sys
import time
import pygame
from pygame.locals import *
def move_rect():
pos_x = 300
pos_y = 250
vel_x = 2
vel_y = 1
color = 255, 255, 0
width = 0
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
pos = pos_x, pos_y, 100, 100
# 若触碰到边界,则反向同时随机变色
if pos_x > 500 or pos_x < 0:
vel_x = -vel_x
color = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
if pos_y > 400 or pos_y < 0:
vel_y = -vel_y
color = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
pygame.draw.rect(screen, color, pos, width)
pygame.display.update()
time.sleep(0.01)
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Rectangles")
move_rect()