zoukankan      html  css  js  c++  java
  • python-pygame

    1. 介绍
    Pygame
    	绘制图形,显示图片,动画效果,与键盘、鼠标、游戏手柄等外设交互,播放声音
    	
    	创建Surface对象,即Pygame里图像的对象
    	
    	pygame可以设置帧率
    	clock = pygame.time.Clock()
    	clock.tick(200)#200运行速度 
    	
    set_mode(resolution=(0,0),flags=0,depth=0) -> Surface对象
    	第一个参数尺寸大小
    	第二个参数:指定扩展选项,如全屏
    	第三个参数:指定颜色的位数(32),一般默认,python会自动根据当前操作系统选值 
    	
    	FULLSCREEN	全屏模式
    	DOUBLEBUF	双缓冲模式
    	HWSURFACE	硬件加速支持(只有在全屏模式下才能使用)
    	OPENGL		使用OpenGL渲染(3D模式使用)
    	PESIZABLE	使得窗口可以调整大小
    	NOFRAME		使得窗口没有边框和控制按钮
    
    1. 事件
    #事件
    import pygame
    import sys
    
    #初始化Pygame
    pygame.init()
    
    size = width,height = 600,400
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('title')
    
    bg = (0,0,0)
    
    font = pygame.font.Font(None,20)
    line_height = font.get_linesize()
    position = 0
    
    screen.fill(bg)
    
    ##f = open('record.txt','w')
    
    while True:
        for event in pygame.event.get():
    ##        f.write(str(event) + '
    ')
    
            if event.type == pygame.QUIT:
    ##            f.close()
                sys.exit()
    
            fontSur = font.render(str(event),True,(0,255,0))#消除锯齿 字体渲染后的survice对象
            screen.blit(fontSur,(0,position))#把字体渲染后的对象放到screen屏幕对象上
            position += line_height
    
            if position > height:
                position = 0 #屏幕清空
                screen.fill(bg)
        pygame.display.flip()
    
    
    1. 绘制基本图形
    pygame绘制基本图形
    	rect(Surface, color, Rect, width=0)
    	
    	pygame.draw.circle(Surface,color,position,width,height)
    	
    	line(Surface,color,start_pos,end_pos,width=1)
    	lines(Surface,color,closed,pointlist,width=1)
    	
    	aaline(Surface,color,start_pos,end_pos,blend=1)#看锯齿blend看阴影
    	aalines(Surface,color,closed,pointlist,blend=1)
    
    #pygame绘制基本图形
    import pygame
    import sys
    from pygame.locals import *
    
    pygame.init()
    
    WHITE = (255,255,255)
    BLACK = (0,0,0)
    
    size = width, height = 640,200
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('hello')
    
    clock = pygame.time.Clock()
    
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
    
        screen.fill(WHITE)
    
        pygame.draw.rect(screen,BLACK,(50,50,150,50),0)
        pygame.draw.rect(screen,BLACK,(250,50,150,50),1)
        pygame.draw.rect(screen,BLACK,(450,50,150,50),10)
    
        pygame.display.flip()#把加载到内存的给反转到显示器
    
        clock.tick(10)
    
    1. 键盘事件、旋转、缩放
    #pygame 是个包,需要初始化
    import pygame
    import sys
    from pygame.locals import *
    
    #初始化Pygame
    pygame.init()
    
    clock = pygame.time.Clock()#帧率对象
    
    size = width,height = 600,400
    bg = (255,255,255) #RGB
    
    
    #创建指定大小的窗口 Suface对象
    screen = pygame.display.set_mode(size,RESIZABLE)
    #设置窗口标题
    pygame.display.set_caption("初次见面")
    
    #加载图片
    turtle = pygame.image.load('turtle.png')
    #获得图像的位置矩形
    position = turtle.get_rect()
    
    fullscreen = False
    
    #设置放缩小比率
    ratio = 1.0
    
    oturtle = pygame.image.load('turtle.png')
    turtle = oturtle
    oturtle_rect = oturtle.get_rect()
    position = turtle_rect = oturtle_rect
    
    #旋转贴边
    speed = [5,0]
    turtle_right = pygame.transform.rotate(turtle,90)
    turtle_top = pygame.transform.rotate(turtle,180)
    turtle_left = pygame.transform.rotate(turtle,270)
    turtle_bottom = turtle
    turtle = turtle_top
    
    l_head = turtle#头像左转
    r_head = pygame.transform.flip(turtle, True,False)#头像右转
    
    while True:
        for event in pygame.event.get():#迭代取出所有事件
            if event.type == pygame.QUIT:
                sys.exit()
                
    ##        if event.type == pygame.KEYDOWN:
    ##            if event.key == K_LEFT:
    ##                turtle = l_head
    ##                speed = [-1,0]
    ##            if event.key == K_RIGHT:
    ##                turtle = r_head
    ##                speed = [1,0]              
    ##            if event.key == K_UP:
    ##                turtle = l_head
    ##                speed = [0,-1]
    ##            if event.key == K_DOWN:
    ##                turtle = l_head
    ##                speed = [0,1]
    
                #(全屏) F11
                if event.key == K_F11:
                    fullscreen = not fullscreen
                    if fullscreen:
                        screen = pygame.display.set_mode((1360,768),FULLSCREEN | HWSURFACE)
                        width,height=1360,768
                    else:
                        screen = pygame.display.set_mode(size)
    
                #放大、缩小小乌龟(=、-)空格键恢复原始尺寸
                if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
                    #最大只能放大一倍 ,缩50%
                    if event.key == K_EQUALS and ratio < 2:
                        ratio += 0.1
                    if event.key == K_MINUS and ratio > 0.5:
                        ratio -= 0.1
                    if event.key == K_SPACE:
                        ratio = 1.0
    
                    turtle = pygame.transform.smoothscale(oturtle,
                                                          (int(oturtle_rect.width * ratio),
                                                           int(oturtle_rect.height * ratio)))
    
                    l_head = turtle
                    r_head = pygame.transform.flip(turtle,True,False)
            #用户调整窗口尺寸
            if event.type == VIDEORESIZE:
                size = event.size
                width,height = size
                print(size)
                screen = pygame.display.set_mode(size,RESIZABLE)
    
        #移动图像
        position = position.move(speed)
    
    ##    if position.left < 0 or position.right >
    ##        #翻转图像
    ##        turtle = pygame.transform.flip(turtle, True, False)
    ##        #反方向移动
    ##        speed[0] = -speed[0]
    ##
    ##    if position.top < 0 or position.bottom > height:
    ##        speed[1] = -speed[1]
    
        if position.right >
            turtle = turtle_right
            position = turtle_rect = turtle.get_rect()
            position.left = width - turtle_rect.width
            speed = [0,5]
        if position.bottom >height:
            turtle = turtle_height
            position = turtle_rect = turtle.get_rect()
            position.left = width - turtle_rect.width
            position.top = height - turtle_rect.height
            speed = [-5,0]
        if position.left <0:
            turtle = turtle_left
            position = turtle_rect = turtle.get_rect()
            position.top = height - turtle_rect.height
            speed = [0,-5]
        if position.top <0:
            turtle = turtle_top
            position = turtle_rect = turtle.get_rect()
            speed = [5,0]
    
            
        #填充背景
        screen.fill(bg)
        #更新图像
        screen.blit(turtle,position)
        #更新界面
        pygame.display.flip()
        #延迟10毫秒
    ##    pygame.time.delay(10)
        clock.tick(200)#不超过200运行速度
        
    
    
    1. 透明度
    import pygame
    import sys
    from pygame.locals import *
    
    pygame.init()
    
    size = width,height = 640,480
    bg = (0,0,0)
    
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('bat.com')
    
    turtle = pygame.image.load('turtle.png').convert_alpha()
    background = pygame.image.load('bg.jpg').convert()
    position = turtle.get_rect()
    position.center = width//2,height//2
    
    def blit_alpha(target,source,location,opacity):
        x = location[0]
        y = location[1]
        temp = pygame.Surface((source.get_width(),source.get_height())).convert()
        temp.blit(target,(-x,-y))
        temp.blit(source,(0,0))
        temp.set_alpha(opacity)
        target.blit(temp,location)
    
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
    
        screen.blit(background,(0,0))
        blit_alpha(screen,turtle,position,200)
    
        pygame.display.flip()
    
        clock.tick(30)
    
    
    
  • 相关阅读:
    c++ 中bool 的默认值
    cocos2d CCLOG格式符号表
    c++数组指针bug
    cocos2d-x-2.2.6创建工程
    Nape实现坐标旋转角度回弹
    haxe 中使用音效
    haxe 嵌入swf 读取里面的内容
    haxe 配置
    Spring Tool Suite(STS)基本安装配置
    git提交忽略文件.gitignore内容
  • 原文地址:https://www.cnblogs.com/cgy-home/p/15017609.html
Copyright © 2011-2022 走看看