【问题标题】:How do i move the square up and down in pygame?我如何在pygame中上下移动正方形?
【发布时间】:2023-01-10 21:55:23
【问题描述】:

我需要让正方形上下移动。从我的以下代码中,方块左右移动,但我需要确保它也上下移动。

import pygame, sys
from pygame.locals import QUIT


def draw_rectangle(surface, rectangle):
    pygame.draw.rect(surface, (0, 255, 0), rectangle)


def main():
    pygame.init()

    #Intalise
    DISPLAYSURF = pygame.display.set_mode((400, 300))
    pygame.display.set_caption('Rectangles and Squares')

    game_clock = pygame.time.Clock()

    #Variables
    x_position = 100

    #Game Loop
    running = True
    while running:
        #Update Section

        #Update delta_time
        delta_time = game_clock.tick(60)/100

        #Handle events
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        #Process keyboard inputs
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            x_position -= 10 * delta_time
        if keys[pygame.K_RIGHT]:
            x_position += 10 * delta_time

        #Draw Section
        DISPLAYSURF.fill((128, 128, 128))

        draw_rectangle(DISPLAYSURF, pygame.Rect(x_position, 100, 50, 50))

        pygame.display.flip()

if __name__ == "__main__":
    main()

【问题讨论】:

  • 提示:代码中写着pygame.Rect(x_position, 100, 50, 50),你认为这些数字是什么意思?特别是,100 是什么意思?请注意此代码如何使用 x_position 变量,并且代码的另一部分变化这个变量?因为那是允许左右移动的原因……你认为什么可能允许上下移动?
  • 你必须改变 y 坐标而不是 x 坐标

标签: python pygame


【解决方案1】:

一段很好的代码,付出了很大的努力。 cmets 中有一些针对原始问题的提示,但由于有多个更改,我已经添加了它们。

以下是所做的 3 项基本修改:

  1. 引入初始 y_position。
  2. 启用上/下移动(记住屏幕顶部的 y=0)。
  3. 将 y_position 添加到 draw_rectangle 函数中。

    所以这段代码有效:

    import pygame, sys
    from pygame.locals import QUIT
    
    
    def draw_rectangle(surface, rectangle):
        pygame.draw.rect(surface, (0, 255, 0), rectangle)
    
    
    def main():
        pygame.init()
    
        #Intalise
        DISPLAYSURF = pygame.display.set_mode((400, 300))
        pygame.display.set_caption('Rectangles and Squares')
    
        game_clock = pygame.time.Clock()
    
        #Variables
        x_position = 100
        y_position = 100    # <---  this
    
        #Game Loop
        running = True
        while running:
            #Update Section
    
            #Update delta_time
            delta_time = game_clock.tick(60)/100
    
            #Handle events
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
    
            #Process keyboard inputs
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                x_position -= 10 * delta_time
            if keys[pygame.K_RIGHT]:
                x_position += 10 * delta_time
    
            # handles the y positions (note y is from top to bottom)    
            if keys[pygame.K_UP]:
                y_position -= 10 * delta_time
            if keys[pygame.K_DOWN]:
                y_position += 10 * delta_time
            #Draw Section
            DISPLAYSURF.fill((128, 128, 128))
    
            draw_rectangle(DISPLAYSURF, pygame.Rect(x_position, y_position, 50, 50))    # <-- this
    
            pygame.display.flip()
    
    if __name__ == "__main__":
        main()
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    相关资源
    最近更新 更多