根據鍵盤方向鍵控制“不知火舞”的上下左右移動
其實我對Python能不能做大型游戲,還是執質疑態度。Python屬於高級語言,那么不可避免的是,它的運行速度沒有C語言快。機器語言到匯編語言,到面向過程再到面向對象,它們在電腦上的執行速度由快到慢,而游戲講究的就是快!一定要快!所以我覺得,Python,不適用於寫大型游戲。有的人不服了,Python語言是有C語言開發的,而C語言是做游戲的最有優勢的語言,所以Python也能做?
不可否認的是,Python的確有先天性的優勢,然而這些優勢並不體現在做游戲上面,它在網頁上面的優勢更大,Python的爬蟲等。但是,做些小游戲,Python是肯定不在話下的。pygame官方給的幀數為40~200,而我們人類肉眼能分辨的幀數不超過30,所以你是不會在玩着游戲的時候,一卡一頓的。
import pygame import sys from pygame.locals import * #初始化Pygame pygame.init() size = width,hight = 600,400 speed = [-2,1] bg = (255,200,255) #RGB顏色 #clock = pygame.time.Clock() #創建指定大寫的窗口 screen = pygame.display.set_mode(size) #設置窗口標題 pygame.display.set_caption('不知火舞') #加載圖片 turtle = pygame.image.load('turtle.png') #獲得圖像的位置矩形 position = 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 == KEYDOWN: if event.key == K_LEFT: turtle = l_head speed = [-1,0] if event.key == K_RIGHT: turtle = r_head speed = [1,0] if event.key == K_UP: speed = [0,-1] if event.key == K_DOWN: speed = [0,1] #移動圖像 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 > hight: speed[1] = -speed[1] #填充背景 screen.fill(bg) #雙緩沖 #更新圖像 screen.blit(turtle,position)#bilt方法將一個圖像覆蓋到另一個圖象上 #更新界面 pygame.display.flip() #延遲10毫秒 pygame.time.delay(10) #clock.tick(200) #什么是surface對象? #pygame 用來表示圖像的對象
額,很簡單的一個東西。基本沒什么難度,但重在堅持嘛!萬一做着做着就特么做了一個大大大大大大.....游戲了呢?
(有了這些我覺得我可以先做個貪吃蛇來玩玩?)