为什么函数代码这样优化就不行?

为什么函数代码这样优化就不行?

相关截图:

war.py

http://img1.sycdn.imooc.com//climg/6057f5b709dc763527441638.jpg

http://img1.sycdn.imooc.com//climg/6057f5b709049b7422801370.jpg


mian.py

http://img1.sycdn.imooc.com//climg/6057f61c09606b8d15320618.jpg

问题描述:

函数代码这样优化后始终无法运行,不知道为什么

相关代码:

war.py

import sys, pygame

import constants
from game.plane import Ourplane, Enemyplane

class Planewar(object):
    READY=0
    PLAYING=1
    OVER=2

    status=READY
    small_enemies = pygame.sprite.Group()
    enemies = pygame.sprite.Group()
    counter = 0

    def __init__(self):
        pygame.init()

        self.width, self.height = 480, 852
    
        self.screen = pygame.display.set_mode((self.width, self.height))

        self.bg = pygame.image.load(constants.bg_img)
        self.img_game_title = pygame.image.load(constants.game_title_img)
        self.img_game_title_rect = self.img_game_title.get_rect()
        t_width, t_height = self.img_game_title.get_size()
        self.img_game_title_rect.topleft = (int((self.width - t_width))/ 2, int(self.height / 2-t_height))
        self.btn_start = pygame.image.load(constants.game_start_img)
        self.btn_start_rect = self.btn_start.get_rect()
        btn_width, btn_height = self.btn_start.get_size()
        self.btn_start_rect.topleft =(int(self.width - btn_width) / 2), int(self.height / 2+btn_height)
        pygame.mixer.music.load(constants.bg_music)
        pygame.mixer.music.play(-1)
        pygame.mixer.music.set_volume(0.5)
        self.ourplane = Ourplane(self.screen, speed = 10)
        self.clock = pygame.time.Clock()
        for i in range(0, 6):
            self.plane = Enemyplane(self.screen, speed=5)
            self.plane.add(self.small_enemies, self.enemies)

    def loop(self):
        while True:
            self.counter += 1
            self.clock.tick(60)
            if self.counter >= 60:
                self.counter = 0

    def bind_event(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.status == self.READY:
                    self.status = self.PLAYING
            elif event.type == pygame.KEYDOWN:
                if self.status == self.PLAYING:
                    if event.key == pygame.K_w or event.key == pygame.K_UP:
                        self.ourplane.move_up()
                    elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                        self.ourplane.move_left()
                    elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                        self.ourplane.move_right()
                    elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                        self.ourplane.move_down()
                    elif event.key == pygame.K_SPACE:
                        self.ourplane.shoot()

    def run_game(self):
        self.loop()
        self.bind_event()
        if self.status == self.READY:
            self.screen.blit(self.bg, self.bg.get_rect())
            self.screen.blit(self.img_game_title, self.img_game_title_rect)
            self.screen.blit(self.btn_start, self.btn_start_rect)

        elif self.status == 1:
            self.screen.blit(self.bg, self.bg.get_rect())
            self.ourplane.update(self.counter)
            self.ourplane.bullets.update()
            self.small_enemies.update()

        pygame.display.set_caption('飞机大战游戏')
        pygame.display.flip()
 

相关代码:

mian.py

​from game.war import Planewar


def main():
War=Planewar()
War.loop()
War.bind_event()
War.run_game()



if __name__ == "__main__":
main()


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学,你好!同学在调用loop()方法时,循环会一直执行导致死循环,不会继续向下执行其他代码,因此会无法执行。同学可以根据视频讲解的方法进行修改

http://img1.sycdn.imooc.com//climg/60581243091a854c04380188.jpg

祝学习愉快!

  • weixin_慕妹5398817 提问者 #1

    所以这里的while True循环里面不能添加函数方法?把mian.py的War.loop()、War.bind_event()去掉,但是war.py里还保留loop()方法,最后还是运行失败

    http://img1.sycdn.imooc.com//climg/605831f309dccaf416621342.jpg

    http://img1.sycdn.imooc.com//climg/6058322c09824a1908700662.jpg

    2021-03-22 13:59:23
  • 同学,你好!是loop()函数中使用的while循环导致的死循环,在run_game()函数中还调用了该方法,因此还是不行

    http://img1.sycdn.imooc.com//climg/60583b6009e6079e03280171.jpg

    是run_game()中的整体代码循环执行,并不是设置帧速率相关代码循环执行,同学可根据下述方法进行修改:

    http://img1.sycdn.imooc.com//climg/60583bed09baa26b07040562.jpg

    祝学习愉快!

    2021-03-22 14:43:02
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
1.Python零基础入门
  • 参与学习           人
  • 提交作业       2727    份
  • 解答问题       8160    个

想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师