首页 > 其他分享 >pygame各类形状

pygame各类形状

时间:2024-08-23 14:05:50浏览次数:10  
标签:color percent 形状 window pygame 各类 dt rect 255

代码:

#coding=utf-8

import os,sys,re,time,math
import pygame
import random
from win32api import GetSystemMetrics
from math import pi

pygame.init()
pygame.display.set_caption("各种形状测试")

percent = 0.6
screen_width = GetSystemMetrics(0)
screen_height = GetSystemMetrics(1)
window_width = int(screen_width*percent)
window_height = int(screen_height*percent)

dt = 0
clock = pygame.time.Clock()

screen = pygame.display.set_mode((window_width, window_height))

#停止处理输入法事件
pygame.key.stop_text_input()

font_path = os.path.join(os.path.dirname(sys.argv[0]), 'simsun.ttc')
font = pygame.font.Font(font_path, 15)

#矩形
rect_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
rect_border_width = 0
rect_position = [window_width//4+10, window_height//5+10, 50, 50] #x,y,w,h
rect_percent = 400
rect_text = font.render('矩形', 1, (255-rect_color[0], 255-rect_color[1], 255-rect_color[2]))

#圆形
circle_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
circle_width = 25
circle_position = [window_width//4+140, window_height//5+35] #x,y
circle_percent = 350
circle_text = font.render('圆形', 1, (255-circle_color[0], 255-circle_color[1], 255-circle_color[2]))

#多边形
polygon_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
polygon_position = [[window_width//4+250,window_height//5+60], [window_width//4+350,window_height//5+15], [window_width//4+255,window_height//5+160], [window_width//4+225,window_height//5+150], [window_width//4+245,window_height//5+20]]
polygon_border_width = 0
polygon_percent = 300
polygon_text = font.render('多边形', 1, (255-polygon_color[0], 255-polygon_color[1], 255-polygon_color[2]))

#椭圆形
ellipse_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
ellipse_rect = [window_width//4+105, window_height//5+135, 80, 50] #x,y,w,h
ellipse_width = 0
ellipse_percent = 250
ellipse_text = font.render('椭圆形', 1, (255-ellipse_color[0], 255-ellipse_color[1], 255-ellipse_color[2]))

#直线
line_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
line_start = [window_width//4+411, window_height//5+10]
line_end = [window_width//4+420, window_height//5+85]
line_width = 2
line_percent = 150
line_text = font.render('直线', 1, (255-line_color[0], 255-line_color[1], 255-line_color[2]))

#抗锯齿直线
aaline_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
aaline_start = [window_width//4+411, window_height//5+110]
aaline_end = [window_width//4+420, window_height//5+185]
aaline_blend = 2
aaline_percent = 50
aaline_text = font.render('抗锯齿直线', 1, (255-aaline_color[0], 255-aaline_color[1], 255-aaline_color[2]))

#弧形
arc_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
arc_rect = [window_width//4+520, window_height//5+10, 50, 80] #x,y,w,h
arc_start = 0
arc_stop = (400/639.)*pi*2.
arc_width = 3
arc_percent = 200
arc_text = font.render('弧形', 1, (255-arc_color[0], 255-arc_color[1], 255-arc_color[2]))

#多个连续的直线段
lines_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
lines_closed = False
lines_points = [[window_width//4+100, window_height//5+280], [window_width//4+150, window_height//5+290], [window_width//4+300, window_height//5+280], [window_width//4+320, window_height//5+230]]
lines_width = 2
lines_percent = 30
lines_text = font.render('多个连续的直线段', 1, (255-lines_color[0], 255-lines_color[1], 255-lines_color[2]))

#多个连续的直线抗锯齿线段
aalines_color = (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
aalines_closed = False
aalines_points = [[window_width//4+100, window_height//5+380], [window_width//4+150, window_height//5+390], [window_width//4+300, window_height//5+380], [window_width//4+320, window_height//5+330]]
aalines_width = 2
aalines_percent = 10
aalines_text = font.render('多个连续的直线抗锯齿线段', 1, (255-aalines_color[0], 255-aalines_color[1], 255-aalines_color[2]))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
    keys_pressed = pygame.key.get_pressed()
    
    #ESC键
    if keys_pressed[pygame.K_ESCAPE]:
        running = False
        
    if keys_pressed[pygame.K_a]:
        rect_position[0] -= rect_percent * dt
        circle_position[0] -= circle_percent * dt
        
        for p in polygon_position:
            p[0] -= polygon_percent * dt
        
        for p in lines_points:
            p[0] -= lines_percent * dt
        
        for p in aalines_points:
            p[0] -= aalines_percent * dt
            
        ellipse_rect[0] -= ellipse_percent * dt
        arc_rect[0] -= arc_percent * dt
        line_start[0] -= line_percent * dt
        line_end[0] -= line_percent * dt
        aaline_start[0] -= aaline_percent * dt
        aaline_end[0] -= aaline_percent * dt
    if keys_pressed[pygame.K_d]:
        rect_position[0] += rect_percent * dt
        circle_position[0] += circle_percent * dt
        
        for p in polygon_position:
            p[0] += polygon_percent * dt
        
        for p in lines_points:
            p[0] += lines_percent * dt
        
        for p in aalines_points:
            p[0] += aalines_percent * dt
            
        ellipse_rect[0] += ellipse_percent * dt
        arc_rect[0] += arc_percent * dt
        line_start[0] += line_percent * dt
        line_end[0] += line_percent * dt
        aaline_start[0] += aaline_percent * dt
        aaline_end[0] += aaline_percent * dt
    if keys_pressed[pygame.K_w]:
        rect_position[1] -= rect_percent * dt
        circle_position[1] -= circle_percent * dt
        
        for p in polygon_position:
            p[1] -= polygon_percent * dt
        
        for p in lines_points:
            p[1] -= lines_percent * dt
        
        for p in aalines_points:
            p[1] -= aalines_percent * dt
            
        ellipse_rect[1] -= ellipse_percent * dt
        arc_rect[1] -= arc_percent * dt
        line_start[1] -= line_percent * dt
        line_end[1] -= line_percent * dt
        aaline_start[1] -= aaline_percent * dt
        aaline_end[1] -= aaline_percent * dt
    if keys_pressed[pygame.K_s]:
        rect_position[1] += rect_percent * dt
        circle_position[1] += circle_percent * dt
        
        for p in polygon_position:
            p[1] += polygon_percent * dt
        
        for p in lines_points:
            p[1] += lines_percent * dt
        
        for p in aalines_points:
            p[1] += aalines_percent * dt
            
        ellipse_rect[1] += ellipse_percent * dt
        arc_rect[1] += arc_percent * dt
        line_start[1] += line_percent * dt
        line_end[1] += line_percent * dt
        aaline_start[1] += aaline_percent * dt
        aaline_end[1] += aaline_percent * dt

    
    screen.fill("purple")
    
    #绘制正方形
    pygame.draw.rect(screen, rect_color, rect_position, rect_border_width)
    screen.blit(rect_text, (rect_position[0]+rect_position[2]//4, rect_position[1]+rect_position[3]//3))
    
    #绘制圆形
    pygame.draw.circle(screen, circle_color, circle_position, circle_width)
    screen.blit(circle_text, (circle_position[0]-circle_width//2, circle_position[1]-circle_width//5))
    
    #绘制多边形
    pygame.draw.polygon(screen, polygon_color, polygon_position, polygon_border_width)
    centerindex = int(math.ceil(len(polygon_position) / 2.0))
    screen.blit(polygon_text, (polygon_position[centerindex][0]+15, polygon_position[centerindex][1]-60))
    
    #绘制椭圆形
    pygame.draw.ellipse(screen, ellipse_color, ellipse_rect, ellipse_width)
    screen.blit(ellipse_text, (ellipse_rect[0]+ellipse_rect[2]//5, ellipse_rect[1]+ellipse_rect[3]//3))
    
    #绘制直线
    pygame.draw.line(screen, line_color, line_start, line_end, line_width)
    screen.blit(line_text, ((line_start[0]+line_end[0])//2, (line_start[1]+line_end[1])//2))
    
    #绘制一条抗锯齿直线
    pygame.draw.aaline(screen, aaline_color, aaline_start, aaline_end, aaline_blend)
    screen.blit(aaline_text, ((aaline_start[0]+aaline_end[0])//2, (aaline_start[1]+aaline_end[1])//2))
    
    #绘制弧线
    pygame.draw.arc(screen, arc_color, arc_rect, arc_start, arc_stop, arc_width)
    screen.blit(arc_text, (arc_rect[0], arc_rect[1]//2))
    
    #绘制多个连续的直线段
    pygame.draw.lines(screen, lines_color, lines_closed, lines_points, lines_width)
    centerindex = int(math.ceil(len(lines_points) / 2.0))
    screen.blit(lines_text, (lines_points[centerindex][0]-65, lines_points[centerindex][1]-60))
    
    #绘制多个连续的直线抗锯齿线段
    pygame.draw.aalines(screen, aalines_color, aalines_closed, aalines_points, aalines_width)
    centerindex = int(math.ceil(len(aalines_points) / 2.0))
    screen.blit(aalines_text, (aalines_points[centerindex][0]-65, aalines_points[centerindex][1]-60))
    
        
    #更新显示
    pygame.display.flip()
    #pygame.display.update()
    
    dt = clock.tick(60) / 600
    
pygame.quit()

 

效果:

 

参考:https://www.osgeo.cn/pygame/ref/draw.html

标签:color,percent,形状,window,pygame,各类,dt,rect,255
From: https://www.cnblogs.com/xuxiaobo/p/18375863

相关文章

  • pygame物体碰撞
    代码:#coding=utf-8importos,sys,re,timeimportpygameimportrandomimportmathfromwin32apiimportGetSystemMetricsfromtkinterimportmessageboxpygame.init()pygame.display.set_caption("我的游戏")percent=0.6screen_width=GetSystemMetri......
  • 【python】PyQt5中的QFrame控件,控制图形的边框样式、阴影效果、形状等属性
    ✨✨欢迎大家来到景天科技苑✨✨......
  • 【PyTorch学习6】张量形状操作
    reshapereshape函数用于改变数据的维度;#使用data.shapedata.size()查看数据大小#reshape前后元素个数不变data=torch.tensor([[1,2,3],[4,5,6]])#torch.Size([2,3])data1=data.reshape(3,2)#torch.Size([3,2])#使用-1省略形状data2=data.reshape(1......
  • 使用 Pygame 创建简单的移动方块游戏
    Pygame是一个用于开发图形和多媒体应用的优秀Python库。下面,我们将逐步解释如何创建一个简单的游戏,其中一个蓝色方块可以在屏幕上移动。 安装Pygame首先,确保你已经安装了Pygame。可以通过以下命令安装:pipinstallpygame 游戏结构1.初始化Pygame开始时,需......
  • pygame开发小游戏
    代码:#coding=utf-8importos,sys,re,timeimportpygameimportrandomfromwin32apiimportGetSystemMetricsfromtkinterimportmessageboxfromsqlparse.filtersimportright_margin#pyinstaller-F-wdaziyan.pypygame.init()pygame.display.set_caption(&......
  • 【python】pygame开发小游戏原来如此简单,掌握这几步就可以快速上手
    ✨✨欢迎大家来到景天科技苑✨✨......
  • 虚幻引擎5 C++基础 C++各类宏的作用
    1.GENERATED_BODY()//自动生成的宏,不直接使用父类的构造函数,如果需要做初始化操作,需要在自己的.h头文件中声明构造函数。在Cpp文件中实现,同时是private。//GENERATED_UCLASS_BODY(),使用父类的构造函数,进行初始化操作不需要.h文件中声明,可以直接在CPP文件中实现构造函数。//UPROP......
  • 只用一个 HTML 元素可以写出多少形状?——多边形篇
    上一篇章的末尾,我们只用一个 div 元素写了一个鸡蛋,在欧几里得平面几何中,鸡蛋的形状已经不能算是标准形状了。对于非标准的形状,没有比较直观的几何规律,命名方面也更加困难,俗称不规则图形,在欧几里得平面几何中,将其统称为多边形。在平行四边形篇中,我们首先使用常规的盒模型写出......
  • BUUCTF 81题吹着贝斯的二维码详解(包含各类工具和python脚本)
    在网上看了很多类似解题步骤和说明,感觉对小白都不友好,于是决定搜集整理下,做个详尽的解题步骤:压缩包解压得到36个无后缀名文件和一个flag.zip压缩包再看压缩包,解压发现有压缩密码,用winhex查看是不是伪加密,在末尾发现一串可疑字符串,拷贝下来留用:GNATOMJVIQZUKNJXGRCTGNRTG......
  • 黑马程序员|Linux2022第5章在Linux上部署各类软件(上)
    一MySQL数据库管理系统安装部署本节有四个部分:MySQL5.7版本在centos系统安装MySQL8.0版本在centos系统安装MySQL5.7版本在Ubuntu系统安装MySQL8.0版本在Ubuntu系统安装只选取8.0版本centos进行实操。 遇到的问题:1.刚开始是yum命令执行错误。2.密钥2022改为2023。......