在opygame中,im试图举起一个球,但卡住了

问题描述 投票:3回答:3
import pygame

Red = 255, 0, 0
Black= 0,0,0
rectXpos = 2
rectypos = 2
speed = 2
screenedgex = 500

pygame.init()

window = pygame.display.set_mode(size=(500, 500))

clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        pygame.display.update()

        window.fill(Black)
        square = pygame.draw.rect(window, Red, [rectXpos, rectypos, 50, 50],2)
        rectXpos += 2

        if rectXpos < 500:
            rectXpos -= 2



        clock.tick(60)
        print(rectXpos)`enter code here`

所以我在做什么错?我尝试制作if语句来停止球并反转它,但它使球保持在窗口的边缘

python if-statement pygame bounce
3个回答
0
投票

这是完整的代码,我分开了x和y跳动,因此您可以使用其中任何一个,也可以更新代码一点,再加上一些额外的格式。

# Imports
import pygame

# Vars
Red = 255, 0, 0
Black= 0,0,0
rectXpos = 2
rectYpos = 2
rect_width = 50
rect_height = 50
screen_width = 500
screen_height = 500
block_x_direction = 1
block_y_direction = 1

# Setup Code
pygame.init()
window = pygame.display.set_mode(size=(screen_width, screen_height))
clock = pygame.time.Clock()
running = True

# Game Loop
########################################################
while running:
    # Event Loop
    ########################################################
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    # Game Code - Update
    ########################################################
    # Game Code - Update - Rect X Bounce
    if rectXpos + (rect_width)>= screen_width:
        block_x_direction = block_x_direction * -1
    rectXpos += 2 * block_x_direction
    # Game Code - Update - Rect Y Bounce
    if rectYpos + (rect_height)>= screen_height:
        block_y_direction = block_y_direction * -1
    rectYpos += 2 * block_y_direction

    # - Tick Game    
    clock.tick(60)

    # Game Code - Render
    ########################################################
    window.fill(Black)
    square = pygame.draw.rect(window, Red, [rectXpos, rectYpos, rect_width, rect_height],2)
    pygame.display.update()

    # Game Code - Debug Code
    ########################################################
    print(clock.tick)

0
投票

您应该在位置上添加speed,并且当触摸边框时,您应该将speed更改为-speed

您还可以使用pygame.Rect()来保持位置和大小-它具有.left.right(及其他)属性,这些属性可能非常有用。您可以使用Rect绘制pygame.draw.rect()(或检查与其他Rect的碰撞)

import pygame

# --- constants --- (UPPER_CASE_NAMES)

RED = (255, 0, 0)
BLACK = (0, 0, 0)

WIDTH = 500
HEIGHT = 500

# --- main ---

speed = 10

pygame.init()

window = pygame.display.set_mode((WIDTH, HEIGHT))

item = pygame.Rect(0, 0, 50, 50)

clock = pygame.time.Clock()
running = True
while running:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # - updates - (without draws)

    item.x += speed

    if item.right >= WIDTH:
        speed = -speed
    if item.left <= 0:
        speed = -speed

    # - draws - (without updates)

    window.fill(BLACK)
    pygame.draw.rect(window, RED, item, 2)
    pygame.display.update()

    clock.tick(60)

# - end -
pygame.quit()    

0
投票

我假设您要在移动鼠标时来回移动矩形。您在这里做错了两件事:1.更正此:if rectXpos > 500:,因为当X达到500时必须减少X2.到达rectXpos 501时,应改变方向,直到到达rectXpos 0但您降低了头寸,直到头寸大于500,因此它会停留在499到501之间正确的代码:

import pygame

Red = 255, 0, 0
Black= 0,0,0
rectXpos = 2
rectypos = 2
speed = 2
screenedgex = 500

pygame.init()

window = pygame.display.set_mode(size=(500, 500))

clock = pygame.time.Clock()
running = True
k=1                                #here is k used to indicate direction
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        pygame.display.update()

        window.fill(Black)
        square = pygame.draw.rect(window, Red, [rectXpos, rectypos, 50, 50],2)
        rectXpos +=  2*k                #here is addition of 2 in given direction

        if (rectXpos > 500) or (rectXpos < 0): #here is condition to change direction
            k=-k



        clock.tick(60)
        print(rectXpos)
© www.soinside.com 2019 - 2024. All rights reserved.