为什么我用Pygame制作的游戏突然停滞了几秒钟?

时间:2020-02-14 08:05:51

标签: python pygame pygame-surface

我用pygame制作了一个小小的太空侵略者游戏,我注意到游戏会定期滞后几秒钟。我不知道问题是什么。我是初学者。 你能告诉我为什么会这样吗?有解决办法吗?

此外,一旦在主游戏循环中添加背景图像,为什么一切都会变慢?

// Get a hook function
const {useState, useEffect} = React;

const Slider = props => {
const { innerWidth: width, innerHeight: height } = window;
  const urls = [
    "https://www.imb.com.au/templates/client/images/header/IMB-logo.svg",
    "https://www.mkmcapital.com.au/wp-content/uploads/2016/08/MKM_logo-2.png",
    "https://www.pnbank.com.au/Static/Theme/assets/images/pnbank-logo-narrow--7465fe2f1d3e10cf7acfc008991cebef.svg",
    "https://www.provident.com.au/images/logo-blue.png",
    "https://www.adelaidebank.com.au/globalassets/globalresources/brand-logos/abl-logo.png",
    "https://www.amp.com.au/content/dam/amp-au/data/icons/amp-logo-reversed.svg",
    "https://www.anz.com.au//content/dam/anzconz/images/common/promopages/logo-promo-anz-small.png",
    "https://www.bt.com.au//content/dam/public/btfg-bt/images/logo/bt_financial_group_logo_mob-2x1.png",
    "https://www.commbank.com.au/content/dam/commbank/commBank-logo.svg",
    "https://www.collinshomeloans.com.au/hs-fs/hubfs/collins-logo-shedow.png?width=230&name=collins-logo-shedow.png",
    "https://depositpower.com.au/wp-content/uploads/2019/01/DepositPower_stacked_white.png",
    "https://www.latrobefinancial.com.au//Images/Logos/Logo-LTF_v2.jpg",
    "https://d2ttwt9gu7swv4.cloudfront.net/assets/images/logo.svg",
    "https://www.mebank.com.au/MEBank/Assets/Images/me-logo.svg",
    "https://www.nab.com.au//etc/designs/nabrwd/clientlibs/images/logo.png",
    "https://www.pepper.com.au//siteassets/logos/pepper-money-logo.png",
    "https://www.suncorp.com.au//content/dam/suncorp/corporate/images/logos/Suncorp_New_Logo_2x.png",
    "https://www.equifax.com.au/sites/all/themes/vedacorporate/logo.png"
  ];
  let totalWidth = urls.length * 120;
  let [ left, setLeft ] = useState(0);
  let [ secondLeft, setSecondLeft ] = useState(-(width+300));

  useEffect(() => {
    const interval = setInterval(() => {
      setLeft(left => left + 1);
      setSecondLeft(secondLeft => secondLeft + 1);
      //console.log('left ' + left + ' sleft ' + secondLeft);
      if (left == 0) {
        setSecondLeft(-(totalWidth))
      }
      if(secondLeft == 0){
          setLeft(-(totalWidth))
      }
    }, 5);
    return () => {
      clearInterval(interval);
    };
  }, [left, secondLeft]);

  return (
    <div>
      <div
        style={{
          width: totalWidth + 'px',
          whiteSpace: "nowrap",
          position: "relative",
          top: "0px",
          left: left + "px",
          float: 'left',
          display: 'inline-block'
        }}
      >
        {urls.map((u, i) => {
          return (
            <div style={{ display: "inline-block", padding: "10px" }}>
              <img src={u} width="100px" />
            </div>
          );
        })}
      </div>
      <div
        style={{
          width: totalWidth + 'px',
          whiteSpace: "nowrap",
          position: "relative",
          top: "0px",
          left: secondLeft + "px",
          float: 'right',
          display: 'inline-block',
          background: '#fff'
        }}
      >
        {urls.map((u, i) => {
          return (
            <div style={{ display: "inline-block", padding: "10px" }}>
              <img src={u} width="100px" />
            </div>
          );
        })}
      </div>
    </div>
  );
};

// Render it
ReactDOM.render(
  <Slider/>,
  document.getElementById("react")
);

1 个答案:

答案 0 :(得分:1)

在背景图片上调用convert()。这样可以确保图像具有与显示表面相同的像素格式,并有助于blit以最佳性能运行:

screen = pygame.display.set_mode((800, 600))
background = pygame.image.load('background.png').convert()

请注意,如果表面的格式与显示格式不同,则blit必须在每一帧中即时转换格式。