我的代碼打印如下。這只是一個簡單的2D程序。如何在pygame中按照鼠標光標製作圖像
bif="C:\\Users\\Andrew\\Pictures\\pygame pictures\\Background(black big).png"
mif="C:\\Users\\Andrew\\Pictures\\pygame pictures\\bullet.png"
import pygame, sys
from pygame.locals import *
pygame.init()
from timeit import default_timer
screen=pygame.display.set_mode((1000,1000),0,32)
background=pygame.image.load(bif).convert()
bullet=pygame.image.load(mif).convert_alpha()
##Lines##
color=(255,255,255)
screen.lock()
pygame.draw.line(background, color, (30,970), (585,970))
pygame.draw.line(background, color, (585,-190), (585,970))
pygame.draw.line(background, color, (30,-190), (30,970))
screen.unlock()
## Horizontal Movement##
x=0
speedx= 0
dmx=0
## Vertical motion##
y=-190
dmy=0
clock=pygame.time.Clock()
speedy= 0
acceleration= 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
我的問題就在這裏:
elif event.type == pygame.MOUSEBUTTONDOWN:
## Horizontal ##
(mouseX, mouseY) = pygame.mouse.get_pos()
x=mouseX-87
speedx= 0
dmx=0
X1 = mouseX-87
## Vertical ##
y=mouseY-172
dmy=0
speedy= 0
acceleration= 0
Y1 = mouseY-172
elif event.type == pygame.MOUSEBUTTONUP:
(mouseX, mouseY) = pygame.mouse.get_pos()
## Horizontal ##
x=mouseX-87
speedx= 100
dmx=0
X2 = mouseX-87
## Vertical ##
y= mouseY-172
dmy=0
speedy= 1
acceleration= .5
y2 = mouseY - 87
screen.blit(background, (0,0))
screen.blit(bullet, (x, y))
我不知道如何讓子彈跟隨鼠標cursor.When鼠標按鈕被按下時,子彈出現,並仍然保持,沒有關於鼠標光標的移動量。當鼠標按鈕被釋放時,子彈即刻出現在該點。如何讓子彈沿着鼠標光標的路徑行進?
變量: DMX =距離在x軸對y軸 其餘 DMY =移動距離移動是不言自明的。