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遊戲

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章