Sign InNuggetsCoursesCodePadCheatsheet

Turtle & Pygame Cheatsheet

Turtle Cheatsheet

To learn turtle graphics, check out the turtle tutorial. For more information about turtle graphics, check out the documentation.
# turtle creation import turtle # import the turtle module t = turtle.Turtle() # create your turtle and give him or her a name # screen setup (optional) screen = turtle.Screen() # create the screen screen.setup(800, 600) # set up the screen to be 800 x 600 screen.bgcolor("lightblue") # set the color # turtle properties t.clear() # clear the current drawing t.shape("turtle") # can be "classic", "arrow", "circle", or "turtle" t.color("red") # set the color t.pensize(5) # set the pen size for drawing t.speed(10) # set turtle speed, and 0 is the fastest # doodle around t.write('Hey Dude!') # write something t.write('Hi', font=('Arial', 60, 'normal')) # change font and size t.up() # lift up the pen so that no trace is left t.goto(20, 100) # move to position (20, 30) t.down() # put down the pen to leave a trace t.stamp() # stamp the shape t.forward(100) # move forward 100 pixels t.left(90) # turn left 90 degrees t.backward(50) # move backward 50 pixels t.right(90) # turn right 90 degrees # draw circles t.circle(20) # draw a circle with radius 20 t.begin_fill() # begin_fill and end_fill will fill the area t.circle(-30) # draw a circle with radius 30 ("-" makes it clockwise) t.end_fill() t.circle(40, 180) # draw a portion of circle - 180 out of the full circle (360 degrees) t.hideturtle() # hide the turtle shape t.showturtle() # show the turtle shape

Pygame Cheatsheet

To learn pygame, check out the pygame tutorial.
import pygame import sys # define colors in the RGB format WHITE = (255, 255, 255) BLACK = (0, 0, 0) def main(): # set up pygame and its screen pygame.init() screen_size = (1200, 800) screen = pygame.display.set_mode(screen_size) # add window caption pygame.display.set_caption('My Cool Game') # set up font and text; size=25, bold=True, italic=False font = pygame.font.SysFont('Arial', 25, True, False) text = font.render("Hello, Pygame!", True, BLACK) # load sound file # sound = pygame.mixer.Sound('song.wav') # load image file # image = pygame.image.load("picture.png").convert() # create a clock to track time clock = pygame.time.Clock() while True: # handle mouse and keyboard events for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: print('key is pressed') if event.key == pygame.K_RETURN: # play sound # sound.play() print('return is pressed') elif event.type == pygame.KEYUP: print('key is released') elif event.type == pygame.MOUSEBUTTONDOWN: print('mouse button is pressed') elif event.type == pygame.MOUSEBUTTONUP: print('mouse button is released') # fills screen with a background color screen.fill(WHITE) # create your graphics here pygame.draw.line(screen, BLACK, [100, 350], [500, 350], 2) pygame.draw.polygon(screen, BLACK, [[50, 300], [550, 300], [300, 150]], 6) pygame.draw.rect(screen, BLACK, [50, 400, 500, 200], 5) pygame.draw.ellipse(screen, BLACK, [50, 400, 500, 200]) # display text on screen screen.blit(text, [200, 100]) # display image on screen # screen.blit(image, [600, 100]) # to get the cursor position (cursor_x, cursor_y) = pygame.mouse.get_pos() # refresh everyting on screen pygame.display.flip() # limit the update to 60 frames per second clock.tick(60) main()


© CS Wonders·Home·About·Contact·Classes·Gallery·Glossary·Fun Facts