2014-10-19 101 views
0

我的代码打印如下。这只是一个简单的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 =移动距离移动是不言自明的。

回答

1

您需要的声明screen.blit(bullet,(x,y))

0

后添加语句pygame.display.update()如果更改

event.type == pygame.MOUSEBUTTONUP

到,

event.type == pygame.MOUSEMOTION,它会跟随你的鼠标。