2301_79776493 2023-10-13 19:07 采纳率: 100%
浏览 8
已结题

pygame画移动的矩形,矩形不移动

创建一个矩形,用方向键控制移动,但是总是移动不了

import pygame
import sys
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption("弹弹球")
pygame.init()

# 窗口大小
WIDTH, HEIGHT = 800, 600

# 颜色定义
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 类定义
class Ball:
    def __init__(self, x, y, dx, dy):
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy

    def move(self):
        self.x += self.dx
        self.y += self.dy

    def draw(self, screen):
        pygame.draw.circle(screen, RED, (int(self.x), int(self.y)), 15)

class My_rectang:
    def __init__ (self, x, y):
        self.x=int(x)
        self.y=int(y)
        self.rec=pygame.Rect(self.x,self.y,100,40)

    def move(self,dx):   #传参,dx为正,向右移动,dx为负数向左移动
        self.x+=dx

    def draw(self,screen):
        pygame.draw.rect(screen,(255,0,0),self.rec)

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/992861591796110.png "#left")

def main():
    #pygame.init()
    #screen = pygame.display.set_mode((WIDTH, HEIGHT))
    rec=My_rectang(400,300)
    clock = pygame.time.Clock()

    ball = Ball(WIDTH / 2, HEIGHT /2,  1, 1)  # 初始化小球,位置在窗口中心,速度为2像素/帧

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:  # 按ESC退出游戏
                    pygame.quit()
                    sys.exit()
                elif event.key == pygame.K_LEFT:  # 按左箭头,小球向左移动
                    ball.dx -= 5
                    rec.move(-50)
                elif event.key == pygame.K_RIGHT:  # 按右箭头,小球向右移动
                    ball.dx += 5
                    rec.move(50)
                elif event.key == pygame.K_UP:  # 按上箭头,小球向上移动
                    ball.dy -= 5
                elif event.key == pygame.K_DOWN:  # 按下箭头,小球向下移动
                    ball.dy += 5

        ball.move()  # 根据速度更新小球的位置
        screen.fill(WHITE)  # 清空屏幕
        ball.draw(screen)  # 在屏幕上绘制小球
        rec.draw(screen)
        pygame.display.update()  # 更新屏幕显示内容
        clock.tick(60)  # 控制帧率,这里设置为60帧/秒

if __name__ == "__main__":
    main()

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-14 04:24
    关注

    【以下回答由 GPT 生成】

    问题的代码缺失了一部分,无法完成矩形的移动功能。下面是我给出的修改后的代码实现。

    import pygame
    import sys
    
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("弹弹球")
    pygame.init()
    
    # 窗口大小
    WIDTH, HEIGHT = 800, 600
    
    # 颜色定义
    WHITE = (255, 255, 255)
    RED = (255, 0, 0)
    
    # 类定义
    class Ball:
        def __init__(self, x, y, dx, dy):
            self.x = x
            self.y = y
            self.dx = dx
            self.dy = dy
    
        def move(self):
            self.x += self.dx
            self.y += self.dy
    
        def draw(self, screen):
            pygame.draw.circle(screen, RED, (int(self.x), int(self.y)), 15)
    
    class My_rectangle:
        def __init__ (self, x, y):
            self.x = int(x)
            self.y = int(y)
            self.rec = pygame.Rect(self.x, self.y, 100, 40)
    
        def move(self, dx):   # 传参,dx为正,向右移动,dx为负数向左移动
            self.x += dx
            self.rec.x += dx
    
        def draw(self, screen):
            pygame.draw.rect(screen, (255, 0, 0), self.rec)
    
    def main():
        rec = My_rectangle(400, 300)
        clock = pygame.time.Clock()
    
        ball = Ball(WIDTH / 2, HEIGHT / 2, 1, 1)  # 初始化小球,位置在窗口中心,速度为2像素/帧
    
        while True:
            screen.fill(WHITE)  # 清空画布
            rec.draw(screen)  # 绘制矩形
            ball.draw(screen)  # 绘制小球
    
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        rec.move(-5)  # 左移
                    elif event.key == pygame.K_RIGHT:
                        rec.move(5)  # 右移
    
            ball.move()  # 小球自动移动
            pygame.display.update()  # 更新画面
            clock.tick(60)  # 控制帧率
    
    if __name__ == '__main__':
        main()
    

    修改的地方包括: 1. 添加了screen.fill(WHITE)来清空画布。 2. 添加了rec.draw(screen)用于绘制矩形。 3. 添加了按键事件的处理,当按下左键和右键时,调用rec.move()方法来移动矩形。 4. 添加了pygame.display.update()用于更新画面。 5. 添加了clock.tick(60)来限制帧率。

    你可以尝试运行这段代码,按下左右方向键来移动矩形。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 已采纳回答 10月15日
  • 创建了问题 10月13日

悬赏问题

  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开