|
画圆
[Python] 纯文本查看 复制代码 import pygame
import sys
pygame.init()
pygame.display.set_caption('画圆')
screen = pygame.display.set_mode([600, 500])
while True:
for event in pygame.event.get(): # 点击关闭x
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#大红实心圆
pygame.draw.circle(screen, (255, 0, 0), (300, 250), 80, 0)
#小蓝空心圆
BLUE = (0, 0, 255) # 圆的颜色
position = 300, 350 # 坐标
radius = 50 # 半径
width = 10 # 为0是实心球
pygame.draw.circle(screen, BLUE, position, radius, width) # 把参数带入,画圆
pygame.display.update()
键盘移动圆
[Python] 纯文本查看 复制代码 import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((400, 300))
x, y = 200, 150
#screen.fill((0, 0, 0))
while True:
pygame.time.delay(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x -= 5
if keys[pygame.K_d]:
x += 5
if keys[pygame.K_w]:
y -= 5
if keys[pygame.K_s]:
y += 5
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (0, 250, 100), (x, y), 50)
pygame.display.update()
鼠标移动画圆
[Python] 纯文本查看 复制代码 import pygame
import sys
from random import randint
pygame.init()
screen = pygame.display.set_mode((500, 500))
screen.fill((255, 255, 255))
pygame.display.set_caption("鼠标移动画圆")
pygame.display.flip()
while True:
# 事件检测
for event in pygame.event.get():
# 退出游戏
if event.type == pygame.QUIT:
print('程序结束')
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: #鼠标按下
print('鼠标按下')
mx, my = event.pos
pygame.draw.circle(screen, (255, 255, 0), (mx, my), 20) #画圆
if event.type == pygame.MOUSEBUTTONUP:
print('鼠标弹出')
pass
if event.type == pygame.MOUSEMOTION: #鼠标移动
print('鼠标移动')
mx, my = event.pos
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
pygame.draw.circle(screen, (r, g, b,), (mx, my), 20)
pygame.display.update()
【标准模板】综合训练-键盘-鼠标
[Python] 纯文本查看 复制代码 import pygame
import sys
from random import randint
pygame.init()
screen = pygame.display.set_mode((500, 500))
screen.fill((255, 255, 255))
pygame.display.set_caption("鼠标移动画圆")
clock = pygame.time.Clock() # 生成游戏时钟
pygame.display.flip()
#pygame.display.flip() 更新整个待显示的Surface对象到屏幕上
#pygame.display.update() 更新部分内容显示到屏幕上,如果没有参数,则与flip功能相同
mx, my = 0, 0
while True:
# 事件检测
for event in pygame.event.get():
# 退出游戏
if event.type == pygame.QUIT:
print('程序结束')
pygame.quit()
sys.exit()
# 你需要在这里编写 [按键事件] 的代码
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
my -= 10
print('上键')
if event.key == pygame.K_DOWN:
my += 10
print('下键')
if event.key == pygame.K_RIGHT:
mx += 10
print('右键')
if event.key == pygame.K_LEFT:
mx -= 10
print('左键')
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
pygame.draw.circle(screen, (r, g, b,), (mx, my), 20)
# 你需要在这里编写 [鼠标事件] 的代码
if event.type == pygame.MOUSEBUTTONDOWN: # 鼠标按下
print('鼠标按下')
mx, my = event.pos
pygame.draw.circle(screen, (255, 255, 0), (mx, my), 20) #画圆
if event.type == pygame.MOUSEBUTTONUP:
print('鼠标弹出')
pass
if event.type == pygame.MOUSEMOTION: #鼠标移动
print('鼠标移动')
mx, my = event.pos
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
pygame.draw.circle(screen, (r, g, b,), (mx, my), 20)
pygame.display.update()
# 设置游戏速度
clock.tick(60)
|
|