【问题标题】:How to make an image follow the mouse cursor in pygame如何在pygame中使图像跟随鼠标光标
【发布时间】:2016-08-02 07:03:45
【问题描述】:

我的代码打印在下面。它只是一个具有 2D 运动的简单程序。

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))

我不知道如何使子弹跟随鼠标光标。当按下鼠标按钮时,无论鼠标光标移动多少,子弹都会出现并保持静止。松开鼠标按钮时,子弹会立即出现在该点。如何让子弹跟随鼠标光标的路径?

变量: dmx = 在 x 轴上移动的距离 dmy = 在 y 轴上移动的距离 其余的都是不言自明的。

【问题讨论】:

    标签: python pygame mouse


    【解决方案1】:

    你需要在声明screen.blit(bullet,(x,y))之后添加声明pygame.display.update()

    【讨论】:

      【解决方案2】:

      如果你改变了

      event.type == pygame.MOUSEBUTTONUP

      到,

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

      【讨论】:

        猜你喜欢
        • 2022-07-12
        • 1970-01-01
        • 2022-06-27
        • 2020-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多