写在前面:
- 去年四月份,跟着某马的Python班利用pygame模块做了个飞机大战,如今刚刚找到工作,祖传技能不能丢,虽然农药已经戒了,但咱们可以转开发游戏呀哈哈!今天跟着书中的例子一个个敲出来,每天记录自己的进步!加油
绘制文字
- ps:这里出事了,跟着网上弄了很久也安不上pygame模块,自己用了科学冲浪的方式才终于用pip3给安好了。
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import * # 导入常量
import sys
white = 255, 255, 255
blue = 0, 0, 255
pygame.init() # 初始化
myfont = pygame.font.Font(None, 60) # 绘制字体,第一个参数是字体,第二个是字号
screen = pygame.display.set_mode((600,500))
textImage = myfont.render("hello pygame", True, white) # 创建一个平面对象
# 清除屏幕,绘制平面,刷新显示
screen.fill(blue)
screen.blit(textImage, (100,100))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
效果
绘制圆形
pygame.draw.circle(screen, color, position, radius, width)
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 24 11:29:53 2020
@author: zhaoxiangyun
"""
import pygame
from pygame.locals import * # 导入常量
import sys
white = 255, 255, 255
blue = 0, 0, 255
pygame.init() # 初始化
myfont = pygame.font.Font(None, 60) # 绘制字体,第一个参数是字体,第二个是字号
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Draw 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 = 50
width = 10
pygame.draw.circle(screen, color, position, radius, width)
pygame.display.update()
效果
绘制矩形(可动,碰到边框回弹)
pygame.draw.rect(screen, color, pos, width)
import pygame
from pygame.locals import *
import sys
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Draw Rectangles")
pos_x = 300
pos_y = 250
vel_x = 0.5
vel_y = 0.25
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 #代表边框,0是实心
pos = pos_x, pos_y,100, 100 #分别代表顶点的xy坐标,长度和宽度
pygame.draw.rect(screen, color, pos, width)
pygame.display.update()
效果
绘制线条
pygame.draw.line(screen, color, (x0, y0),(x1, y1), width)
import pygame
from pygame.locals import *
import sys
pygame.init()
screen = pygame.display.set_mode((600, 800))
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, 80, 0))
# 绘制线条
color = 100,255,200
width = 8
# 绘制参数: surface , color, 起始坐标,终止坐标,宽度
pygame.draw.line(screen, color, (100, 100),(500, 400), width)
pygame.display.update()
效果
绘制弧形
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
import pygame
import math
from pygame.locals import *
import sys
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,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()
效果
注意:
piece小游戏(点亮1234即可获胜)
import pygame
import math
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))
# 绘制四个数字减去20是为了让两边对称
textImg1 = myfont.render("1", True, color)
screen.blit(textImg1, (x+radius/2-20, y-radius/2))
textImg2 = myfont.render("2", True, color)
screen.blit(textImg2, (x - radius / 2 , y - radius / 2))
textImg3 = myfont.render("3", True, color)
screen.blit(textImg3, (x - radius / 2 , y + radius / 2-20))
textImg4 = myfont.render("4", True, color)
screen.blit(textImg4, (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)
# is the pie finished?
if piece1 and piece2 and piece3 and piece4:
color = 0, 255, 0
textImg5 = myfont.render("Success!", True, (255,100,63))
screen.blit(textImg5, (x - 80, y - 50))
# 刷新
pygame.display.update()
遇到的BUG:自己对着书写完发现总是黑屏,应该是因为elif 的缩进搞错了,然后调整了字体绘制方法render中的位置,因为字体本身有大小,而坐标是按照字体对象的左上角为原点布置的,所以要加减一个字体的长度。
最终运行效果
课后挑战:
- 绘制椭圆:pygame.draw.ellipse(screen, white, (100, 100, 400, 100), 1)
- 绘制一千个随机线条:
import pygame
from pygame.locals import *
import sys
import time
import random
pygame.init()
screen = pygame.display.set_mode((600, 800))
pygame.display.set_caption("Drawing one thousand Lines")
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
color = 100, 255, 200
width = 8
screen.fill((0, 80, 0))
# 绘制线条
for i in range(1000):
# 生成随机坐标
rand_l = [0, 0]
for a in range(2):
random.seed()
rand_x = random.randint(0, 600)
rand_y = random.randint(0, 800)
rand_site = (rand_x, rand_y)
print(rand_site)
rand_l[a] = rand_site
# 绘制参数: surface , color, 起始坐标,终止坐标,宽度
print(rand_l)
pygame.draw.line(screen, color, rand_l[0], rand_l[1], width)
time.sleep(0.02)
pygame.display.update()
注意:每次都要用seed设置随机种子,否则产生的随机数是一样的。另外列表初始化时候注意下标的范围问题。
增强版:颜色也变为随机
import pygame
from pygame.locals import *
import sys
import time
import random
pygame.init()
screen = pygame.display.set_mode((600, 800))
pygame.display.set_caption("Drawing one thousand Lines")
while True:
width = 8
screen.fill((0, 80, 0))
# 绘制线条
for i in range(1000):
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
# 生成随机坐标
rand_l = [0, 0]
color_l = [0,0,0]
for a in range(2):
random.seed()
rand_x = random.randint(0, 600)
rand_y = random.randint(0, 800)
rand_site = (rand_x, rand_y)
print(rand_site)
rand_l[a] = rand_site
for col in range(3):
random.seed()
color_x = random.randint(0, 255)
color_l[col]= color_x
color = (color_l[0],color_l[1],color_l[2])
# 绘制参数: surface , color, 起始坐标,终止坐标,宽度
print(rand_l)
pygame.draw.line(screen, color, rand_l[0], rand_l[1], width)
time.sleep(0.1)
pygame.display.update()
3. 修改矩形程序,使其在碰撞边框时改变颜色
思路:在判断越界条件中把颜色设置为随机
import pygame
from pygame.locals import *
import sys
import random
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Draw Rectangles")
pos_x = 300
pos_y = 250
vel_x = 0.25
vel_y = 0.12
color = 255, 255, 0
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
screen.fill((255,250,250))
# 移动矩形
pos_x += vel_x
pos_y += vel_y
if pos_x > 500 or pos_x < 0:
vel_x = -vel_x
# 改变随机颜色
color_l = [0, 0, 0]
for col in range(3):
random.seed()
color_x = random.randint(0, 255)
color_l[col] = color_x
color = (color_l[0], color_l[1], color_l[2])
if pos_y > 400 or pos_y < 0:
vel_y = -vel_y
# 改变随机颜色
color_l = [0, 0, 0]
for col in range(3):
random.seed()
color_x = random.randint(0, 255)
color_l[col] = color_x
color = (color_l[0], color_l[1], color_l[2])
# 绘制矩形
width = 0
pos = pos_x, pos_y,100, 100
pygame.draw.rect(screen, color, pos, width)
pygame.display.update()