簡體   English   中英

在pygame中用鼠標移動圖像

[英]moving a image with the mouse in pygame

我已經編寫了一些代碼來使用pygame移動名為燈泡的圖像(使用鼠標),但是當我將其移動到屏幕底部的左側時,它變得很爛,無法再次拾取,任何幫助都將是驚人的,謝謝你非常

import pygame
import math

pygame.init()

width = 800
height = 800

black = (0,0,0)
white = (255, 255, 255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((width, height)) #make the display
pygame.display.set_caption("car game") #title of the window
clock = pygame.time.Clock()  #frames per second defined here

bulb = pygame.image.load("bulb.png")

def bulbfunc(x,y):
    gameDisplay.blit(bulb, (x,y))


def mainloop():
    gameExit = False
    mouseDown = False
    x = width - 170
    y = 20
    while not gameExit:
        for event in pygame.event.get(): #when any action is done by the user
            if event.type == pygame.QUIT:
                quit()
        gameDisplay.fill(white)
        bulbfunc(x,y)
        clock.tick(60)
        pygame.display.update()


        clickstatus = pygame.mouse.get_pressed()
        (xs, ys) = pygame.mouse.get_pos()
        if mouseDown == False:
            if (x - 45) < xs < (x + 45) and (y - 45) < ys < (x + 45):
                print("true")
                print(x)
                if clickstatus[0] == 1:
                    mouseDown = True

        if mouseDown == True:
            if clickstatus[0] == 1:
                (x, y) = (xs, ys)
            else:
                mouseDown = False



mainloop()
pygame.exit()
exit()

您在此行中有錯別字:

if (x - 45) < xs < (x + 45) and (y - 45) < ys < (x + 45)

它應該是:

if (x - 45) < xs < (x + 45) and (y - 45) < ys < (y + 45)

請注意,結尾處是(y + 45),而不是(x + 45)

您有一行這樣的代碼:

if (x - 45) < xs < (x + 45) and (y - 45) < ys < (x + 45):

我假設您的意思是最后一個條件為(y + 45) 這可以解釋它卡在底部的左側,其中ys大而x小,所以從不滿足第二個條件。 如果不小心,復制和粘貼可能很危險!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM