简体   繁体   English

用鼠标单击在任意位置移动球

[英]Moving a Ball in Random place with mouse click

The following program I have provided below is one I developed to have a ball move around a screen randomly on its own free will. 我在下面提供的以下程序是我开发的程序,它可以使球以自己的意愿随意在屏幕上移动。 The next thing I have been trying to make it do is to have it move to a random location if you click the ball with a mouse. 我要尝试做的下一件事是,如果您用鼠标单击球,则将其移动到随机位置。 I've tried if statements but can not get any to work?? 我已经尝试过if语句,但是无法正常工作? Any ideas?? 有任何想法吗?? Would really appreciate the help! 非常感谢您的帮助!

   from pygame import * 
import random
init()
size = width, height = 800, 600
screen = display.set_mode(size)

#Setting up colour
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# setting up constants to help with the parts of the list
BALLX = 0
BALLY = 1
BALLSPEEDX = 2
BALLSPEEDY = 3

# function to set up a ball with random attributes
def initBall():
    ballx = random.randint(0, 800) # randomly setting the x position
    bally = random.randint(0, 600) # randomly setting the y position
    dirx = random.randint(-5,5)    # randomly setting the x speed
    diry = random.randint(-5,5)    # randomly setting the y speed
    data = [ballx, bally, dirx, diry]  # returning a list with all the data the ball needs
    return data # returning the list

def moveBall(data): # takes in the list of the ball
    data[BALLX] += data[BALLSPEEDX] # increases the position of the ball
    data[BALLY] += data[BALLSPEEDY]

    # checks to see if the ball is hitting the walls in the x direction
    if data[BALLX] > 800:
        data[BALLX] = 800
        data[BALLSPEEDX] *= -1
    elif data[BALLX] < 0:
        data[BALLX] = 0
        data[BALLSPEEDX] *= -1

    # checks to see if the ball is hitting the walls in the y direction
    if data[BALLY] < 0:
        data[BALLY] = 0
        data[BALLSPEEDY] *= -1
    elif data[BALLY] > 600:
        data[BALLY] = 600
        data[BALLSPEEDY] *= -1

    return data # returning the updated list

def drawScreen(data):   # sends a ball to be displayed
    draw.rect(screen, BLACK, (0, 0, 800, 600))
    # drawing a ball at the x and y position
    draw.circle(screen, RED, (data[BALLX], data[BALLY]), 10)
    display.flip()


running = True      # variable that controls the main loop
myClock = time.Clock()  # for controlling the frames per second

ball = initBall()   # initializing the ball
# Game Loop
while running == True:  # do this loop as long as running is True
    # events all ready
    for evnt in event.get():             # checks all events that happen
        if evnt.type == QUIT:
            running = False
        if evnt.type == MOUSEBUTTONDOWN:
            mx, my = evnt.pos          
            button = evnt.button

    # calling the draw screen function and sending the ball information
    drawScreen(ball)
    # moving the ball function, notice ball = the returned value
    ball = moveBall(ball)

    myClock.tick(60)        # waits long enough to have 60 frames per second

quit()

First you need to do is get a rect around your ball. 首先,您需要做的是在球周围摆正。

Ok, so basically what you need to do is , get the mouse coordinates and check if they collide with your ball image. 好的,基本上,您需要做的是,获取鼠标坐标并检查它们是否与您的球图像相撞。
You can use the following code in your loop to check if mouse coordinates collides with the ball(rect). 您可以在循环中使用以下代码来检查鼠标坐标是否与ball(rect)碰撞。

 for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            # Set the x, y postions of the mouse click
            x, y = event.pos
            if ballrect.collidepoint(x, y):
                ballrect.center=(random.randint(5,1060),random.randint(5,700))

                continue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM