練習(xí)pygame
以下代碼為:在畫布上繪制一個背景圖,再繪制一個圖片隨著鼠標(biāo)的移動而移動文章來源:http://www.zghlxwxcb.cn/news/detail-523434.html
import pygame
# 定義畫布的大小
W = 900
H = 800
size = (W, H)
back = (0, 0, 0) # 背景色
screen = pygame.display.set_mode(size) # 創(chuàng)建一個窗口
pygame.display.set_caption("我是標(biāo)題11") # 設(shè)置標(biāo)題
back_img = pygame.image.load('1.jpg') # 加載背景大圖
mouse_img = pygame.image.load('2.jpg') # 加載鼠標(biāo)圖片
rect = back_img.get_rect() # 獲得背景圖的矩形位置
is_true = True # 定義一個循環(huán)開始結(jié)束的標(biāo)志
while is_true: # 游戲主循環(huán)
# 獲取事件,如果用戶點擊退出,則退出循環(huán)
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_true = False
# 設(shè)置背景色
screen.fill(back)
# 將背景大圖畫上去
screen.blit(back_img, rect)
# 獲取鼠標(biāo)位置
x, y = pygame.mouse.get_pos()
# 得到鼠標(biāo)圖片的坐標(biāo)(坐標(biāo)以窗口左上角為基準(zhǔn)點)
x -= mouse_img.get_width() / 2
y -= mouse_img.get_height() / 2
# 將鼠標(biāo)圖片畫上去
screen.blit(mouse_img, (x, y))
# 重新繪制整個屏幕對應(yīng)的窗口
pygame.display.flip()
效果為:文章來源地址http://www.zghlxwxcb.cn/news/detail-523434.html
到了這里,關(guān)于pygame--圖片隨鼠標(biāo)移動的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!