簡體   English   中英

用鼠標單擊在任意位置移動球

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

我在下面提供的以下程序是我開發的程序,它可以使球以自己的意願隨意在屏幕上移動。 我要嘗試做的下一件事是,如果您用鼠標單擊球,則將其移動到隨機位置。 我已經嘗試過if語句,但是無法正常工作? 有任何想法嗎?? 非常感謝您的幫助!

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

首先,您需要做的是在球周圍擺正。

好的,基本上,您需要做的是,獲取鼠標坐標並檢查它們是否與您的球圖像相撞。
您可以在循環中使用以下代碼來檢查鼠標坐標是否與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