cmu-cs-academy / desktop-cmu-graphics

BSD 3-Clause "New" or "Revised" License
15 stars 9 forks source link

Samples for the other mode of cmu_graphics #73

Open Mohamed-Waiel-Shikfa opened 3 months ago

Mohamed-Waiel-Shikfa commented 3 months ago

From my understanding there are multiple valid ways of running cmu_graphics; one that follows an MVC model and one that doesn't. I think there might be more nuances, but I am not sure and would appreciate clarification (particularly on runAppWithScreens and what it does).

Following those clarification, my suggestion is to add a section to README.md to make users aware of those nuances so that they be able to use whichever of the modes they like. I could take care of that. Together with this, it would be nice to provide some samples that uses each of these modes, and I could also take care of this (currently there are only samples with cmu_graphics.run() mode).

dAIsySHEng1 commented 2 months ago

Hi @Mohamed-Waiel-Shikfa - thanks for these ideas and suggestions :).

The CPCS team is actively working on expanding documentation for cmu_graphics, including for Desktop CMU Graphics, to go up on the CS Academy site. So, if there are any topics that you would like to see added, please let us know!

The README.md serves as a high level overview, so I think that the best place to discuss the nuances is in the documentation that we're working on revising and expanding.

If there are any topics that you are especially interested in providing samples for, please feel free to share. I'll check to see if those are ones we haven't worked on or are looking for further examples for, and we'd be happy to have you contribute :).

dAIsySHEng1 commented 2 months ago

Regarding runAppWithScreens(), here's a quick example:

# Screen demo mdtaylor, edited agreenaw

from cmu_graphics import *
import random

def onAppStart(app):
    print('We recommend only one onAppStart!')
    app.x = 100
    app.y = 200
    app.dx = random.randint(-10, 10)
    app.dy = random.randint(-10, 10)
    app.r = 15

#---------------------------------------------------

def welcome_redrawAll(app):
    drawLabel("Welcome to The Game", app.width/2, app.height/2, size = 24)
    drawLabel("Press Space to start", app.width/2, app.height/2+20)

def welcome_onKeyPress(app, key):
    if key == 'space':
        setActiveScreen('game')

#---------------------------------------------------

def game_onStep(app):
    app.x += app.dx
    app.y += app.dy
    app.x = app.x%app.width
    app.y = app.y%app.height

def game_redrawAll(app):
    drawCircle(app.x, app.y, app.r, fill = 'purple')
    drawLabel("Press p to Pause, e to Exit", app.width/2, 10)

def game_onKeyPress(app, key):
    if key == 'e':
        setActiveScreen('welcome')
    elif key == 'p':
        setActiveScreen('pause')
    else:
        app.dx = random.randint(-10, 10)
        app.dy = random.randint(-10, 10)

#---------------------------------------------------

def pause_onKeyPress(app, key):
    if key=='p':
        setActiveScreen('game')

def pause_redrawAll(app):
    drawCircle(app.x, app.y, app.r, fill = 'purple')
    drawLabel('PAUSED!', app.width/2, app.height/2)
    drawLabel('Press p to Play', app.width/2, 10)

#---------------------------------------------------

# Your screen names should be strings
def main():
    app = runAppWithScreens(initialScreen='welcome')

main()

This in essence allows you to switch between multiple different customizable screens in your app. And this can be a very useful feature when building out more complex apps. Some further examples of screens in a game app could be a mode-selection screen or a help manual screen.

Hope this helps in giving you some further clarity about runAppWithScreens(). That's something we're going to try to add in the documentation for the coming Fall.

Best, Daisy (CPCS Team Lead)