Crazy1969 2019-11-01 10:55 采纳率: 0%
浏览 453

Python入门到实践习题12-3移动火箭问题

根据习题描述,自己写了个移动火箭代码,程序运行无问题,但是不能移动火箭,习题各位大神能指点下代码哪里有问题。代码如下:

import pygame
import sys

def screen_main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption('移动火箭')
    bg_color = (193, 215, 192)

    # 加载图片
    image = pygame.image.load(r'images\timg.jpg')   

    # 获取图片的矩形区域
    img_rect = image.get_rect()     

    # 获取窗口的矩形区域
    screen_rect = screen.get_rect()         

    # 将窗口的矩形x坐标赋值给图片的矩形x坐标
    img_rect.centerx = screen_rect.centerx  

    # 将窗口的矩形y坐标赋值给图片的矩形y坐标
    img_rect.centery = screen_rect.centery  

    # 定义移动标志
    moving_down = False
    moving_up = False
    moving_left = False
    moving_right = False

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.type == pygame.K_DOWN:
                    moving_down = True
                elif event.type == pygame.K_UP:
                    moving_up = True
                elif event.type == pygame.K_LEFT:
                    moving_left = True
                elif event.type == pygame.K_RIGHT:
                    moving_right = True
            elif event.type == pygame.KEYUP:
                if event.type == pygame.K_DOWN:
                    moving_down = False
                elif event.type == pygame.K_UP:
                    moving_up = False
                elif event.type == pygame.K_LEFT:
                    moving_left = False
                elif event.type == pygame.K_RIGHT:
                    moving_right = False

        if moving_left:
            img_rect.centerx -= 1
        if moving_right:
            img_rect.centerx += 1
        if moving_down:
            img_rect.centery+= 1
        if moving_up:
            img_rect.centery -= 1

        screen.fill(bg_color)

        # 在屏幕上绘制image图片,第二个参数为目标参数
        screen.blit(image, img_rect)  

        pygame.display.flip()

screen_main()












  • 写回答

1条回答 默认 最新

  • chenxizhan1995 2019-11-01 17:17
    关注

    判断按键的代码有点问题,把 if event.type == pygame.K_DOWN: 修改成

    keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_DOWN]:
    

    就可以了。
    这是修改后的完整代码

    import pygame
    import sys
    
    def screen_main():
        pygame.init()
        screen = pygame.display.set_mode((800, 600))
        pygame.display.set_caption('移动火箭')
        bg_color = (193, 215, 192)
    
        # 加载图片
        image = pygame.image.load(r'images\timg.jpg')
    
        # 获取图片的矩形区域
        img_rect = image.get_rect()
    
        # 获取窗口的矩形区域
        screen_rect = screen.get_rect()
    
        # 将窗口的矩形x坐标赋值给图片的矩形x坐标
        img_rect.centerx = screen_rect.centerx
    
        # 将窗口的矩形y坐标赋值给图片的矩形y坐标
        img_rect.centery = screen_rect.centery
    
        # 定义移动标志
        moving_down = False
        moving_up = False
        moving_left = False
        moving_right = False
        tnc=0
        while True:
            tnc+=1
            cnt=0
    
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    keys_pressed = pygame.key.get_pressed()
                                    # 修改按键判断语句
                    # if event.type == pygame.K_DOWN:
                    if keys_pressed[pygame.K_DOWN]:
                        moving_down = True
                    elif keys_pressed[pygame.K_UP]:
                        moving_up = True
                    elif keys_pressed[pygame.K_LEFT]:
                        moving_left = True
                    elif keys_pressed[pygame.K_RIGHT]:
                        moving_right = True
                elif event.type == pygame.KEYUP:
                    # if event.type == pygame.K_DOWN:
                    if keys_pressed[pygame.K_DOWN]:
                        moving_down = False
                    elif keys_pressed[pygame.K_UP]:
                        moving_up = False
                    elif keys_pressed[pygame.K_LEFT]:
                        moving_left = False
                    elif keys_pressed[pygame.K_RIGHT]:
                        moving_right = False
                print("for ", cnt)
                cnt+=1
            if moving_left:
                img_rect.centerx -= 1
            if moving_right:
                img_rect.centerx += 1
            if moving_down:
                img_rect.centery+= 1
            if moving_up:
                img_rect.centery -= 1
    
            screen.fill(bg_color)
    
            # 在屏幕上绘制image图片,第二个参数为目标参数
            screen.blit(image, img_rect)
    
            pygame.display.flip()
    
    screen_main()
    
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?