2

Currently, I am working on a game in which you are asked a mathematical question such as 2 + 1 and numbers will fall from the top of the screen. The player has to click the right answer before it falls below the screen. However, I don't want to download images of the numbers because it would waste lots of time. Is there a way to draw or blit numbers instead? If there is, can the numbers be in a circle shape instead of a rectangle?

EDIT: I could create the numbers by displaying them as text. However, that would mean I have to create hundreds of different rectangles for each possible answer. My question then is, is there a way for me to tell Python to generate numbers and place them on a rectangle without me having to do it manually?

Matthew Smith
  • 508
  • 7
  • 22
Abdulrahman
  • 111
  • 9
  • I think it would be a good solution to create the answer objects (numbers) anew for each question. So create a class for the numbers and then fill a list or sprite group with the instances when a new question gets asked, and clear the list afterwards. – skrx May 05 '18 at 06:39
  • BTW, it's usually required to show us what you've tried (a minimal, complete example). We don't want to (and should not) do someone else's homework. – skrx May 05 '18 at 06:48
  • i am quite new in this website. thank you for letting me know. Next time, i will try and put an example. – Abdulrahman May 05 '18 at 08:32

1 Answers1

1

One solution would be to create a class in which you store the actual answer, the text surface and the rect for the collision detection. I use a pygame.sprite.Sprite subclass here, but you can do the same with a normal class. When a new question gets asked, you can just create a bunch of instances and add them to a sprite group, so that you can update them altogether in the main loop. You can use the pygame.Rect.collidepoint method for the collision detection.

import random

import pygame as pg
from pygame.math import Vector2


pg.init()
BLUE = pg.Color('dodgerblue1')
FONT = pg.font.Font(None, 42)


class Answer(pg.sprite.Sprite):

    def __init__(self, pos, number):
        super().__init__()
        # Store the actual number, so that we can compare it
        # when the user clicks on this object.
        self.number = number
        # Render the new text image/surface.
        self.image = FONT.render(str(number), True, BLUE)
        # A rect with the size of the surface, used for collision
        # detection and rendering.
        self.rect = self.image.get_rect(topleft=pos)
        self.vel = Vector2(0, random.uniform(1, 4))
        self.pos = Vector2(pos)

    def update(self):
        self.pos += self.vel
        self.rect.center = self.pos

        if self.rect.top > 480:  # Screen bottom.
            self.kill()  # Remove the sprite from all groups.


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    all_sprites = pg.sprite.Group()
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 3:  # Right mouse button.
                    # Add 20 numbers (Answer objects).
                    for _ in range(20):
                        number = random.randrange(100)
                        all_sprites.add(Answer((random.randrange(620), -20), number))
                elif event.button == 1:  # Left mouse button.
                    # See if the user clicked on a number.
                    for answer in all_sprites:
                        # event.pos is the mouse position.
                        if answer.rect.collidepoint(event.pos):
                            print(answer.number)


        all_sprites.update()

        screen.fill((30, 30, 30))
        all_sprites.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()

(Right click to spawn the numbers, left click to touch single numbers.)

skrx
  • 19,980
  • 5
  • 34
  • 48
  • 1
    Thank you for answering. Sorry, but i don't understand the class module. If you don't mind (and it would save time), where would u suggest learning this topic because i believe it's important – Abdulrahman May 05 '18 at 08:33
  • Program Arcade Games' [chapter 12](http://programarcadegames.com/index.php?lang=de&chapter=introduction_to_classes) and [13](http://programarcadegames.com/index.php?lang=de&chapter=introduction_to_sprites) about classes and pygame sprites are pretty good. – skrx May 05 '18 at 19:52