資訊內(nèi)容
用python和pygame游戲編程入門-鼠標(biāo)設(shè)備
世界上最早的鼠標(biāo)誕生于1964年,它是由美國(guó)人道格·恩格爾巴特(Doug Engelbart)發(fā)明的。IEEE協(xié)會(huì)把鼠標(biāo)的發(fā)明列為計(jì)算機(jī)誕生50年來(lái)最重大的事件之一,可見其對(duì)IT歷程的重大影響作用。1983年蘋果公司給自家的電腦安上了鼠標(biāo),用戶就開始離不開這個(gè)小東西了。而現(xiàn)代游戲,離開了鼠標(biāo),99%的都沒法玩!我們自然得好好研究如何使用鼠標(biāo)來(lái)操控我們的游戲。
在3D游戲中,可以使用鼠標(biāo)來(lái)控制視角。這種時(shí)候,我們不使用鼠標(biāo)的位置,因?yàn)槭髽?biāo)可能會(huì)跑到窗口外面,我們使用鼠標(biāo)現(xiàn)在與上一幀的相對(duì)偏移量。在下面的例子中,我們演示使用鼠標(biāo)的左右移動(dòng)來(lái)轉(zhuǎn)動(dòng)我們熟悉的小魚
background_image_filename = './img/Underwater.png' sprite_image_filename = './img/fish-b.png' import pygame from pygame.locals import * from sys import exit from Vec2d import * from math import * pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) background = pygame.image.load(background_image_filename).convert() sprite = pygame.image.load(sprite_image_filename).convert_alpha() # 讓pygame完全控制鼠標(biāo)start pygame.mouse.set_visible(False) pygame.event.set_grab(True) # 讓pygame完全控制鼠標(biāo)end clock = pygame.time.Clock() sprite_pos = Vec2d(200, 150) sprite_speed = 300. sprite_rotation = 0. sprite_rotation_speed = 360. while True: for event in pygame.event.get(): if event.type == QUIT: exit() #完全控制鼠標(biāo),這樣鼠標(biāo)的光標(biāo)看不見,也不會(huì)跑到pygame窗口外面去所以你得準(zhǔn)備一句代碼來(lái)退出程序。 # 按Esc則退出游戲 if event.type == KEYDOWN: if event.key == K_ESCAPE: exit() pressed_keys = pygame.key.get_pressed() # 這里獲取鼠標(biāo)的按鍵情況 pressed_mouse = pygame.mouse.get_pressed() rotation_direction = 0. movement_direction = 0. # 獲得x方向上的偏移量,計(jì)算轉(zhuǎn)動(dòng),除以5是把動(dòng)作放慢一點(diǎn) rotation_direction = pygame.mouse.get_rel()[0]/5.0 if pressed_keys[K_LEFT]: rotation_direction = +1. if pressed_keys[K_RIGHT]: rotation_direction = -1. # 多了一個(gè)鼠標(biāo)左鍵按下的判斷 if pressed_keys[K_UP] or pressed_mouse[0]: movement_direction = +1. # 多了一個(gè)鼠標(biāo)右鍵按下的判斷 if pressed_keys[K_DOWN] or pressed_mouse[2]: movement_direction = -1. screen.blit(background, (0,0)) rotated_sprite = pygame.transform.rotate(sprite, sprite_rotation) w, h = rotated_sprite.get_size() sprite_draw_pos = Vec2d(sprite_pos.x-w/2, sprite_pos.y-h/2) screen.blit(rotated_sprite, sprite_draw_pos) time_passed = clock.tick() time_passed_seconds = time_passed / 1000.0 sprite_rotation += rotation_direction * sprite_rotation_speed * time_passed_seconds heading_y = sin(sprite_rotation*pi/180.) heading_x = cos(sprite_rotation*pi/180.) heading = Vec2d(heading_x, heading_y) heading *= movement_direction sprite_pos+= heading * sprite_speed * time_passed_seconds pygame.display.update()
pygame.mouse的函數(shù):
pygame.mouse.get_pressed —— 返回按鍵按下情況,返回的是一元組,分別為(左鍵, 中鍵, 右鍵),如按下則為True
pygame.mouse.get_rel —— 返回相對(duì)偏移量,(x方向, y方向)的一元組
pygame.mouse.get_pos —— 返回當(dāng)前鼠標(biāo)位置(x, y)
pygame.mouse.set_pos —— 顯而易見,設(shè)置鼠標(biāo)位置
pygame.mouse.set_visible —— 設(shè)置鼠標(biāo)光標(biāo)是否可見
pygame.mouse.get_focused —— 如果鼠標(biāo)在pygame窗口內(nèi)有效,返回True
pygame.mouse.set_cursor —— 設(shè)置鼠標(biāo)的默認(rèn)光標(biāo)式樣,是不是感覺我們以前做的事情白費(fèi)了?哦不會(huì),我們使用的方法有著更好的效果。
pyGame.mouse.get_cursor —— 不再解釋。
本站部分內(nèi)容轉(zhuǎn)載自網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系管理員及時(shí)刪除。
