天津網站設計公司排名優(yōu)幫云排名優(yōu)化
目錄
前言
一、監(jiān)聽按鍵并作出判斷
?二、持續(xù)移動
?三、左右移動
?總結:
前言
今天開始正式操控我們的小蜜蜂了,之前學java的時候是有一個函數(shù)監(jiān)聽鼠標和鍵盤的操作,我們通過傳過來不同的值進行判斷,現(xiàn)在來看看python是否一樣的實現(xiàn)。
一、監(jiān)聽按鍵并作出判斷
?以我淺薄的知識判斷,流程應該為時刻監(jiān)聽鍵盤或者鼠標的操作,然后判斷鍵盤是否點擊的方向鍵,假如點擊一下向左移動,那么我們就將小蜜蜂的位置向左移動一個設定好的距離,然后再顯示在屏幕上。我們專門創(chuàng)建了一個模塊game_functions來存放游戲操作的代碼,那么我們在game_functions模塊里面編寫就性,下面我們看看代碼:
import sys
import pygamedef check_events(ship):for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type==pygame.KEYDOWN:if event.key == pygame.K_RIGHT:ship.rect.centerx += 1
def update_screen(new_setting,screen,ship):screen.fill(new_setting.bg_color)ship.blitme()pygame.display.flip()
可以看出,我們將check_events()函數(shù)進行了補充,增加了判斷,之前只是判斷是否點擊了關閉,現(xiàn)在增加了對鍵盤輸入的判斷,我們詳細分析一下:
1、首先判斷事件類型是否為鍵盤事件KEYDOWN。pygame.KEYDOWN是一個事件類型,指的是鍵盤上某個鍵被按下的事件。當鍵盤上的某個鍵被按下時,pygame會生成一個KEYDOWN事件,程序可以通過檢測這個事件來獲取鍵盤按鍵的信息,如按下的是哪個鍵、是否同時按下了Shift、Ctrl等修飾鍵。
2、當判斷我們確實是在鍵盤按下某個鍵后,進入下一步判斷,判斷到底是按了哪個鍵,代碼中我們目前只寫的按向右移動的鍵。常見的event.key值包括:
- pygame.K_UP/K_DOWN/K_LEFT/K_RIGHT表示方向鍵上下左右
- pygame.K_SPACE表示空格鍵
- pygame.K_ESCAPE表示Esc鍵
- pygame.K_RETURN表示回車鍵
- pygame.K_a到pygame.K_z表示26個字母鍵
3、當我們判斷匹配后,我們需要將小蜜蜂位置向右移動一格,那么我們需要改變Ship模塊里面的rect.centerx值,將它加1,那么我們就需要傳入ship,因此在定義check_events()是要設置參數(shù),將ship傳進來。
?我們將check_events()函數(shù)修改好以后,那么我們就需要在主函數(shù)里調用它,之前我們已經調用了check_events()函數(shù)用來判斷程序的關閉,但是現(xiàn)在因為要加傳參進去,所以略加修改,增加傳參ship就行。
import pygame
import settings
from ship import Ship
import game_functions as gfdef run_game():pygame.init()new_setting=settings.Settings()screen = pygame.display.set_mode((new_setting.screen_width,new_setting.screen_height))ship = Ship(screen)pygame.display.set_caption("Alien Invasion")while True:gf.check_events(ship)gf.update_screen(new_setting,screen,ship)run_game()
?
?
?通過運行程序,點擊右方向鍵,我們可以看出,小蜜蜂向右進行了移動。
?二、持續(xù)移動
?在操作的過程中,我發(fā)現(xiàn)我需要不停的點擊右移動鍵才能實現(xiàn)小蜜蜂不斷右移動,這是反人性的,以我多年打cs、街頭籃球、QQ飛車、泡泡堂的經驗來說,人類更習慣于點著不放實現(xiàn)持續(xù)移動,喜歡連發(fā),而不喜歡點射?!按篁摺焙苜N心的告訴我們下一步該怎么實現(xiàn)持續(xù)功能。
?“大蟒蛇”提供的思路是:不再以按下向右移動鍵為判斷小蜜蜂向右移動的條件,而是設置另一個變量(比如m),m初始值為0,如果按下右移動鍵,m為1,只要m等于1,小蜜蜂就向右移動,如果m等于0,小蜜蜂就不動。個人覺得思路可行,只需要再加一個判斷,判斷松開右移動鍵時,將0賦值給m。下面我們來看代碼
import pygameclass Ship():def __init__(self,screen):self.screen = screenself.image = pygame.image.load('cat.png')self.rect = self.image.get_rect()self.screen_rect = screen.get_rect()self.rect.centerx = self.screen_rect.centerxself.rect.bottom=self.screen_rect.bottomself.moving_right = Falsedef update(self):if self.moving_right:self.rect.centerx += 1def blitme(self):self.screen.blit(self.image,self.rect)
?我們看到,我們重寫了Ship模塊,不僅是增加了一個變量(moving_right就相當于我之前說的m,True和False就相當于1和0),還增加了函數(shù)update,將小蜜蜂的移動寫到了這里,那么我們的主函數(shù)和game_functions也要作出相應修改(為什么不寫在game_functions里?)
import sys
import pygamedef check_events(ship):for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type==pygame.KEYDOWN:if event.key == pygame.K_RIGHT:ship.moving_right = Trueelif event.type==pygame.KEYUP:if event.key == pygame.K_RIGHT:ship.moving_right = Falsedef update_screen(new_setting,screen,ship):screen.fill(new_setting.bg_color)ship.blitme()pygame.display.flip()
我們可以看出,在game_functions模塊里面,我們只需要將moving_right變量進行修改,就能控制小蜜蜂的移動。如果能理解前面我們的思路,那么KRYUP狀態(tài)就很好理解了,就是彈起或者說松開按鍵的意思。下面我們再在主程序對我們新建立的update函數(shù)調用就行了。
import pygame
import settings
from ship import Ship
import game_functions as gfdef run_game():pygame.init()new_setting=settings.Settings()screen = pygame.display.set_mode((new_setting.screen_width,new_setting.screen_height))ship = Ship(screen)pygame.display.set_caption("Alien Invasion")while True:gf.check_events(ship)ship.update()gf.update_screen(new_setting,screen,ship)run_game()
?三、左右移動
?上面我們已經實現(xiàn)了向右移動,那么向左移動就變得十分簡單,只需要在同樣的地方加一個判斷就行,這里建議大家自己寫,我們只需要在ship和game_functions模塊添加代碼就行。
?ship:
import pygameclass Ship():def __init__(self,screen):self.screen = screenself.image = pygame.image.load('cat.png')self.rect = self.image.get_rect()self.screen_rect = screen.get_rect()self.rect.centerx = self.screen_rect.centerxself.rect.bottom=self.screen_rect.bottomself.moving_right = Falseself.moving_left = Falsedef update(self):if self.moving_right:self.rect.centerx += 1if self.moving_left:self.rect.centerx -= 1def blitme(self):self.screen.blit(self.image,self.rect)
?game_functions:
import sys
import pygamedef check_events(ship):for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type==pygame.KEYDOWN:if event.key == pygame.K_RIGHT:ship.moving_right = Trueelif event.key == pygame.K_LEFT:ship.moving_left = Trueelif event.type==pygame.KEYUP:if event.key == pygame.K_RIGHT:ship.moving_right = Falseif event.key == pygame.K_LEFT:ship.moving_left = Falsedef update_screen(new_setting,screen,ship):screen.fill(new_setting.bg_color)ship.blitme()pygame.display.flip()
??
?總結:
?今天我們完成了小蜜蜂的左右移動,由點及面,窺一斑可見全豹,我們可以整理一下思路,之后我們只需要建立大黃蜂模塊和子彈模塊,然后在那兩個模塊里設置變量控制他們的移動和消失,就可以初步完成游戲的基本功能。