简体   繁体   中英

Adding a Sprite to screen in pygame

I'm a beginner and I'm been set a challenge to make a 'Simon says' game. However, I can't get 'yellow_tile' to appear on the screen in the code below. What am I missing. I know it must be something very elementary, but I'm stuck.

Thanks.

Code(under comment 'create tiles') :

# Simon Says
# displays sequences of sounds and/or colors which the user has to repeat

from livewires import games, color
import random

# initialise screen
games.init(screen_width = 640, screen_height = 480, fps = 50)

class Game(object):
    """A sequence repeat game"""
    sequence_count = 1
    score = 0

    def __init__(self):
        """Initialize Game object"""

        # create key
        self.menu_title = games.Text(value = "Key",
                                     size = 25,
                                     color  = color.white,
                                     top = 5,
                                     left = games.screen.width - 100,
                                     is_collideable = False)
        self.menu_choice0 = games.Text(value = "0 - Quit",
                          size = 20,
                          color  = color.white,
                          top = self.menu_title.bottom + 5,
                          left = games.screen.width - 120,
                          is_collideable = False)
        self.menu_choice1 = games.Text(value = "1 - Yellow",
                          size = 20,
                          color  = color.yellow,
                          top = self.menu_choice0.bottom + 5,
                          left = games.screen.width - 120,
                          is_collideable = False)
        self.menu_choice2 = games.Text(value = "2 - Red",
                          size = 20,
                          color  = color.red,
                          top = self.menu_choice1.bottom + 5,
                          left = games.screen.width - 120,
                          is_collideable = False)
        self.menu_choice3 = games.Text(value = "2 - Blue",
                          size = 20,
                          color  = color.blue,
                          top = self.menu_choice2.bottom + 5,
                          left = games.screen.width - 120,
                          is_collideable = False)
        self.menu_choice4 = games.Text(value = "4 - Green",
                                       size = 20,
                                       color = color.green,
                                       top = self.menu_choice3.bottom + 5,
                                       left = games.screen.width - 120,
                                       is_collideable = False)

        games.screen.add(self.menu_title)
        games.screen.add(self.menu_choice0)
        games.screen.add(self.menu_choice1)
        games.screen.add(self.menu_choice2)
        games.screen.add(self.menu_choice3)
        games.screen.add(self.menu_choice4)

        # add scrolling text at bottom of screen
        self.instructions = games.Text(value = "Repeat the sequence by entering the "
                                               "corresponding number.",
                                               size = 20,
                                               color = color.white,
                                               x = games.screen.width/2,
                                               bottom = games.screen.height - 2)

        games.screen.add(self.instructions)

        # create score
        self.score = games.Text(value = 0,
                                size = 30,
                                color = color.white,
                                top = 5,
                                left = 10,
                                is_collideable = False)
        games.screen.add(self.score)


        # create tiles
        self.yellow_tile = Yellow(game = self, x = games.screen.width/4,
                                  y = games.screen.height/4)
        games.screen.add(self.yellow_tile)

    def play(self):
        """Play game"""
        # load and set background
        black_background = games.load_image("blackbackground.jpg", transparent = False)
        games.screen.background = black_background
        games.screen.mainloop()


class Yellow(games.Sprite):
    """The yellow square"""
    sound = games.load_sound("thrust.wav")
    image = games.load_image("yellow_sq.bmp")
    def __init__(self, game, x, y):
        """Initialize yellow sprite"""
        super(Yellow, self).__init__(image = Yellow.image, x = x, y = y)
        self.game = game




def main():
    sequence = Game()
    sequence.play()

main()

The issue was not that the Sprite was not being added to the screen, but that the image used was not appearing (for reasons I have yet to investigate).

Other images, known to be good, appear as they should when entered into the same code.

This is a painful lesson! Check everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM