在pygame中用鼠标移动图像

时间:2015-06-15 16:44:07

标签: python pygame

我已经制作了一些代码,使用pygame移动一个名为bulb的图像(使用鼠标),但当我将它移动到屏幕底部的左侧时,它会变得很糟糕,并且无法再次拾起任何帮助太棒了,非常感谢你

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

2 个答案:

答案 0 :(得分:1)

你在这一行有一个错字:

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

应该是:

string colorStr = "rgb(67,134,215)";

Regex regex = new Regex(@"rgb\((?<r>\d{1,3}),(?<g>\d{1,3}),(?<b>\d{1,3})\)");
Match match = regex.Match(colorStr);
if (match.Success)
{
    int r = int.Parse(match.Groups["r"].Value);
    int g = int.Parse(match.Groups["g"].Value);
    int b = int.Parse(match.Groups["b"].Value);

    //// Create your new object with the r, g, b values
}

注意它的末尾是(y + 45)而不是(x + 45)

答案 1 :(得分:1)

你有一行代码如下:

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

我认为你的意思是将最后一个条件设为(y + 45)。这可以解释它卡在底部的左侧,ys很大且x很小,因此第二个条件永远不会实现。如果你不小心,复制和粘贴可能很危险!