python 图形pygame模板的使用

2017-04-11 117点热度 1人点赞

保持pygame窗口打开

import pygame

pygame.init()

screen =pygame.display.set_mode([640,480])

while True:
	pass

 

#让那个叉叉可以关闭窗口
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255]) #用白色背景填充窗口
pygame.draw.circle(screen,[255,0,0],[100,100],30,0) #画一个圆
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
           sys.exit()

 

#让那个叉叉可以关闭窗口
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255]) #用白色背景填充窗口
my_list=[250,150,300,300]
pygame.draw.rect(screen,[255,0,0],my_list,0) #矩形的颜色,位置和线宽(填充)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
           sys.exit()

 

 

 

python 图形pygame模板的使用

 

#使用draw.rect 实现艺术创作
import pygame, sys, random
pygame.init()
screen =pygame.display.set_mode([640,640])
screen.fill([255,255,255])
for i in range (100):
    width = random.randint(0,250)
    height = random.randint(0,100)
    top = random.randint(0,400)
    left = random.randint(0,500)
    pygame.draw.rect(screen,[0,0,0],[left,top,width,height],1)
pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

python 图形pygame模板的使用

 

 

 

 

#使用draw.rect 实现艺术创作
import pygame, sys, random
from pygame.color import THECOLORS
pygame.init()
screen =pygame.display.set_mode([640,640])
screen.fill([255,255,255])
for i in range (100):
    width = random.randint(0,250)
    height = random.randint(0,100)
    top = random.randint(0,400)
    left = random.randint(0,500)
    color_name = random.choice(list(THECOLORS.keys()))
    color = THECOLORS[color_name]
    line_width = random.randint(1,3)
    pygame.draw.rect(screen,color,[left,top,width,height])
pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
'''
在2.7中,找到key所对应的第一个元素为:firstStr = myTree.keys()[0],这在3.4中运行会报错:'dict_keys' object does not support indexing,这是因为python3改变了dict.keys,返回的是dict_keys对象,支持iterable 但不支持indexable,我们可以将其明确的转化成list,则此项功能在3中应这样实现:

firstSides = list(myTree.keys()) 
firstStr = firstSides[0]#找到输入的第一个元素
'''

python 图形pygame模板的使用

 

#一条完美连接的正弦曲线
import pygame, sys
import math #导入数学函数,包含sin()
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
plotPoints =[]
for x in range(0,640):
    y =int(math.sin(x/640.0*4*math.pi)*200+240)#计算各个点的y坐标
    plotPoints.append([x,y])#将各个点添加到列表
pygame.draw.lines(screen,[0,0,0],False,plotPoints,1)#用函数画出整条曲线
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

python 图形pygame模板的使用

 

 

 

 

#在pygame窗口中显示图片
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_ball = pygame.image.load("1.png")
screen.blit(my_ball,[50,50])
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

 

#在pygame窗口中移动图片
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([1000,800])
screen.fill([255,255,255])
tu = pygame.image.load("tu.png")
screen.blit(tu,[50,50])
pygame.display.flip()
#以下3行是上面的代码加上的
pygame.time.delay(2000)
screen.blit(tu,[150,50])#tu是图片名
pygame.display.flip()
pygame
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

 

#在pygame窗口中移动图片,带擦除,就是动画啦
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
tu = pygame.image.load("tu.png")
screen.blit(tu,[50,50])
pygame.display.flip()

pygame.draw.rect(screen,[255,255,255],[50,50,900,900],0)#擦除第一个球
#创建的矩形左上角距离窗口右边界50像素,距离窗口上边界50像素,宽90,高90
pygame.time.delay(2000)
screen.blit(tu,[150,50])#tu是图片名

pygame.display.flip()
pygame
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

 

可以用Surface.set_at()方法访问一个表面上的单个像素,指出设置的像素和颜色 screen.set_at([x,y],[0,0,0]),screen是表面的名字

 

 

#在pygame窗口中移动图片,带擦除,就是动画啦,连续移动
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([900,600])
screen.fill([255,255,255])
tu = pygame.image.load("tu.png")
x=50
y=50
screen.blit(tu,[x,y])
pygame.display.flip()
for looper in range (1,100):
    pygame.time.delay(20)
    pygame.draw.rect(screen,[255,255,255],[x,y,900,900],0)
    x=x+5
    screen.blit(tu,[x,y])
    pygame.display.flip()
pygame.draw.rect(screen,[255,255,255],[50,50,900,900],0)#擦除第一个球
#创建的矩形左上角距离窗口右边界50像素,距离窗口上边界50像素,宽90,高90
pygame.time.delay(2000)
screen.blit(tu,[150,50])#tu是图片名

pygame.display.flip()
pygame
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

 

#在pygame窗口中移动图片,带擦除,就是动画啦,连续移动,左右移动
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([900,600])
screen.fill([255,255,255])
tu = pygame.image.load("tu.png")
x=50
y=50
flag=1;
screen.blit(tu,[x,y])
pygame.display.flip()
for looper in range (1,100000):
    pygame.time.delay(20)
    pygame.draw.rect(screen,[255,255,255],[x,y,900,900],0)
    if looper%100 ==0:
        if flag==1:
            flag=0
        else:
            flag=1
    if flag==1:
        x+=5
    else:
        x-=5
    screen.blit(tu,[x,y])
    pygame.display.flip()
pygame.draw.rect(screen,[255,255,255],[50,50,900,900],0)#擦除第一个球
#创建的矩形左上角距离窗口右边界50像素,距离窗口上边界50像素,宽90,高90
pygame.time.delay(2000)
screen.blit(tu,[150,50])#tu是图片名

pygame.display.flip()
pygame
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

#17-1 使用动画精灵在屏幕上放多个沙滩球图像
import sys,pygame

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location

size = width,heigh = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
balls = []
for row in range (0,3):
    for column in range (0,3):
        location = [column *180 +10,row * 180 +10]
        ball = MyBallClass(img_file,location)
        balls.append(ball)
for ball in balls:
    screen.blit(ball.image,ball.rect)
pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#17-2 使用动画精灵移动球的程序
import sys,pygame
from random import *

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location
        self.speed = speed
    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = - self.speed[0]
            
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]
size = width,height = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
balls = []
for row in range (0,3):
    for column in range (0,3):
        location = [column *180 +10,row * 180 +10]
        speed = [choice([-2,2]),choice([-2,2])]
        ball = MyBallClass(img_file,location,speed)
        balls.append(ball)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.time.delay(20)
    screen.fill([255,255,255])
    for ball in balls:
        ball.move()
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
pygame.quit()

#17-3 使用精灵组和不是列表

import sys,pygame
from random import *

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location
        self.speed = speed
    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = - self.speed[0]
            
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

def animate(group):
    screen.fill([255,255,255])
    for ball in group:
        group.remove(ball)
        if pygame.sprite.spritecollide(ball,group,False):
            ball.speed[0] = -ball.speed[0]
            ball.speed[1] = -ball.speed[1]
        group.add(ball)
        ball.move()
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
    pygame.time.delay(50)
size = width,height = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
group = pygame.sprite.Group()

for row in range (0,2):
    for column in range (0,2):
        location = [column *180 +10,row * 180 +10]
        speed = [choice([-5,5]),choice([-5,5])]
        ball = MyBallClass(img_file,location,speed)
        group.add(ball)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    animate(group)
pygame.quit()

#17-3修改不会卡和颤抖

import sys,pygame
from random import *

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location
        self.speed = speed
    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = - self.speed[0]
            
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

def animate(group):
    screen.fill([255,255,255])
    for ball in group: #先移动所有球
        ball.move()
    for ball in group:
        group.remove(ball)
        if pygame.sprite.spritecollide(ball,group,False):
            ball.speed[0] = -ball.speed[0]
            ball.speed[1] = -ball.speed[1]
        group.add(ball)
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
    pygame.time.delay(20)
size = width,height = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
group = pygame.sprite.Group()

for row in range (0,2):
    for column in range (0,2):
        location = [column *180 +10,row * 180 +10]
        speed = [choice([-5,5]),choice([-5,5])]
        ball = MyBallClass(img_file,location,speed)
        group.add(ball)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    animate(group)
pygame.quit()
#17-4沙滩球程序中使用clock和get_fps

import sys,pygame
from random import *

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location
        self.speed = speed
    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = - self.speed[0]
            
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

def animate(group):
    screen.fill([255,255,255])
    for ball in group: #先移动所有球
        ball.move()
    for ball in group:
        group.remove(ball)
        if pygame.sprite.spritecollide(ball,group,False):
            ball.speed[0] = -ball.speed[0]
            ball.speed[1] = -ball.speed[1]
        group.add(ball)
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
    
size = width,height = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
clock = pygame.time.Clock()
group = pygame.sprite.Group()

for row in range (0,2):
    for column in range (0,2):
        location = [column *180 +10,row * 180 +10]
        speed = [choice([-5,5]),choice([-5,5])]
        ball = MyBallClass(img_file,location,speed)
        group.add(ball)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            frame_rate = clock.get_fps()#检查帧速率
            print("frame rate = ",frame_rate)
    animate(group)
    clock.tick(10)
pygame.quit()

 

 

 

 

未经允许不得转载!python 图形pygame模板的使用

本文地址:https://ai.52learn.online/619

站长邮箱:ai52learn@foxmail.com