目錄
預備知識
鍵盤控制物體移動事件
鼠標點擊控制物體移動事件
鼠標移動的同時畫彩色小球
預備知識
pygame的基本函數(shù)介紹
1.初始化函數(shù),pygame的必備
?pygame.init()??
2.設置屏幕的大小
pygame.display.set_mode(size) //(長,寬)
3. 程序的命名
pygame.display.set_caption("string")
4.屏幕的顏色填充
screen.fill(R,B,G)??
5.圖片的載入
?pygame.image.load(''路徑'')
6.事件的獲取
pygame.event.get()?
7.畫圓
pygame.draw.circle(屏幕,顏色,圓心,半徑)
8.更新事件
?pygame.display.update()?
9.二維向量對象(多用于坐標)
pygame.math.Vector2()
10.返回向量的歐幾里得長度
pygame.math.Vector2.length()
11.規(guī)范化向量
pygame.math.Vector2.normalize_ip()?
12.鍵盤按下的事件
event.type == pygame.KEYDOWN
event.key == pygame.K_UP //向上
event.key == pygame.K_DOWN //向下
event.key == pygame.K_LEFT //向左
event.key == pygame.K_RIGHT? //向右
13. 鼠標的彈起事件
event.type == pygame.MOUSEBUTTONUP
好了,有了以上知識就可以開始施法了:
鍵盤控制物體移動事件
在屏幕上初始化一個外星人,用上、下、左、右鍵控制外星人移動
import sys
import pygame
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode(size)
screen.fill('white')
pygame.display.set_caption('外星人鍵盤移動事件')
img=pygame.image.load('C:/Users/leslie/Desktop/alien.png')
position = img.get_rect()
while True:
site = [0, 0]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
site[1] -= 10
if event.key == pygame.K_DOWN:
site[1] += 10
if event.key == pygame.K_LEFT:
site[0] -= 10
if event.key == pygame.K_RIGHT:
site[0] += 10
if event.type == pygame.MOUSEBUTTONDOWN:
xx,yy=event.pos
site=[xx,yy]
position = position.move(site)
screen.fill('white')
screen.blit(img, position)
pygame.display.flip()
?文章來源:http://www.zghlxwxcb.cn/news/detail-400626.html
鼠標點擊控制物體移動事件
在屏幕上初始化一個外星人,點擊鼠標使外星人移動到鼠標點擊的地方
import pygame
from pygame import Vector2
import sys
pygame.init()
pygame.display.set_caption('外星人鼠標點擊移動')
size = width, height = 600, 600
screen = pygame.display.set_mode(size)
color = (250, 250, 250)
alien = pygame.image.load('C:/Users/leslie/Desktop/alien.png')
alienrect = alien.get_rect()
start_position = Vector2(0,0)
speed = 6
mouse_xy = (0,0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_xy = Vector2(event.pos)
dis = mouse_xy - start_position
dis_lenth = dis.length()
if dis_lenth < speed:
mouse_xy = start_position
elif dis_lenth != 0:
dis.normalize_ip()
dis = dis*speed
start_position += dis
screen.fill(color)
screen.blit(alien, start_position)
pygame.display.flip()
?
鼠標移動的同時畫彩色小球
隨著鼠標的移動,每移動以下,在移動后的位置畫出顏色隨機的小球
import pygame
import sys
from random import randint
pygame.init()
screen = pygame.display.set_mode((500,500))
screen.fill('white')
pygame.display.set_caption("鼠標移動畫圓")
pygame.display.flip()
while True:
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mx,my = event.pos
pygame.draw.circle(screen,(255,255,0),(mx,my),20)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONUP:
pass
if event.type == pygame.MOUSEMOTION:
mx, my = event.pos
r = randint(0,255)
g = randint(0,255)
b = randint(0,255)
pygame.draw.circle(screen, (r,g,b,),(mx, my),20)
pygame.display.update()
?
其中的外星人圖片如下:?
?
當然,我們還可以給程序加上背景,假裝外星人在太空中遨游~~~文章來源地址http://www.zghlxwxcb.cn/news/detail-400626.html
到了這里,關于秒會Pygame:鍵盤移動和鼠標點擊移動物體的方法(含完整的代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!