• pygame初步(一)绘制一个运动的矩形


    <More Python Programming for the Absolute Beginner>一书中的第二章练习3(P33)

    使用Python的Pygame库

    import sys, pygame, random    #导入库文件
    from pygame.locals import *
    
    pygame.init()  #初始化
    
    white= 0, 0, 0  #定义颜色
    pos_x= 300    #定义初始位置
    pos_y= 250
    vel_x= 0.2
    vel_y= 0.1 
    
    screen= pygame.display.set_mode((600, 500))  #获取窗口对象
    #myfont= pygame.font.Font(None, 60)
    #textImage= myfont.render("Hello, Pygame!", True, white)
    pygame.display.set_caption("Drawing Moving Rectangle")  #窗口名字
    
    color_r= 255
    color_g= 0
    color_b= 0
    color= color_r, color_g, color_b  #定义初始颜色
    
    while True:
        for event in pygame.event.get():
            if event.type in (QUIT, KEYDOWN):
            sys.exit()
        screen.fill(white)
    
        pos_x+= vel_x
        pos_y+= vel_y
    
        if pos_x> 500 or pos_x< 0:  #若x轴超出范围,则
          color_r= random.randint(0, 255)  
          color_g= random.randint(0, 255)
          color_b= random.randint(0, 255)
          color= color_r, color_g, color_b    #则矩形边的颜色随机改变
          vel_x= -vel_x  #速度方向相反
    
        if pos_y> 400 or pos_y< 0:  #类似上面
          color_r= random.randint(0, 255)
          color_g= random.randint(0, 255)
          color_b= random.randint(0, 255)
          color= color_r, color_g, color_b
          vel_y= -vel_y
    
        position= pos_x, pos_y, 100, 100  
        width= 1
        pygame.draw.rect(screen, color, position, width)  #画矩形
    
    #    screen.blit(textImage, (100, 100))
        pygame.display.update()  #更新
       

    第一个Pygame程序,如有建议,敬请提出。

    欢迎交流。

  • 相关阅读:
    Spring 整合Hibernate与Struts2
    Spring @注解详解(转)
    Spring 事务
    Spring c3p0支持Hibernate配置
    Spring c3p0连接池配置
    Spring dbcp连接池配置与示例(以及JDBCTemplate的使用)
    struts转换器详解
    struts拦截器详解
    struts拦截器的使用
    OGNL表达式详解
  • 原文地址:https://www.cnblogs.com/ruchicyan/p/4659503.html
Copyright © 2020-2023  润新知