Pygame 游戏开发 实战 1
Posted 我是小白呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pygame 游戏开发 实战 1相关的知识,希望对你有一定的参考价值。
Pygame 游戏开发 第三课 实战 1
动态彩色圆环
通过圆形和线的绘制, 和文字的添加, 来实现一个简单的数字选择小游戏.
代码:
import pygame
from pygame.locals import *
import sys
import random
import math
# 初始化 pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((600, 400))
# 设置字体和字号 (仿宋)
myFont = pygame.font.Font("C:\\Windows\\Fonts\\simfang.ttf", 50)
# 设置背景颜色
screen.fill((255, 205, 232))
# 设置标题
pygame.display.set_caption("动态彩色圆环")
# 捕获游戏事件
typelist = [QUIT]
# 初始化变量
pos_x = 300 # 大圆横坐标
pos_y = 200 # 大圆纵坐标
radius_big = 170 # 大圆半径
radius_small = 20 # 小圆半径
angle = 0 # 小圆初始角度
while True:
# 获取事件
for event in pygame.event.get():
# 接收到退出事件, 退出程序
if event.type == QUIT:
sys.exit() # 退出
# 获取键盘按键
keys = pygame.key.get_pressed()
# 扫描1-9按键
for k in range(1, 10):
if keys[K_0 + k]:
# 刷新背景
screen.fill((255, 205, 232))
# 改变小圆半径
radius_small = 5 * k
# 角度递增
angle += 1
# 随机生成颜色
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = r, g, b
# 计算小圆对于大圆的坐标
x = radius_big * math.cos(math.radians((angle)))
y = radius_big * math.sin(math.radians((angle)))
# 计算小圆圆心坐标
pos = (int(pos_x + x), int(pos_y + y))
# 绘制小圆
pygame.draw.circle(screen, color, pos, radius_small)
# 更新显示
pygame.display.update()
输出结果:
选择数字
键盘按下对应的数字 1-4, 显示对应的 1/4 圆. 当 1-4 都被点击, 变成绿色.
代码:
import pygame
from pygame.locals import *
import sys
def draw_text():
# 创建数字
text_img1 = myFont.render("1", True, color)
text_img2 = myFont.render("2", True, color)
text_img3 = myFont.render("3", True, color)
text_img4 = myFont.render("4", True, color)
screen.blits([(text_img1, (center[0] + 40, center[1] - 70)),
(text_img2, (center[0] - 60, center[1] - 70)),
(text_img3, (center[0] - 60, center[1] + 30)),
(text_img4, (center[0] + 40, center[1] + 30)), ])
# 初始化 pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((600, 400))
# 设置字体和字号 (仿宋)
myFont = pygame.font.Font("C:\\Windows\\Fonts\\simfang.ttf", 50)
# 设置背景颜色
screen.fill((255, 205, 232))
# 设置标题
pygame.display.set_caption("选择数字")
# 初始化变量
radius = 150 # 半径
center = 300, 200 # 圆心
color = 217, 184, 241 # 颜色
p1 = p2 = p3 = p4 = False # 按键是否按下
l1 = l2 = l3 = l4 = False # 直线是否创建
flag = False # 是否运行完毕
# 添加文字
draw_text()
# 更新显示
pygame.display.update()
while True:
# 获取事件
for event in pygame.event.get():
# 接收到退出事件, 退出程序
if event.type == QUIT:
sys.exit() # 退出
# 按键事件
elif event.type == KEYDOWN:
# 当 1 被按下
if event.key == pygame.K_1:
p1 = True
# 画1/4圆
pygame.draw.circle(screen, color, center, radius, width=10, draw_top_right=True)
if not l1:
pygame.draw.line(screen, color, center, (center[0] + radius, center[1]), width=10)
l1 = True
if not l2:
pygame.draw.line(screen, color, center, (center[0], center[1] - radius), width=10)
l2 = True
# 更新显示
pygame.display.update()
# 当 2 被按下
elif event.key == pygame.K_2:
p2 = True
# 画1/4圆
pygame.draw.circle(screen, color, center, radius, width=10, draw_top_left=True)
if not l2:
pygame.draw.line(screen, color, center, (center[0], center[1] - radius), width=10)
l2 = True
if not l3:
pygame.draw.line(screen, color, center, (center[0] - radius, center[1]), width=10)
l3 = True
# 更新显示
pygame.display.update()
# 当 3 被按下
elif event.key == pygame.K_3:
p3 = True
# 画1/4圆
pygame.draw.circle(screen, color, center, radius, width=10, draw_bottom_left=True)
if not l3:
pygame.draw.line(screen, color, center, (center[0] - radius, center[1]), width=10)
l3 = True
if not l4:
pygame.draw.line(screen, color, center, (center[0], center[1] + radius), width=10)
l4 = True
# 更新显示
pygame.display.update()
# 当 4 被按下
elif event.key == pygame.K_4:
p4 = True
# 画1/4圆
pygame.draw.circle(screen, color, center, radius, width=10, draw_bottom_right=True)
if not l4:
pygame.draw.line(screen, color, center, (center[0], center[1] + radius), width=10)
l4 = True
if not l1:
pygame.draw.line(screen, color, center, (center[0] + radius, center[1]), width=10)
l1 = True
# 更新显示
pygame.display.update()
# 1-4按键全部按下变绿
if p1 and p2 and p3 and p4:
# 判断是否已完成
if flag:
continue
# 完成
flag = True
# 将 color 改为绿色
color = 184, 241, 204
# 设置文字
draw_text()
# 变色
pygame.draw.circle(screen, color, center, radius, width=10, draw_top_right=True)
pygame.draw.circle(screen, color, center, radius, width=10, draw_top_left=True)
pygame.draw.circle(screen, color, center, radius, width=10, draw_bottom_left=True)
pygame.draw.circle(screen, color, center, radius, width=10, draw_bottom_right=True)
pygame.draw.line(screen, color, center, (center[0] + radius, center[1]), width=10)
pygame.draw.line(screen, color, center, (center[0], center[1] - radius), width=10)
pygame.draw.line(screen, color, center, (center[0] - radius, center[1]), width=10)
pygame.draw.line(screen, color, center, (center[0], center[1] + radius), width=10)
# 更新显示
pygame.display.update()
输出结果:
指针时钟
实现钟表.
代码:
import pygame
from pygame.locals import *
import sys
import math
from datetime import datetime
# 初始化 pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((600, 400))
# 设置字体和字号 (仿宋)
myFont = pygame.font.Font("C:\\Windows\\Fonts\\simfang.ttf", 20)
# 设置标题
pygame.display.set_caption("指针时钟")
# 初始化变量
clock_color = 184, 241, 237 # 钟颜色
hour_color = 255, 100, 100 # 时针颜色
minute_color = 217, 184, 241 # 分针颜色
second_color = 255, 255, 0 # 秒针颜色
pos_x = 300 # 圆心横坐标
pos_y = 200 # 圆心纵坐标
radius = 180 # 半径
while True:
# 获取事件
for event in pygame.event.get():
# 接收到退出事件, 退出程序
if event.type == QUIT:
sys.exit() # 退出
# 设置背景颜色
screen.fill((255, 205, 232))
# 绘制时钟外围
pygame.draw.circle(screen, clock_color, (pos_x, pos_y), radius=radius, width=6)
# 绘制表盘文字
for i in range(1, 13):
angle = math.radians(i * 30 - 90)
x = math.cos(angle) * (radius - 20) - 10
y = math.sin(angle) * (radius - 20) - 10
text_img = myFont.render(str(i), True, (200, 255, 255))
screen.blit(text_img, (pos_x + x, pos_y + y))
# 获取当前时间
today = datetime.today()
hour = today.hour
minute = today.minute
second = today.second
# 绘制时针
hourAngle = math.radians((hour * 30 - 90) % 360)
hour_x = math.cos(hourAngle) * (radius - 90) + pos_x
hour_y = math.sin(hourAngle) * (radius - 90) + pos_y
pygame.draw.line(screen, hour_color, (pos_x, pos_y), (hour_x, hour_y), width=20)
# 绘制分针
minuteAngle = math.radians((minute * 6 - 90) % 360)
minute_x = math.cos(minuteAngle) * (radius - 60) + pos_x
minute_y = math.sin(minuteAngle) * (radius - 60) + pos_y
pygame.draw.line(screen, minute_color, (pos_x, pos_y), (minute_x, minute_y), width=12)
# 绘制分针
secondAngle = math.radians((second * 6 - 90) % 360)
second_x = math.cos(secondAngle) * (radius - 60) + pos_x
second_y = math.sin(secondAngle) * (radius - 60) + pos_y
pygame.draw.line(screen, second_color, (pos_x, pos_y), (second_x, second_y), width=12)
# 绘制表盘中心实心圆
pygame.draw.circle(screen, clock_color, (pos_x, pos_y), 20)
# 显示时间
image_txt = myFont.render(str(hour) + ":" + str(minute) + ":" + str(second), True, (255, 255, 255))
screen.blit(image_txt, (0, 0))
# 更新显示
pygame.display.update()
输出结果:
以上是关于Pygame 游戏开发 实战 1的主要内容,如果未能解决你的问题,请参考以下文章
实战用Python+Pygame+Kivy(Buildozer)+Ubuntu开发安卓android手机端apk游戏及踩坑分享