Slalom in D-MOL

(The title doesn't work well in English, but would in German and most other languages. :)

Our last homework will finally involve some "actual" cycling.

Technical Stuff

Sound effects and images: see the file at the end of Slovenian text (or below the link to this page). You do not need to submit these files or module risar; we already have them. :) HOWEVER: the files with sounds and images must be in the same folder as your program! Draw images with, for instances, risar.slika(x, y, "flower.png") and not risar.slika(x, y, "c:/Users/benjamin/faks/programiranje1/dn12/slike/flower.png", or we will hate you.)

Tests: no tests this time.

Drawing obstacles: add the following function to risar.

@pogojno_obnovi
def pravokotnik(x0, y0, x1, y1, barva=bela, sirina=1, poln=False, zaobljen=0):
    path = QPainterPath()
    if zaobljen:
        path.addRoundedRect(QRectF(0, 0, x1 - x0, y1 - y0), zaobljen, zaobljen)
    else:
        path.addRect(QRectF(0, 0, x1 - x0, y1 - y0))
    rect = QGraphicsPathItem(path)
    rect.setPen(QPen(barva, sirina))
    rect.setBrush(QColor(barva).darker(150))
    rect.setPos(x0, y0)
    widget.scene.addItem(rect)
    return rect

It draws a rectangle with top left corner at (x0, y0) and bottom down at (x1, y1). If you add poln=True, it will be filled (with a slightly darker color). If you add zaobljen=5 (it can also be another number), corners will be rounded.

Changing texts: if you draw some text with b = risar.besedilo(...), you can later change it by calling b.setPlainText(some_new_text).

Use of modules other than risar/Qt: the proper library for this would be PyGame; Qt is meant for other things. However, don't use other modules because (a) using it might make the task trivial and you wouldn't learn/show anything and (b) checking your work could require install other modules that would or would not work on our computers, and this would be a nightmare.

Grade 6

Implement obstacles.

  • They appear at the top. Their height is 25 and the widths are between 30 and 80. Horizontally, the obstacle may appear at the left or right border, but not beyond. Hint: first choose the width, and them the position.
  • To draw the obstacle, use the above function pravokotnik.
  • Obstacles move downwards by 1 (pixel) at a time. Do this by moving the existing obstacles down, not be drawing new obstacles. (See how we moved the turtle.)
  • Moving the obstacles will run in an endless loop. In that loop first move all obstacles downwards and then call risar.cakaj(0.002). Do not call this after moving every individual obstacle!
  • When an obstacle is no longer visible, call risar.odstrani(obstacle), where obstacle is an object that represents the obstacle (that is, the result of function pravokotnik). Besides, you will probably maintain a list of obstacles and remove your obstacle from there, but that part is your internal business.
  • Obstacles appear in the following fashion: at each step (e.g. before the loop that moves all obstacles downwards), check whether the number of visible obstacles is less than 20. If so, there is a 2 % chance (e.g. if random.random() < 0.02) that a new obstacle will appear at that iteration of the loop. (This also means there will never be more than 20 visible obstacles.)

Grade 7

  • Add a cyclist somewhere 40 points above the lower edge of the window. He can be moved left and right using "left" and "right" buttons. Functions risar.levo() and risar.desno() return True when the corresponding button is pressed.
  • Do not allow the cyclist to move beyond the left or right edge.
  • When a cyclist crashes into an obstacle, the game ends. Be a bit tolerant when checking for the crash (the cyclist is not a rectangle).

Grade 8

  • Change the game so the cyclist loses only one at each crash. The Department for economy and motorized traffic at City of Ljubljana (MOL) is convinced that cyclists have nine lives. This is of course nonsense, we have only three. The game should therefore end when the cyclist crashes the third time.
  • The number of remaining lives must be indicated at the top right corner of the screen.
  • After crashing, the game continues from the current point.

(The game in the video does not implement this but ends at the first crash.)

Grade 9

  • Add objects that bring points (see the video). The total number of points so far must be indicated at the top left corner, as in the video.
  • Awards are as in previous homeworks, that is:
nagrade = [("flowers.png", 1), ("bottle.png", 1), ("stones.png", 2), ("grass.png", 3), ("walker.png", 4),
           ("scooter.png", 2)]

Do not add other objects, like staircases or highways, because you are submitting only code, not pictures.

Grade 10

Discover how to add sound and add it. Use only what is provided by Qt, no other libraries. Also, only use the files provided with the homework.

  • One sound is playing continuously; set the loop.
  • The other plays when collecting awards.
  • The third plays when the cyclist hits an obstacle. If this is the third hit and the game ends, the (annoying) continuous sound ceases just before this, third sound plays.