python学习之游戏开发第一篇

游戏开发

pygame模块能干什么呢?

1.绘制图形
  矩形 菱形、
2.显示图片
3.显示动画效果
4.与键盘,鼠标和游戏手柄等外设交互
5.播放声音
6.碰撞检测

做一个电视屏保程序



import pygame
import sys


#初始化pygame
pygame.init()

size = width,height = 600,400
speed = [-2,1]
bg = (255,255,255)#RGB


clock = pygame.time.Clock()
#创建指定大小的窗口
screen = pygame.display.set_mode(size)

#设置窗口标题
pygame.display.set_caption('初次见面,请多关照!')


#加载图片
turtle = pygame.image.load("D:/Study/2.gif")

#获得图像的位置矩形
position = turtle.get_rect()


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


    #移动图像
    position = position.move(speed)

    if position.left < 0 or position.right > width:
        #翻转图像
        turtle = pygame.transform.flip(turtle,True,False)
        #反方向移动
        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]


    #填充背景
    screen.fill(bg)
    #更新图像
    screen.blit(turtle,position)
    #更新界面
    pygame.display.flip()
    #延迟10秒
    #pygame.time.delay(50)
    #设置帧率
    clock.tick(200)






















什么是surface对象

什么是双缓冲

帧率

由于显卡的速度质量不一样,所以游戏的运行效率不够高

oldalien游戏

原创文章 59 获赞 39 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_28174951/article/details/79356879