pygame动画制作
程序员文章站
2022-03-09 20:17:20
...
'''
画圆
'''
'''
绘制基本图形
'''
import pygame
print(pygame.ver)
import sys # 这个是退出程序要用的
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
WHITE=(255,255,255)
BLACK= (0,0,0)
RED=(255,0,0)
BLUE=(0,0,255)
GREEN=(0,255,0)
points=[(200,75),(300,25),(400,75),(450,75),(450,25),(450,125),(400,75),(300,125)]
size=width,height= 840,680
screen = pygame.display.set_mode(size)
pygame.display.set_caption("GGGG")
position= size[0]//2,size[1]//2
moving= False
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button ==1: #鼠标左键按下,注意1代表鼠标左键,不代表按下
moving = True
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1: # 鼠标左键松开
moving = False
if moving:
position= pygame.mouse.get_pos()
screen.fill(WHITE)
pygame.draw.rect(screen,BLACK,(50,50,150,50),0) #0代表填充,
pygame.draw.rect(screen, BLACK, (250, 50, 150, 50), 1)
pygame.draw.rect(screen, BLACK, (450, 50, 150, 50), 10)
pygame.draw.circle(screen, RED, position,25,1)
pygame.draw.circle(screen, GREEN, position, 75, 1)
pygame.draw.circle(screen, BLUE, position, 125, 1)
pygame.draw.ellipse(screen, BLUE, (50,50,150,50), 1) #限定矩形
pygame.draw.ellipse(screen, BLUE, (50, 50, 200, 200), 1)
import math
pygame.draw.arc(screen,BLUE,(50, 50, 440, 100),0,math.pi,1) #弧线就是从椭圆上取下来的
pygame.draw.lines(screen,GREEN,1,points,1)
pygame.draw.line(screen, GREEN, (100,200), (540,250), 1)
pygame.draw.aaline(screen, GREEN, (100, 200), (540, 350), 1)
pygame.draw.aaline(screen, GREEN, (100, 300), (540, 450), 0)
pygame.display.flip()
clock.tick(10)
'''
pygame具有的动能:1,绘制图形
2,显示图片
3,动画效果
4,与键盘、鼠标、和游戏手柄等外设交互(播放声音)
5,碰撞检测
新的东西
size=a,b=1,2 print(size)#(1, 2)
pycharm 使用方法:
ctrl 鼠标左键
pycharm 有些代码不提示,比如turtle.get_rect() 就不提示
第一节初步使用方法(关键的代码)
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Hello")
turtle=pygame.image.load("turtle.jpg")
position=turtle.get_rect()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(turtle,position) #这句话是说将turtle画到指的位置
pygame.display.flip() # 这就话是 显示到屏幕
pygame.time.delay(1)
问题:
1什么是surface对象 答:就是指图像
2,将一个图像绘制到另一个图像上是什么?
答:其实并不是移动到另一个图像上了,懂了。
3,移动图像怎么回事(帧率 表示 1s钟 可以切换多少图像 python 40到200帧数)
答:screen.fill(bg) #全部变成白色(在内存中工作)
screen.blit(turtle,position) #画图像 也在内存中 看不见
pygame.display.flip() # 在内存中画好了在 显示到屏幕
4如何控制游戏速度:
clock =pygame.time.Clock()
clock.tick(1) # 设置帧率为1,每秒切换1次图片
5,pygame效率高不高
(人类接受的流畅度为30帧,python在40到200
所以效率高,所有的游戏引擎都是基于C语言编写的)
6,从哪里获取帮助
'''
import pygame
print(pygame.ver)
import sys # 这个是退出程序要用的
pygame.init()
clock = pygame.time.Clock()
size = width, height = 1200, 600
speed = [-2, 1]
bg = (255, 255, 255)
# create a window of a given size
screen = pygame.display.set_mode(size) #创建一个背景,指定size大小
# set the title of windows
pygame.display.set_caption("Hello") # 设置窗口标题
# loading the picture
turtle = pygame.image.load("fish3.jpg") #图片都是矩形
position = turtle.get_rect() #这里得到一个矩形的左上角的位置
print(position) #<rect(0, 0, 280, 188)>
# help(turtle.get_rect)
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 == pygame.K_LEFT:
turtle = l_head
speed = [-5, 0]
if event.key == pygame.K_RIGHT:
turtle = r_head
speed = [5, 0]
if event.key == pygame.K_UP:
speed = [0, -5]
if event.key == pygame.K_DOWN:
speed = [0, 5]
position = position.move(speed)
if position.left < 0 or position.right > width:
# Flipped Picture
turtle = pygame.transform.flip(turtle, True, False)
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
# fill the background
screen.fill(bg) # 这一行如果注释掉 会很好玩
# update image
screen.blit(turtle, position) # 这句话是说将turtle画到指的位置
# update surface 双缓冲方式
pygame.display.flip() # 这就话是 显示到屏幕
# delay 10s
# pygame.time.delay(1)
clock.tick(200)
上一篇: 选择排序-直接选择排序
下一篇: Mac调整Launchpad图标大小