Commits

tatiana_alchueyr committed eca723c

ADD: First version

  • Participants
  • Parent commits 75f07c3

Comments (0)

Files changed (1)

+import pygame
+from pygame.locals import *
+import sys
+import time
+
+# Display mode flag
+FULL = VIDEORESIZE | FULLSCREEN
+
+# Colour
+BLACK = (0,0,0)
+WHITE = (255,255,255)
+
+m = pygame.display.set_mode
+
+if __name__ == '__main__':
+    # Display info
+    size = width, height = 640,480
+    screen = pygame.display.set_mode(size, VIDEORESIZE)
+    icon = pygame.image.load("images/sweeper_original.gif")
+    screen_colour = BLACK
+
+    pygame.display.set_icon(icon)
+    pygame.display.set_caption('Sweeper')
+   
+    # Line
+    lhorizontal = True
+    lthickness = 1
+    lcolour = WHITE
+    rect = pygame.Rect(0,0,width,lthickness)
+    line = pygame.Surface(rect.size)
+    line.fill(lcolour) 
+
+    # General control  
+    quit = False
+    fullscreen = False
+    pause = False
+    loop = False
+    repeat = True
+
+    # Speed
+    lspeed = 1
+    speed = [0, lspeed]
+    sleep_time = 0
+    invert = False
+
+    print "TODO: Loop" 
+    while not quit:
+
+        # EVENTS
+        for event in pygame.event.get():
+            # QUIT
+            if (event.type is QUIT):
+                quit = True
+            # RESIZE
+            if event.type == VIDEORESIZE:
+                size = width, height = event.size
+                screen = m(size, RESIZABLE, 32)
+                if lhorizontal:
+                    rect.size = (width, lthickness)
+                else:
+                    rect.size = (lthickness, height)
+                line = pygame.Surface(rect.size)
+                line.fill(lcolour) 
+            # KEY
+            elif (event.type is KEYDOWN):
+
+                # Quit
+                if event.key == K_ESCAPE:
+                    quit = True 
+                
+                # Toggle fullscreen
+                elif event.key == K_f:
+                    fullscreen = not fullscreen
+                    if fullscreen:
+                        screen = m((width, height), FULL, 32)
+                    else:
+                        screen = m((width, height), VIDEORESIZE, 32) 
+                    print "TODO: Hide awfull cursor"
+
+                # Stop line!
+                elif event.key == K_SPACE:
+                    pause = not pause
+
+                # Start again
+                elif event.key == K_BACKSPACE:
+                    if lhorizontal:
+                        rect.top = 0
+                    else:
+                        rect.left = 0
+
+                # Make line thinner
+                elif event.key == K_DOWN:
+                    if lhorizontal:
+                        if rect.height > 1:
+                            rect.height = rect.height - 1
+                            lthickness = rect.height 
+                    else:
+                        if rect.width > 1:
+                            rect.width = rect.width - 1
+                            lthickness = rect.width
+                    line = pygame.Surface(rect.size)
+                    line.fill(lcolour) 
+
+                # Make line fatter
+                elif event.key == K_UP:
+                    if lhorizontal:
+                        rect.height = rect.height + 1
+                        lthickness = rect.height
+                    else:
+                        rect.width = rect.width + 1
+                        lthickness = rect.width
+                    line = pygame.Surface(rect.size)
+                    line.fill(lcolour) 
+
+                # Speed down
+                elif event.key == K_LEFT:
+                    if lspeed > 1:
+                        lspeed -= 1
+                    else:
+                        sleep_time += 0.02
+
+                    if lhorizontal:
+                        speed = [0, lspeed]
+                    else:
+                        speed = [lspeed, 0]
+ 
+                # Speed up
+                elif event.key == K_RIGHT:
+                    if sleep_time:
+                        sleep_time -= 0.02
+                    else:
+                        lspeed += 1
+                    if lhorizontal:
+                        speed = [0, lspeed]
+                    else:
+                        speed = [lspeed, 0]
+
+                # Horizontal line
+                elif event.key == K_h:
+                    rect.left = 0
+                    rect.top = 0
+                    rect.size = (width, lthickness)
+                    speed = [0, lspeed]
+                    lhorizontal = True
+                    line = pygame.Surface(rect.size)
+                    line.fill(lcolour) 
+
+                # Vertical line
+                elif event.key == K_v:
+                    rect.left = 0
+                    rect.top = 0
+                    rect.size = (lthickness, height)
+                    speed = [lspeed, 0]
+
+                    lhorizontal = False
+                    line = pygame.Surface(rect.size)
+                    line.fill(lcolour) 
+
+                # Black line (and bg to white)
+                elif event.key == K_b:
+                    lcolour = BLACK
+                    line.fill(lcolour)
+                    screen_colour = WHITE
+
+                # White line (and bg to black)
+                elif event.key == K_w:
+                    lcolour = WHITE
+                    line.fill(lcolour)
+                    screen_colour = BLACK
+
+                # Invert line movement direction
+                elif event.key == K_i:
+                    invert = not invert
+
+                # Loop
+                elif event.key == K_l:
+                    loop = not loop
+                    if loop:
+                        repeat = False
+
+                # Repeat
+                elif event.key == K_r:
+                    repeat = not repeat 
+                    if repeat:
+                        loop = False
+                    
+
+        # RUN
+        if not pause:
+            # For some reason, sleep_time was being set to
+            # 2.77555756156e-17, and then to -0.1, which is an
+            # invalid value... Therefore, we need to check this
+            try:
+                time.sleep(sleep_time)
+            except IOError:
+                sleep_time = 0
+
+            # Repeat
+            if repeat:
+                if rect.left < 0:
+                    rect.right = width
+                elif rect.right > width:
+                    rect.left = 0
+                if rect.top < 0:
+                   rect.bottom = height 
+                elif rect.bottom > height:
+                    rect.top = 0
+
+            # Loop 
+            if loop:
+                if rect.left < 0 or rect.right > width:
+                    invert = not invert
+                if rect.top < 0 or rect.bottom > height:
+                    invert = not invert
+
+            # Move the line
+            if invert:
+                _speed = [-value for value in speed]
+            else:
+                _speed = speed
+            rect = rect.move(_speed)
+                       
+        screen.fill(screen_colour)
+        screen.blit(line, rect)
+
+        pygame.display.flip()
+
+    sys.exit()
+
+