国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現在的位置是:首頁 » python編程資訊

資訊內容

用python和pygame游戲編程入門-鼠標設備

極客小將2020-10-28-
在3D游戲中,可以使用鼠標來控制視角。這種時候,我們不使用鼠標的位置,因為鼠標可能會跑到窗口外面,我們使用鼠標現在與上一幀的相對偏移量。

世界上最早的鼠標誕生于1964年,它是由美國人道格·恩格爾巴特(Doug Engelbart)發明的。IEEE協會把鼠標的發明列為計算機誕生50年來最重大的事件之一,可見其對IT歷程的重大影響作用。1983年蘋果公司給自家的電腦安上了鼠標,用戶就開始離不開這個小東西了。而現代游戲,離開了鼠標,99%的都沒法玩!我們自然得好好研究如何使用鼠標來操控我們的游戲。

在3D游戲中,可以使用鼠標來控制視角。這種時候,我們不使用鼠標的位置,因為鼠標可能會跑到窗口外面,我們使用鼠標現在與上一幀的相對偏移量。在下面的例子中,我們演示使用鼠標的左右移動來轉動我們熟悉的小魚


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完全控制鼠標start
pygame.mouse.set_visible(False)
pygame.event.set_grab(True) 
# 讓pygame完全控制鼠標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()
		#完全控制鼠標,這樣鼠標的光標看不見,也不會跑到pygame窗口外面去所以你得準備一句代碼來退出程序。
		# 按Esc則退出游戲
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                exit()

    pressed_keys = pygame.key.get_pressed()
	# 這里獲取鼠標的按鍵情況
    pressed_mouse = pygame.mouse.get_pressed()

    rotation_direction = 0.
    movement_direction = 0.
 
    # 獲得x方向上的偏移量,計算轉動,除以5是把動作放慢一點
    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.

    # 多了一個鼠標左鍵按下的判斷
    if pressed_keys[K_UP] or pressed_mouse[0]:
        movement_direction = +1.
    # 多了一個鼠標右鍵按下的判斷
    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的函數:

pygame.mouse.get_pressed —— 返回按鍵按下情況,返回的是一元組,分別為(左鍵, 中鍵, 右鍵),如按下則為True

pygame.mouse.get_rel —— 返回相對偏移量,(x方向, y方向)的一元組

pygame.mouse.get_pos —— 返回當前鼠標位置(x, y)

pygame.mouse.set_pos —— 顯而易見,設置鼠標位置

pygame.mouse.set_visible —— 設置鼠標光標是否可見

pygame.mouse.get_focused —— 如果鼠標在pygame窗口內有效,返回True

pygame.mouse.set_cursor —— 設置鼠標的默認光標式樣,是不是感覺我們以前做的事情白費了?哦不會,我們使用的方法有著更好的效果。

pyGame.mouse.get_cursor —— 不再解釋。


本站部分內容轉載自網絡,如有侵權請聯系管理員及時刪除。

預約試聽課

已有385人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 馆陶县| 蕉岭县| 图木舒克市| 广安市| 静乐县| 安达市| 天全县| 许昌市| 阿拉善左旗| 尚义县| 辉南县| 越西县| 南漳县| 清水河县| 奉新县| 顺昌县| 长阳| 临夏市| 义马市| 黄冈市| 唐海县| 卢氏县| 麻栗坡县| 莲花县| 海安县| 郧西县| 泾阳县| 镇安县| 湘西| 英德市| 改则县| 岐山县| 广德县| 富裕县| 迭部县| 正镶白旗| 河南省| 白银市| 视频| 沂南县| 资阳市|