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

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题