Python游戏开发-01-初探

运行环境:window7 64位,IDLE (Python 3.6 64-bit),pygame-1.9.3

import pygame
import sys

#初始化Pygame
pygame.init()

size = width,height = 600,400 #实际上是元组
speed = [-2,1]
bg =(255,255,255) #rgb,white

#创建指定大小的窗口
screen = pygame.display.set_mode(size)
#设置标题
pygame.display.set_caption("木尧-pygame初探")
#加载图
muyao = pygame.image.load("muyao.png")
#获得图像位置矩形
position = muyao.get_rect()

#死循环,一直动
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    #move
    position = position.move(speed)

    if position.left < 0 or position.right > width:
        #水平翻转
        muyao = pygame.transform.flip(muyao,True,False) #muyao,水平yes,垂直no
        #反向
        speed[0] = -speed[0]

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

    #填充背景
    screen.fill(bg)
    #更新图像
    screen.blit(muyao,position)
    #更新界面(双缓冲)
    pygame.display.flip()
    #延迟10ms
    pygame.time.delay(20)  
(muyao.png)

运行效果:(碰壁会自动水平翻转)

调节游戏速度另解:

clock = pygame.time.Clock()

clock.tick(200)#不高于200帧





猜你喜欢

转载自blog.csdn.net/muyao987/article/details/79099428