Bug using pygame where my sprite won't appear on screen












0














I'm at the very beginning of a chess program and want to print the board on the screen. However, I am having trouble at the first hurdle and cannot even get it to print the squares of the board. It just comes up with a black screen and doesn't put the sprite on it.



I've tried looking at some code from a previous project where it worked, but I can't find any differences in this part of the program.



import pygame
import os
import time

pygame.init()

WHITE = (0, 30, 0)
BLACK = (200, 200, 200)
screen_width = 1400
screen_height = 800
square_size = screen_height/10
screen = pygame.display.set_mode([screen_width, screen_height])
os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


class square(pygame.sprite.Sprite):
def __init__(self, colour, x, y):
super().__init__()
self.image = pygame.Surface([square_size, square_size])
pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
self.colour = colour
self.rect = self.image.get_rect()


squares = pygame.sprite.Group()
s = square(WHITE, 20, 20)
squares.add(s)
squares.draw(screen)
time.sleep(3)


I would expect this to output one white square in the top left hand corner of the screen, but only a black screen appears.










share|improve this question







New contributor




Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    I'm at the very beginning of a chess program and want to print the board on the screen. However, I am having trouble at the first hurdle and cannot even get it to print the squares of the board. It just comes up with a black screen and doesn't put the sprite on it.



    I've tried looking at some code from a previous project where it worked, but I can't find any differences in this part of the program.



    import pygame
    import os
    import time

    pygame.init()

    WHITE = (0, 30, 0)
    BLACK = (200, 200, 200)
    screen_width = 1400
    screen_height = 800
    square_size = screen_height/10
    screen = pygame.display.set_mode([screen_width, screen_height])
    os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


    class square(pygame.sprite.Sprite):
    def __init__(self, colour, x, y):
    super().__init__()
    self.image = pygame.Surface([square_size, square_size])
    pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
    self.colour = colour
    self.rect = self.image.get_rect()


    squares = pygame.sprite.Group()
    s = square(WHITE, 20, 20)
    squares.add(s)
    squares.draw(screen)
    time.sleep(3)


    I would expect this to output one white square in the top left hand corner of the screen, but only a black screen appears.










    share|improve this question







    New contributor




    Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      I'm at the very beginning of a chess program and want to print the board on the screen. However, I am having trouble at the first hurdle and cannot even get it to print the squares of the board. It just comes up with a black screen and doesn't put the sprite on it.



      I've tried looking at some code from a previous project where it worked, but I can't find any differences in this part of the program.



      import pygame
      import os
      import time

      pygame.init()

      WHITE = (0, 30, 0)
      BLACK = (200, 200, 200)
      screen_width = 1400
      screen_height = 800
      square_size = screen_height/10
      screen = pygame.display.set_mode([screen_width, screen_height])
      os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


      class square(pygame.sprite.Sprite):
      def __init__(self, colour, x, y):
      super().__init__()
      self.image = pygame.Surface([square_size, square_size])
      pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
      self.colour = colour
      self.rect = self.image.get_rect()


      squares = pygame.sprite.Group()
      s = square(WHITE, 20, 20)
      squares.add(s)
      squares.draw(screen)
      time.sleep(3)


      I would expect this to output one white square in the top left hand corner of the screen, but only a black screen appears.










      share|improve this question







      New contributor




      Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm at the very beginning of a chess program and want to print the board on the screen. However, I am having trouble at the first hurdle and cannot even get it to print the squares of the board. It just comes up with a black screen and doesn't put the sprite on it.



      I've tried looking at some code from a previous project where it worked, but I can't find any differences in this part of the program.



      import pygame
      import os
      import time

      pygame.init()

      WHITE = (0, 30, 0)
      BLACK = (200, 200, 200)
      screen_width = 1400
      screen_height = 800
      square_size = screen_height/10
      screen = pygame.display.set_mode([screen_width, screen_height])
      os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


      class square(pygame.sprite.Sprite):
      def __init__(self, colour, x, y):
      super().__init__()
      self.image = pygame.Surface([square_size, square_size])
      pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
      self.colour = colour
      self.rect = self.image.get_rect()


      squares = pygame.sprite.Group()
      s = square(WHITE, 20, 20)
      squares.add(s)
      squares.draw(screen)
      time.sleep(3)


      I would expect this to output one white square in the top left hand corner of the screen, but only a black screen appears.







      python pygame






      share|improve this question







      New contributor




      Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Dec 27 '18 at 16:33









      Arkleseisure

      14




      14




      New contributor




      Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Arkleseisure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You need an event.get loop, and to update your display using pygame.display.update() after the squares.draw(screen):



          import pygame
          import os
          import time

          pygame.init()

          WHITE = (0, 30, 0)
          BLACK = (200, 200, 200)
          screen_width = 1400
          screen_height = 800
          square_size = screen_height/10
          screen = pygame.display.set_mode([screen_width, screen_height])
          os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


          class square(pygame.sprite.Sprite):
          def __init__(self, colour, x, y):
          super().__init__()
          self.image = pygame.Surface([square_size, square_size])
          pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
          self.colour = colour
          self.rect = self.image.get_rect()

          for event in pygame.event.get():
          pass

          squares = pygame.sprite.Group()
          s = square(WHITE, 20, 20)
          squares.add(s)
          squares.draw(screen)
          pygame.display.update()
          time.sleep(3)


          Also note, an RGB value of (0, 30, 0) is not white, it's a dark green, if you want bright white try (255, 255, 255).






          share|improve this answer










          New contributor




          Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1




            Thanks, I think I got my white and black the wrong way round.
            – Arkleseisure
            Dec 27 '18 at 17:11






          • 1




            Didn't want completely white or completely black either, so that it would look chessboardy
            – Arkleseisure
            Dec 27 '18 at 20:20










          • Ah yeah that makes sense, if you want I have a pretty easy to use massive color library :)
            – Eli Rockenbeck
            Dec 29 '18 at 0:32






          • 1




            Thanks for the offer, but I'm fine with the one I've got atm :).
            – Arkleseisure
            Dec 29 '18 at 11:46



















          0














          Most likely, your problem is that you didn't flip/update the screen.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            Arkleseisure is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948098%2fbug-using-pygame-where-my-sprite-wont-appear-on-screen%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You need an event.get loop, and to update your display using pygame.display.update() after the squares.draw(screen):



            import pygame
            import os
            import time

            pygame.init()

            WHITE = (0, 30, 0)
            BLACK = (200, 200, 200)
            screen_width = 1400
            screen_height = 800
            square_size = screen_height/10
            screen = pygame.display.set_mode([screen_width, screen_height])
            os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


            class square(pygame.sprite.Sprite):
            def __init__(self, colour, x, y):
            super().__init__()
            self.image = pygame.Surface([square_size, square_size])
            pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
            self.colour = colour
            self.rect = self.image.get_rect()

            for event in pygame.event.get():
            pass

            squares = pygame.sprite.Group()
            s = square(WHITE, 20, 20)
            squares.add(s)
            squares.draw(screen)
            pygame.display.update()
            time.sleep(3)


            Also note, an RGB value of (0, 30, 0) is not white, it's a dark green, if you want bright white try (255, 255, 255).






            share|improve this answer










            New contributor




            Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.














            • 1




              Thanks, I think I got my white and black the wrong way round.
              – Arkleseisure
              Dec 27 '18 at 17:11






            • 1




              Didn't want completely white or completely black either, so that it would look chessboardy
              – Arkleseisure
              Dec 27 '18 at 20:20










            • Ah yeah that makes sense, if you want I have a pretty easy to use massive color library :)
              – Eli Rockenbeck
              Dec 29 '18 at 0:32






            • 1




              Thanks for the offer, but I'm fine with the one I've got atm :).
              – Arkleseisure
              Dec 29 '18 at 11:46
















            0














            You need an event.get loop, and to update your display using pygame.display.update() after the squares.draw(screen):



            import pygame
            import os
            import time

            pygame.init()

            WHITE = (0, 30, 0)
            BLACK = (200, 200, 200)
            screen_width = 1400
            screen_height = 800
            square_size = screen_height/10
            screen = pygame.display.set_mode([screen_width, screen_height])
            os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


            class square(pygame.sprite.Sprite):
            def __init__(self, colour, x, y):
            super().__init__()
            self.image = pygame.Surface([square_size, square_size])
            pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
            self.colour = colour
            self.rect = self.image.get_rect()

            for event in pygame.event.get():
            pass

            squares = pygame.sprite.Group()
            s = square(WHITE, 20, 20)
            squares.add(s)
            squares.draw(screen)
            pygame.display.update()
            time.sleep(3)


            Also note, an RGB value of (0, 30, 0) is not white, it's a dark green, if you want bright white try (255, 255, 255).






            share|improve this answer










            New contributor




            Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.














            • 1




              Thanks, I think I got my white and black the wrong way round.
              – Arkleseisure
              Dec 27 '18 at 17:11






            • 1




              Didn't want completely white or completely black either, so that it would look chessboardy
              – Arkleseisure
              Dec 27 '18 at 20:20










            • Ah yeah that makes sense, if you want I have a pretty easy to use massive color library :)
              – Eli Rockenbeck
              Dec 29 '18 at 0:32






            • 1




              Thanks for the offer, but I'm fine with the one I've got atm :).
              – Arkleseisure
              Dec 29 '18 at 11:46














            0












            0








            0






            You need an event.get loop, and to update your display using pygame.display.update() after the squares.draw(screen):



            import pygame
            import os
            import time

            pygame.init()

            WHITE = (0, 30, 0)
            BLACK = (200, 200, 200)
            screen_width = 1400
            screen_height = 800
            square_size = screen_height/10
            screen = pygame.display.set_mode([screen_width, screen_height])
            os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


            class square(pygame.sprite.Sprite):
            def __init__(self, colour, x, y):
            super().__init__()
            self.image = pygame.Surface([square_size, square_size])
            pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
            self.colour = colour
            self.rect = self.image.get_rect()

            for event in pygame.event.get():
            pass

            squares = pygame.sprite.Group()
            s = square(WHITE, 20, 20)
            squares.add(s)
            squares.draw(screen)
            pygame.display.update()
            time.sleep(3)


            Also note, an RGB value of (0, 30, 0) is not white, it's a dark green, if you want bright white try (255, 255, 255).






            share|improve this answer










            New contributor




            Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            You need an event.get loop, and to update your display using pygame.display.update() after the squares.draw(screen):



            import pygame
            import os
            import time

            pygame.init()

            WHITE = (0, 30, 0)
            BLACK = (200, 200, 200)
            screen_width = 1400
            screen_height = 800
            square_size = screen_height/10
            screen = pygame.display.set_mode([screen_width, screen_height])
            os.environ['SDL_VIDEO_WINDOWS_POS'] = '10,10'


            class square(pygame.sprite.Sprite):
            def __init__(self, colour, x, y):
            super().__init__()
            self.image = pygame.Surface([square_size, square_size])
            pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
            self.colour = colour
            self.rect = self.image.get_rect()

            for event in pygame.event.get():
            pass

            squares = pygame.sprite.Group()
            s = square(WHITE, 20, 20)
            squares.add(s)
            squares.draw(screen)
            pygame.display.update()
            time.sleep(3)


            Also note, an RGB value of (0, 30, 0) is not white, it's a dark green, if you want bright white try (255, 255, 255).







            share|improve this answer










            New contributor




            Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited Dec 27 '18 at 16:57









            Pedro Gaspar

            541321




            541321






            New contributor




            Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered Dec 27 '18 at 16:41









            Eli Rockenbeck

            263




            263




            New contributor




            Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Eli Rockenbeck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.








            • 1




              Thanks, I think I got my white and black the wrong way round.
              – Arkleseisure
              Dec 27 '18 at 17:11






            • 1




              Didn't want completely white or completely black either, so that it would look chessboardy
              – Arkleseisure
              Dec 27 '18 at 20:20










            • Ah yeah that makes sense, if you want I have a pretty easy to use massive color library :)
              – Eli Rockenbeck
              Dec 29 '18 at 0:32






            • 1




              Thanks for the offer, but I'm fine with the one I've got atm :).
              – Arkleseisure
              Dec 29 '18 at 11:46














            • 1




              Thanks, I think I got my white and black the wrong way round.
              – Arkleseisure
              Dec 27 '18 at 17:11






            • 1




              Didn't want completely white or completely black either, so that it would look chessboardy
              – Arkleseisure
              Dec 27 '18 at 20:20










            • Ah yeah that makes sense, if you want I have a pretty easy to use massive color library :)
              – Eli Rockenbeck
              Dec 29 '18 at 0:32






            • 1




              Thanks for the offer, but I'm fine with the one I've got atm :).
              – Arkleseisure
              Dec 29 '18 at 11:46








            1




            1




            Thanks, I think I got my white and black the wrong way round.
            – Arkleseisure
            Dec 27 '18 at 17:11




            Thanks, I think I got my white and black the wrong way round.
            – Arkleseisure
            Dec 27 '18 at 17:11




            1




            1




            Didn't want completely white or completely black either, so that it would look chessboardy
            – Arkleseisure
            Dec 27 '18 at 20:20




            Didn't want completely white or completely black either, so that it would look chessboardy
            – Arkleseisure
            Dec 27 '18 at 20:20












            Ah yeah that makes sense, if you want I have a pretty easy to use massive color library :)
            – Eli Rockenbeck
            Dec 29 '18 at 0:32




            Ah yeah that makes sense, if you want I have a pretty easy to use massive color library :)
            – Eli Rockenbeck
            Dec 29 '18 at 0:32




            1




            1




            Thanks for the offer, but I'm fine with the one I've got atm :).
            – Arkleseisure
            Dec 29 '18 at 11:46




            Thanks for the offer, but I'm fine with the one I've got atm :).
            – Arkleseisure
            Dec 29 '18 at 11:46













            0














            Most likely, your problem is that you didn't flip/update the screen.






            share|improve this answer


























              0














              Most likely, your problem is that you didn't flip/update the screen.






              share|improve this answer
























                0












                0








                0






                Most likely, your problem is that you didn't flip/update the screen.






                share|improve this answer












                Most likely, your problem is that you didn't flip/update the screen.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 27 '18 at 16:39









                Lie Ryan

                44.4k968121




                44.4k968121






















                    Arkleseisure is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Arkleseisure is a new contributor. Be nice, and check out our Code of Conduct.













                    Arkleseisure is a new contributor. Be nice, and check out our Code of Conduct.












                    Arkleseisure is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948098%2fbug-using-pygame-where-my-sprite-wont-appear-on-screen%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Can't read property showImagePicker of undefined in react native iOS

                    Pushsharp Apns notification error: 'InvalidToken'