用pygame做一个画笔的游戏
import pygame
from pygame.locals import *
import random
# 定义颜色变量
redColour = pygame.Color(255, 0, 0) # 红色:游戏结束后的字体颜色
blackColour = pygame.Color(0, 0, 0) # 黑色:背景、字体颜色、圆形的颜色(初始为黑色,玩家点击后变成随机的颜色)
def main():
# 初始化pygame库中的所有模块
pygame.init()
# 创建一个新的800x600大小的窗体,并把它命名为“画笔游戏”。这里size可以根据想要的大小进行修改。
screen = pygame.display.set_mode((800, 600)) # 窗体大小
pygame.display.set_caption('画笔游戏') # 窗体标题
gameOver = False # 标志是否退出游戏循环
# 循环直到退出标志为True时退出循环
while not gameOver:
screen.fill(blackColour) # 背景填充为黑色
for event in pygame.event.get(): # 遍历所有事件
if event.type == QUIT: # 检测到QUIT事件时退出循环并退出pygame库。即关闭界面时会逐步回复pygmae库中执行过的动作。 gameOver = True break elif event .type == MOUSEBUTTONDOWN : mouseX , mouseY = event .pos radius = 5 colour = (random .randint (0 , 255), random .randint (0 , 255), random .randint (0 , 255)) width=5 circleX=mouseX-radius circleY=mouseY-radius circleRect=(circleX,circleY,width*2+radius*2,width*2+radius*2) print('圆心坐标',circleX+radius,circleY+radius,'半径',radius,'colour',colour ) pygame .draw .rect (screen , colour , circleRect ) print ('mouse clicked at ' + str (event .pos )) else : pass else : pass else : pass else : pass else : pass else : pass elif event .type == KEYDOWN and event .key == K_ESCAPE: gameOver=True break else: pass if gameOver==True: font1=pygame
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!