├── README.md ├── basics ├── cat.png ├── catamination.ipynb ├── catanimation.py ├── HelloWorld.py ├── HelloWorld.ipynb ├── catanimation.ipynb └── drawing.ipynb ├── HelloWorld.py ├── Memory-Puzzle ├── .ipynb_checkpoints │ └── memoryPuzzle-checkpoint.ipynb ├── memoryPuzzle.ipynb └── memoryPuzzle.py ├── HelloWorld.ipynb └── drawing.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # LearnPyGame -------------------------------------------------------------------------------- /basics/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andersy005/LearnPyGame/master/basics/cat.png -------------------------------------------------------------------------------- /basics/catamination.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 0 6 | } 7 | -------------------------------------------------------------------------------- /HelloWorld.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | import pygame 8 | import sys 9 | from pygame.locals import * 10 | 11 | # In[2]: 12 | 13 | pygame.init() 14 | DISPLAYSURF = pygame.display.set_mode((400, 300)) 15 | pygame.display.set_caption('Hello World!') 16 | while True: 17 | for event in pygame.event.get(): 18 | if event.type == QUIT: 19 | pygame.quit() 20 | sys.exit() 21 | pygame.display.update() 22 | 23 | 24 | # In[ ]: 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Memory-Puzzle/.ipynb_checkpoints/memoryPuzzle-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [] 11 | } 12 | ], 13 | "metadata": { 14 | "kernelspec": { 15 | "display_name": "Python [Root]", 16 | "language": "python", 17 | "name": "Python [Root]" 18 | }, 19 | "language_info": { 20 | "codemirror_mode": { 21 | "name": "ipython", 22 | "version": 2 23 | }, 24 | "file_extension": ".py", 25 | "mimetype": "text/x-python", 26 | "name": "python", 27 | "nbconvert_exporter": "python", 28 | "pygments_lexer": "ipython2", 29 | "version": "2.7.12" 30 | } 31 | }, 32 | "nbformat": 4, 33 | "nbformat_minor": 0 34 | } 35 | -------------------------------------------------------------------------------- /basics/catanimation.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | import pygame 7 | import sys 8 | from pygame.locals import * 9 | 10 | 11 | # In[2]: 12 | 13 | pygame.init() 14 | 15 | 16 | # In[3]: 17 | 18 | FPS = 30 # frames per second setting 19 | fpsClock = pygame.time.Clock() 20 | 21 | 22 | # In[4]: 23 | 24 | # set up the window 25 | DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32) 26 | pygame.display.set_caption('Animation') 27 | 28 | 29 | # In[5]: 30 | 31 | WHITE = (255, 255, 255) 32 | catImg = pygame.image.load('Cat.png') 33 | catx = 10 34 | caty = 10 35 | direction = 'right' 36 | 37 | 38 | # In[6]: 39 | 40 | while True: 41 | DISPLAYSURF.fill(WHITE) 42 | 43 | if direction == 'right': 44 | catx += 5 45 | if catx == 280: 46 | direction = 'down' 47 | 48 | elif direction == 'down': 49 | caty += 5 50 | if caty == 220: 51 | direction = 'left' 52 | 53 | elif direction == 'left': 54 | catx -= 5 55 | if catx == 10: 56 | direction = 'up' 57 | 58 | elif direction == 'up': 59 | caty -= 5 60 | if caty == 10: 61 | direction = 'right' 62 | 63 | DISPLAYSURF.blit(catImg, (catx, caty)) 64 | for event in pygame.event.get(): 65 | if event.type == QUIT: 66 | pygame.quit() 67 | sys.exit() 68 | pygame.display.update() 69 | fpsClock.tick(FPS) 70 | 71 | -------------------------------------------------------------------------------- /basics/HelloWorld.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | import pygame, sys 7 | from pygame.locals import * 8 | 9 | 10 | # In[2]: 11 | 12 | pygame.init() 13 | DISPLAYSURF = pygame.display.set_mode((400, 300)) 14 | pygame.display.set_caption('Hello World!') 15 | 16 | WHITE = (255, 255, 255) 17 | GREEN = (0, 255, 0) 18 | BLUE = (0, 0, 255) 19 | 20 | fontObj = pygame.font.Font('freesansbold.ttf', 32) 21 | textSurfaceObj = fontObj.render('Hello World!', True, GREEN, BLUE) 22 | textRectObj = textSurfaceObj.get_rect() 23 | textRectObj.center = (200, 150) 24 | 25 | while True: 26 | DISPLAYSURF.fill(WHITE) 27 | DISPLAYSURF.blit(textSurfaceObj, textRectObj) 28 | for event in pygame.event.get(): 29 | if event.type == QUIT: 30 | pygame.quit() 31 | sys.exit() 32 | pygame.display.update() 33 | 34 | 35 | # There are six steps to making text appear on the screen: 36 | # 37 | # 1. Create a pygame.font.Font object. 38 | # 39 | # 2. Create a Surface object with the text drawn on it by calling the Font object’s render() method. 40 | # 41 | # 3. Create a Rect object from the Surface object by calling the Surface object’s get_rect() method. This Rect object will have the width and height correctly set for the text that was rendered, but the top and left attributes will be 0. 42 | # 43 | # 4. Set the position of the Rect object by changing one of its attributes. On line 15, we set the center of the Rect object to be at 200, 150. 44 | # 45 | # 5. Blit the Surface object with the text onto the Surface object returned by pygame.display.set_mode(). 46 | # 47 | # 6. Call pygame.display.update() to make the display Surface appear on the screen. 48 | 49 | # In[ ]: 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /HelloWorld.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "\n", 12 | "import pygame, sys\n", 13 | "from pygame.locals import *\n" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 2, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [ 23 | { 24 | "ename": "SystemExit", 25 | "evalue": "", 26 | "output_type": "error", 27 | "traceback": [ 28 | "An exception has occurred, use %tb to see the full traceback.\n", 29 | "\u001b[1;31mSystemExit\u001b[0m\n" 30 | ] 31 | }, 32 | { 33 | "name": "stderr", 34 | "output_type": "stream", 35 | "text": [ 36 | "To exit: use 'exit', 'quit', or Ctrl-D.\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "pygame.init()\n", 42 | "DISPLAYSURF = pygame.display.set_mode((400, 300))\n", 43 | "pygame.display.set_caption('Hello World!')\n", 44 | "while True:\n", 45 | " for event in pygame.event.get():\n", 46 | " if event.type == QUIT:\n", 47 | " pygame.quit()\n", 48 | " sys.exit()\n", 49 | " pygame.display.update()" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": { 56 | "collapsed": true 57 | }, 58 | "outputs": [], 59 | "source": [] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python [Root]", 65 | "language": "python", 66 | "name": "Python [Root]" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 2 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython2", 78 | "version": "2.7.12" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 0 83 | } 84 | -------------------------------------------------------------------------------- /Memory-Puzzle/memoryPuzzle.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "# Memory Puzzle clone\n", 12 | "# By Anderson Banihirwe \n" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 2, 18 | "metadata": { 19 | "collapsed": true 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "import random\n", 24 | "import pygame\n", 25 | "import sys\n", 26 | "from pygame.locals import *\n" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "# %load memoryPuzzle.py\n", 38 | "\n", 39 | "# Memory Puzzle clone\n", 40 | "# By Anderson Banihirwe \n", 41 | "\n", 42 | "\n", 43 | "import random\n", 44 | "import pygame\n", 45 | "import sys\n", 46 | "from pygame.locals import *\n", 47 | "\n", 48 | "\n", 49 | "FPS = 30 # frames per second, the general speed of the program\n", 50 | "WINDOWWIDTH = 640 # size of window's width in pixels\n", 51 | "WINDOWHEIGHT = 480 # size of window's height in pixels\n", 52 | "\n", 53 | "\n" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [] 64 | } 65 | ], 66 | "metadata": { 67 | "kernelspec": { 68 | "display_name": "Python [Root]", 69 | "language": "python", 70 | "name": "Python [Root]" 71 | }, 72 | "language_info": { 73 | "codemirror_mode": { 74 | "name": "ipython", 75 | "version": 2 76 | }, 77 | "file_extension": ".py", 78 | "mimetype": "text/x-python", 79 | "name": "python", 80 | "nbconvert_exporter": "python", 81 | "pygments_lexer": "ipython2", 82 | "version": "2.7.12" 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 0 87 | } 88 | -------------------------------------------------------------------------------- /basics/HelloWorld.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import pygame, sys\n", 12 | "from pygame.locals import *" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 2, 18 | "metadata": { 19 | "collapsed": false 20 | }, 21 | "outputs": [ 22 | { 23 | "ename": "SystemExit", 24 | "evalue": "", 25 | "output_type": "error", 26 | "traceback": [ 27 | "An exception has occurred, use %tb to see the full traceback.\n", 28 | "\u001b[1;31mSystemExit\u001b[0m\n" 29 | ] 30 | }, 31 | { 32 | "name": "stderr", 33 | "output_type": "stream", 34 | "text": [ 35 | "To exit: use 'exit', 'quit', or Ctrl-D.\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "pygame.init()\n", 41 | "DISPLAYSURF = pygame.display.set_mode((400, 300))\n", 42 | "pygame.display.set_caption('Hello World!')\n", 43 | "\n", 44 | "WHITE = (255, 255, 255)\n", 45 | "GREEN = (0, 255, 0)\n", 46 | "BLUE = (0, 0, 255)\n", 47 | "\n", 48 | "fontObj = pygame.font.Font('freesansbold.ttf', 32)\n", 49 | "textSurfaceObj = fontObj.render('Hello World!', True, GREEN, BLUE)\n", 50 | "textRectObj = textSurfaceObj.get_rect()\n", 51 | "textRectObj.center = (200, 150)\n", 52 | "\n", 53 | "while True:\n", 54 | " DISPLAYSURF.fill(WHITE)\n", 55 | " DISPLAYSURF.blit(textSurfaceObj, textRectObj)\n", 56 | " for event in pygame.event.get():\n", 57 | " if event.type == QUIT:\n", 58 | " pygame.quit()\n", 59 | " sys.exit()\n", 60 | " pygame.display.update()" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": { 66 | "collapsed": true 67 | }, 68 | "source": [ 69 | "There are six steps to making text appear on the screen:\n", 70 | "\n", 71 | "1. Create a pygame.font.Font object. \n", 72 | "\n", 73 | "2. Create a Surface object with the text drawn on it by calling the Font object’s render() method. \n", 74 | "\n", 75 | "3. Create a Rect object from the Surface object by calling the Surface object’s get_rect() method. This Rect object will have the width and height correctly set for the text that was rendered, but the top and left attributes will be 0.\n", 76 | "\n", 77 | "4. Set the position of the Rect object by changing one of its attributes. On line 15, we set the center of the Rect object to be at 200, 150.\n", 78 | "\n", 79 | "5. Blit the Surface object with the text onto the Surface object returned by pygame.display.set_mode().\n", 80 | "\n", 81 | "6. Call pygame.display.update() to make the display Surface appear on the screen." 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": { 88 | "collapsed": true 89 | }, 90 | "outputs": [], 91 | "source": [] 92 | } 93 | ], 94 | "metadata": { 95 | "kernelspec": { 96 | "display_name": "Python [Root]", 97 | "language": "python", 98 | "name": "Python [Root]" 99 | }, 100 | "language_info": { 101 | "codemirror_mode": { 102 | "name": "ipython", 103 | "version": 2 104 | }, 105 | "file_extension": ".py", 106 | "mimetype": "text/x-python", 107 | "name": "python", 108 | "nbconvert_exporter": "python", 109 | "pygments_lexer": "ipython2", 110 | "version": "2.7.12" 111 | } 112 | }, 113 | "nbformat": 4, 114 | "nbformat_minor": 0 115 | } 116 | -------------------------------------------------------------------------------- /drawing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 10, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import pygame\n", 12 | "import sys\n", 13 | "from pygame.locals import *" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 11, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "text/plain": [ 26 | "(6, 0)" 27 | ] 28 | }, 29 | "execution_count": 11, 30 | "metadata": {}, 31 | "output_type": "execute_result" 32 | } 33 | ], 34 | "source": [ 35 | "pygame.init()" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 12, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "# set up the window\n", 47 | "DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)\n", 48 | "pygame.display.set_caption('Drawing')" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 13, 54 | "metadata": { 55 | "collapsed": true 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "# set up the colors\n", 60 | "BLACK = ( 0, 0, 0)\n", 61 | "WHITE = (255, 255, 255)\n", 62 | "RED = (255, 0, 0)\n", 63 | "GREEN = ( 0, 255, 0)\n", 64 | "BLUE = ( 0, 0, 255)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 14, 70 | "metadata": { 71 | "collapsed": false 72 | }, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "" 78 | ] 79 | }, 80 | "execution_count": 14, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "DISPLAYSURF.fill(WHITE)\n", 87 | "pygame.draw.polygon(DISPLAYSURF, GREEN, ((146,0), (291, 106), (236,277),\n", 88 | " (56,277), 90,106))\n", 89 | "pygame.draw.line(DISPLAYSURF, BLUE, (60, 60), (120, 60), 4)\n", 90 | "pygame.draw.line(DISPLAYSURF, BLUE, (120, 60), (60, 120), 8)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": { 97 | "collapsed": false 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "while True:\n", 102 | " for event in pygame.event.get():\n", 103 | " if event.type == QUIT:\n", 104 | " pygame.quit()\n", 105 | " sys.exit()\n", 106 | " pygame.display.update()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": { 113 | "collapsed": true 114 | }, 115 | "outputs": [], 116 | "source": [] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [] 126 | } 127 | ], 128 | "metadata": { 129 | "kernelspec": { 130 | "display_name": "Python [Root]", 131 | "language": "python", 132 | "name": "Python [Root]" 133 | }, 134 | "language_info": { 135 | "codemirror_mode": { 136 | "name": "ipython", 137 | "version": 2 138 | }, 139 | "file_extension": ".py", 140 | "mimetype": "text/x-python", 141 | "name": "python", 142 | "nbconvert_exporter": "python", 143 | "pygments_lexer": "ipython2", 144 | "version": "2.7.12" 145 | } 146 | }, 147 | "nbformat": 4, 148 | "nbformat_minor": 0 149 | } 150 | -------------------------------------------------------------------------------- /basics/catanimation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import pygame\n", 12 | "import sys\n", 13 | "from pygame.locals import *" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 2, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "text/plain": [ 26 | "(6, 0)" 27 | ] 28 | }, 29 | "execution_count": 2, 30 | "metadata": {}, 31 | "output_type": "execute_result" 32 | } 33 | ], 34 | "source": [ 35 | "pygame.init()\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "FPS = 30 # frames per second setting\n", 47 | "fpsClock = pygame.time.Clock()" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 4, 53 | "metadata": { 54 | "collapsed": true 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "# set up the window\n", 59 | "DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)\n", 60 | "pygame.display.set_caption('Animation')" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 5, 66 | "metadata": { 67 | "collapsed": false 68 | }, 69 | "outputs": [], 70 | "source": [ 71 | "WHITE = (255, 255, 255)\n", 72 | "catImg = pygame.image.load('Cat.png')\n", 73 | "catx = 10\n", 74 | "caty = 10\n", 75 | "direction = 'right'" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 6, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [ 85 | { 86 | "ename": "SystemExit", 87 | "evalue": "", 88 | "output_type": "error", 89 | "traceback": [ 90 | "An exception has occurred, use %tb to see the full traceback.\n", 91 | "\u001b[1;31mSystemExit\u001b[0m\n" 92 | ] 93 | }, 94 | { 95 | "name": "stderr", 96 | "output_type": "stream", 97 | "text": [ 98 | "To exit: use 'exit', 'quit', or Ctrl-D.\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "while True:\n", 104 | " DISPLAYSURF.fill(WHITE)\n", 105 | " \n", 106 | " if direction == 'right':\n", 107 | " catx += 5\n", 108 | " if catx == 280:\n", 109 | " direction = 'down'\n", 110 | " \n", 111 | " elif direction == 'down':\n", 112 | " caty += 5\n", 113 | " if caty == 220:\n", 114 | " direction = 'left'\n", 115 | " \n", 116 | " elif direction == 'left':\n", 117 | " catx -= 5\n", 118 | " if catx == 10:\n", 119 | " direction = 'up'\n", 120 | " \n", 121 | " elif direction == 'up':\n", 122 | " caty -= 5\n", 123 | " if caty == 10:\n", 124 | " direction = 'right'\n", 125 | " \n", 126 | " DISPLAYSURF.blit(catImg, (catx, caty))\n", 127 | " for event in pygame.event.get():\n", 128 | " if event.type == QUIT:\n", 129 | " pygame.quit()\n", 130 | " sys.exit()\n", 131 | " pygame.display.update()\n", 132 | " fpsClock.tick(FPS)" 133 | ] 134 | } 135 | ], 136 | "metadata": { 137 | "anaconda-cloud": {}, 138 | "kernelspec": { 139 | "display_name": "Python [Root]", 140 | "language": "python", 141 | "name": "Python [Root]" 142 | }, 143 | "language_info": { 144 | "codemirror_mode": { 145 | "name": "ipython", 146 | "version": 2 147 | }, 148 | "file_extension": ".py", 149 | "mimetype": "text/x-python", 150 | "name": "python", 151 | "nbconvert_exporter": "python", 152 | "pygments_lexer": "ipython2", 153 | "version": "2.7.12" 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 0 158 | } 159 | -------------------------------------------------------------------------------- /basics/drawing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import pygame\n", 12 | "import sys\n", 13 | "from pygame.locals import *" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 2, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "text/plain": [ 26 | "(6, 0)" 27 | ] 28 | }, 29 | "execution_count": 2, 30 | "metadata": {}, 31 | "output_type": "execute_result" 32 | } 33 | ], 34 | "source": [ 35 | "pygame.init()" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "# set up the window\n", 47 | "DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)\n", 48 | "pygame.display.set_caption('Drawing')" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 4, 54 | "metadata": { 55 | "collapsed": true 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "# set up the colors\n", 60 | "BLACK = ( 0, 0, 0)\n", 61 | "WHITE = (255, 255, 255)\n", 62 | "RED = (255, 0, 0)\n", 63 | "GREEN = ( 0, 255, 0)\n", 64 | "BLUE = ( 0, 0, 255)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 5, 70 | "metadata": { 71 | "collapsed": false 72 | }, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "" 78 | ] 79 | }, 80 | "execution_count": 5, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "DISPLAYSURF.fill(WHITE)\n", 87 | "pygame.draw.polygon(DISPLAYSURF, GREEN, ((146,0), (291, 106), (236,277),\n", 88 | " (56,277), 90,106))\n", 89 | "pygame.draw.line(DISPLAYSURF, BLUE, (60, 60), (120, 60), 4)\n", 90 | "pygame.draw.line(DISPLAYSURF, BLUE, (120, 60), (60, 120), 8)\n", 91 | "pygame.draw.ellipse(DISPLAYSURF, RED, (300, 250, 40, 80), 2)\n", 92 | "pygame.draw.circle(DISPLAYSURF, GREEN, (300, 50), 20, 0)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "- pygame.draw.line(surface, color, start_point, end_point, width) – This function draws a line between the start_point and end_point parameters.\n", 100 | "\n", 101 | "- pygame.draw.lines(surface, color, closed, pointlist, width) – This function draws a series of lines from one point to the next, much like pygame.draw.polygon(). The only difference is that if you pass False for the closed parameter, there will not be a line from the last point in the pointlist parameter to the first point. If you pass True, then it will draw a line from the last point to the first.\n", 102 | "\n", 103 | "- pygame.draw.circle(surface, color, center_point, radius, width) – This function draws a circle. The center of the circle is at the center_point parameter. The integer passed for the radius parameter sets the size of the circle. The radius of a circle is the distance from the center to the edge. (The radius of a circle is always half of the diameter.) Passing 20 for the radius parameter will draw a circle that has a radius of 20 pixels.\n", 104 | "\n", 105 | "- pygame.draw.ellipse(surface, color, bounding_rectangle, width) – This function draws an ellipse (which is like a squashed or stretched circle). This function has all the usual parameters, but in order to tell the function how large and where to draw the ellipse, you must specify the bounding rectangle of the ellipse. A bounding rectangle is the smallest rectangle that can be drawn around a shape." 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 6, 111 | "metadata": { 112 | "collapsed": true 113 | }, 114 | "outputs": [], 115 | "source": [ 116 | "pixObj = pygame.PixelArray(DISPLAYSURF)\n", 117 | "pixObj[480][380] = BLACK\n", 118 | "pixObj[482][382] = BLACK\n", 119 | "pixObj[484][384] = BLACK\n", 120 | "pixObj[486][386] = BLACK\n", 121 | "pixObj[488][388] = RED\n", 122 | "del pixObj" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 7, 128 | "metadata": { 129 | "collapsed": false 130 | }, 131 | "outputs": [ 132 | { 133 | "ename": "SystemExit", 134 | "evalue": "", 135 | "output_type": "error", 136 | "traceback": [ 137 | "An exception has occurred, use %tb to see the full traceback.\n", 138 | "\u001b[1;31mSystemExit\u001b[0m\n" 139 | ] 140 | }, 141 | { 142 | "name": "stderr", 143 | "output_type": "stream", 144 | "text": [ 145 | "To exit: use 'exit', 'quit', or Ctrl-D.\n" 146 | ] 147 | } 148 | ], 149 | "source": [ 150 | "while True:\n", 151 | " for event in pygame.event.get():\n", 152 | " if event.type == QUIT:\n", 153 | " pygame.quit()\n", 154 | " sys.exit()\n", 155 | " pygame.display.update()" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": { 162 | "collapsed": true 163 | }, 164 | "outputs": [], 165 | "source": [] 166 | } 167 | ], 168 | "metadata": { 169 | "kernelspec": { 170 | "display_name": "Python [Root]", 171 | "language": "python", 172 | "name": "Python [Root]" 173 | }, 174 | "language_info": { 175 | "codemirror_mode": { 176 | "name": "ipython", 177 | "version": 2 178 | }, 179 | "file_extension": ".py", 180 | "mimetype": "text/x-python", 181 | "name": "python", 182 | "nbconvert_exporter": "python", 183 | "pygments_lexer": "ipython2", 184 | "version": "2.7.12" 185 | } 186 | }, 187 | "nbformat": 4, 188 | "nbformat_minor": 0 189 | } 190 | -------------------------------------------------------------------------------- /Memory-Puzzle/memoryPuzzle.py: -------------------------------------------------------------------------------- 1 | # Memory Puzzle clone 2 | # By Anderson Banihirwe 3 | 4 | 5 | import random 6 | import pygame 7 | import sys 8 | from pygame.locals import * 9 | 10 | FPS = 30 # frames per second, the general speed of the program 11 | WINDOWWIDTH = 640 # size of window's width in pixels 12 | WINDOWHEIGHT = 480 # size of window's height in pixels 13 | REVEALSPEED = 8 # speed boxes' sliding reveals and covers 14 | BOXSIZE = 40 # size of box height & width in pixels 15 | GAPSIZE = 10 # size of gap between boxes in pixels 16 | BOARDWIDTH = 10 # number of columns of icons 17 | BOARDHEIGHT = 7 # number of rows of icons 18 | 19 | assert (BOARDHEIGHT * BOARDWIDTH) % 2 == 0, 'Board needs to have an even number of boxes for pairs of matches.' 20 | 21 | XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * (BOXSIZE + GAPSIZE))) / 2) 22 | YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * (BOXSIZE + GAPSIZE))) / 2) 23 | 24 | GRAY = (100, 100, 100) 25 | NAVYBLUE = (60, 60, 100) 26 | WHITE = (255, 255, 255) 27 | RED = (255, 0, 0) 28 | GREEN = (0, 255, 0) 29 | BLUE = (0, 0, 255) 30 | YELLOW = (255, 255, 0) 31 | ORANGE = (255, 128, 0) 32 | PURPLE = (255, 0, 255) 33 | CYAN = (0, 255, 255) 34 | 35 | BGCOLOR = NAVYBLUE 36 | LIGHTBGCOLOR = GRAY 37 | BOXCOLOR = WHITE 38 | HIGHLIGHTCOLOR = BLUE 39 | 40 | DONUT = 'donut' 41 | SQUARE = 'square' 42 | DIAMOND = 'diamond' 43 | LINES = 'lines' 44 | OVAL = 'oval' 45 | 46 | ALLCOLORS = (RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, CYAN) 47 | ALLSHAPES = (DONUT, SQUARE, DIAMOND, LINES, OVAL) 48 | assert len(ALLCOLORS) * len(ALLSHAPES) * 2 >= BOARDWIDTH * BOARDHEIGHT, \ 49 | "Board is too big for the number of shapes/colors defined" 50 | 51 | 52 | def main(): 53 | global FPSCLOCK, DISPLAYSURF 54 | pygame.init() 55 | FPSCLOCK = pygame.time.Clock() 56 | DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) 57 | 58 | mousex = 0 # used to store x coordinate of mouse event 59 | mousey = 0 # used to store y coordinate of mouse event 60 | pygame.display.set_caption('Memory Game') 61 | 62 | mainBoard = getRandomizedBoard() 63 | revealedBoxes = generateRevealedBoxesData(False) 64 | 65 | firstSelection = None # stores the (x, y) of the first box clicked. 66 | 67 | DISPLAYSURF.fill(BGCOLOR) 68 | startGameAnimation(mainBoard) 69 | 70 | while True: # main game loop 71 | mouseClicked = False 72 | 73 | DISPLAYSURF.fill(BGCOLOR) # drawing the window 74 | drawBoard(mainBoard, revealedBoxes) 75 | 76 | for event in pygame.event.get(): # event handling loop 77 | if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE): 78 | pygame.quit() 79 | sys.exit() 80 | 81 | elif event.type == MOUSEMOTION: 82 | mousex, mousey = event.pos 83 | 84 | elif event.type == MOUSEBUTTONUP: 85 | mousex, mousey = event.pos 86 | mouseClicked = True 87 | 88 | boxx, boxy = getBoxAtPixel(mousex, mousey) 89 | 90 | if boxx is not None and boxy is not None: 91 | # The mouse is currently over a box 92 | if not revealedBoxes[boxx][boxy]: 93 | drawHighlightBox(boxx, boxy) 94 | if not revealedBoxes[boxx][boxy] and mouseClicked: 95 | revealBoxesAnimation(mainBoard, [(boxx, boxy)]) 96 | revealedBoxes[boxx][boxy] = True # set the box as revealed 97 | 98 | if firstSelection is None: # the current box was the first box clicked 99 | firstSelection = (boxx, boxy) 100 | 101 | else: # current box was the second box clicked 102 | # check if there is a match between the two icons 103 | icon1shape, icon1color = getShapeAndColor(mainBoard, 104 | firstSelection[0], firstSelection[1]) 105 | 106 | icon2shape, icon2color = getShapeAndColor(mainBoard, boxx, boxy) 107 | 108 | if icon1shape != icon2shape or icon1color != icon2color: 109 | # Icons don't match. Re-cover up both selections 110 | pygame.time.wait(1000) # 1000 milliseconds = 1 sec 111 | coverBoxesAnimation(mainBoard, [(firstSelection[0], firstSelection[1])]) 112 | revealedBoxes[firstSelection[0]][firstSelection[1]] = False 113 | 114 | revealedBoxes[boxx][boxy] = False 115 | 116 | elif haswon(revealedBoxes): # check if all pairs found 117 | gameWonAnimation(mainBoard) 118 | pygame.time.wait(2000) 119 | 120 | # Reset the board 121 | mainBoard = getRandomizedBoard() 122 | revealedBoxes = generateRevealedBoxesData(False) 123 | 124 | # show the fully unrevealed board for a second 125 | drawBoard(mainBoard, revealedBoxes) 126 | pygame.display.update() 127 | pygame.time.wait(1000) 128 | 129 | # Replay the start game animation. 130 | startGameAnimation(mainBoard) 131 | firstSelection = None # reset firstSelection variable 132 | 133 | # Redraw the screen and wait a clock tick 134 | pygame.display.update() 135 | FPSCLOCK.tick(FPS) 136 | 137 | 138 | def generateRevealedBoxesData(val): 139 | revealedBoxes = [] 140 | for i in range(BOARDWIDTH): 141 | revealedBoxes.append([val] * BOARDHEIGHT) 142 | 143 | return revealedBoxes 144 | 145 | 146 | def getRandomizedBoard(): 147 | # Get a list of every possible shape in every possible color. 148 | 149 | icons = [] 150 | for color in ALLCOLORS: 151 | for shape in ALLSHAPES: 152 | icons.append((shape, color)) 153 | 154 | random.shuffle(icons) # randomize the order of the icons list 155 | numIconsUsed = int(BOARDWIDTH * BOARDHEIGHT / 2) # calculate how many icons are neeeded 156 | icons = icons[:numIconsUsed] * 2 # make two of each 157 | random.shuffle(icons) 158 | 159 | # Create the board data structure, with randomly placed icons. 160 | 161 | board = [] 162 | for x in range(BOARDWIDTH): 163 | column = [] 164 | for y in range(BOARDHEIGHT): 165 | column.append(icons[0]) 166 | del icons[0] # remove the icons as we assign them 167 | 168 | board.append(column) 169 | return board 170 | 171 | 172 | def splitIntoGroupsOf(groupSize, theList): 173 | # splits a list into a list of lists, where the inner lists have 174 | # at most groupSize number of items 175 | 176 | result = [] 177 | for i in range(0, len(theList), groupSize): 178 | result.append(theList[i:i + groupSize]) 179 | 180 | return result 181 | 182 | 183 | def leftTopCoordsOfBox(boxx, boxy): 184 | # Convert board coordinates to pixel coordinates 185 | left = boxx * (BOXSIZE + GAPSIZE) + XMARGIN 186 | top = boxy * (BOXSIZE + GAPSIZE) + YMARGIN 187 | return left, top 188 | 189 | 190 | def getBoxAtPixel(x, y): 191 | for boxx in range(BOARDWIDTH): 192 | for boxy in range(BOARDHEIGHT): 193 | left, top = leftTopCoordsOfBox(boxx, boxy) 194 | boxRect = pygame.Rect(left, top, BOXSIZE, BOXSIZE) 195 | if boxRect.collidepoint(x, y): 196 | return boxx, boxy 197 | 198 | return None, None 199 | 200 | 201 | def drawIcon(shape, color, boxx, boxy): 202 | quarter = int(BOXSIZE * 0.25) # syntactic sugar 203 | half = int(BOXSIZE * 0.5) # syntactic sugar 204 | 205 | left, top = leftTopCoordsOfBox(boxx, boxy) # get pixel coords from board coords 206 | 207 | # Draw the shapes 208 | if shape == DONUT: 209 | pygame.draw.circle(DISPLAYSURF, color, (left + half, top + half), half - 5) 210 | 211 | pygame.draw.circle(DISPLAYSURF, BGCOLOR, (left + half, top + half), quarter - 5) 212 | 213 | elif shape == SQUARE: 214 | pygame.draw.rect(DISPLAYSURF, color, (left + quarter, top + quarter, BOXSIZE - half, BOXSIZE - half)) 215 | 216 | elif shape == DIAMOND: 217 | pygame.draw.polygon(DISPLAYSURF, color, ((left + half, top), (left + half, top + BOXSIZE - 1), 218 | (left + BOXSIZE - 1, top + half), (left, top + half))) 219 | 220 | elif shape == LINES: 221 | for i in range(0, BOXSIZE, 4): 222 | pygame.draw.line(DISPLAYSURF, color, (left, top + i), (left + i, top)) 223 | pygame.draw.line(DISPLAYSURF, color, (left + i, top + BOXSIZE - 1), (left + BOXSIZE - 1, top + i)) 224 | 225 | elif shape == OVAL: 226 | pygame.draw.ellipse(DISPLAYSURF, color, (left, top + quarter, BOXSIZE, half)) 227 | 228 | 229 | def getShapeAndColor(board, boxx, boxy): 230 | # shape value for x, y spot is stored in board[x][y][0] 231 | # color value for x, y spot is stored in board[x][y][1] 232 | 233 | return board[boxx][boxy][0], board[boxx][boxy][1] 234 | 235 | 236 | def drawBoxCOvers(board, boxes, coverage): 237 | # Draws boxes being covered/ revealed. "Boxes" is a list 238 | # of two-item lists, which have the x & y spot of the box. 239 | 240 | for box in boxes: 241 | left, top = leftTopCoordsOfBox(box[0], box[1]) 242 | pygame.draw.rect(DISPLAYSURF, BGCOLOR, (left, top, BOXSIZE, BOXSIZE)) 243 | 244 | shape, color = getShapeAndColor(board, box[0], box[1]) 245 | drawIcon(shape, color, box[0], box[1]) 246 | if coverage > 0: # only draw the cover if there is an average 247 | pygame.draw.rect(DISPLAYSURF, BOXCOLOR, (left, top, coverage, BOXSIZE)) 248 | 249 | pygame.display.update() 250 | FPSCLOCK.tick(FPS) 251 | 252 | 253 | def revealBoxesAnimation(board, boxesToReveal): 254 | # Do the "box reveal" animation 255 | for coverage in range(BOXSIZE, (-REVEALSPEED) - 1, - REVEALSPEED): 256 | drawBoxCOvers(board, boxesToReveal, coverage) 257 | 258 | 259 | def coverBoxesAnimation(board, boxesToCover): 260 | # Do the "box cover" animation 261 | for coverage in range(0, BOXSIZE + REVEALSPEED, REVEALSPEED): 262 | drawBoxCOvers(board, boxesToCover, coverage) 263 | 264 | 265 | def drawBoard(board, revealed): 266 | # Draws all of the boxes in their covered or revealed state. 267 | 268 | for boxx in range(BOARDWIDTH): 269 | for boxy in range(BOARDHEIGHT): 270 | left, top = leftTopCoordsOfBox(boxx, boxy) 271 | if not revealed[boxx][boxy]: 272 | # Draw a covered box 273 | pygame.draw.rect(DISPLAYSURF, BOXCOLOR, (left, top, BOXSIZE, BOXSIZE)) 274 | 275 | else: 276 | # Draw the (revealed) icon 277 | shape, color = getShapeAndColor(board, boxx, boxy) 278 | drawIcon(shape, color, boxx, boxy) 279 | 280 | 281 | def drawHighlightBox(boxx, boxy): 282 | left, top = leftTopCoordsOfBox(boxx, boxy) 283 | pygame.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR, (left - 5, top - 5, 284 | BOXSIZE + 10, BOXSIZE + 10), 4) 285 | 286 | 287 | def startGameAnimation(board): 288 | # Randomly reveal the boxes 8 at a time. 289 | 290 | coveredBoxes = generateRevealedBoxesData(False) 291 | boxes = [] 292 | for x in range(BOARDWIDTH): 293 | for y in range(BOARDHEIGHT): 294 | boxes.append((x, y)) 295 | 296 | random.shuffle(boxes) 297 | boxGroups = splitIntoGroupsOf(8, boxes) 298 | 299 | drawBoard(board, coveredBoxes) 300 | for boxGroup in boxGroups: 301 | revealBoxesAnimation(board, boxGroup) 302 | coverBoxesAnimation(board, boxGroup) 303 | 304 | 305 | def gameWonAnimation(board): 306 | # flash the background color when the player has won 307 | coveredBoxes = generateRevealedBoxesData(True) 308 | color1 = LIGHTBGCOLOR 309 | color2 = BGCOLOR 310 | 311 | for i in range(13): 312 | color1, color2 = color2, color1 # swap colors 313 | DISPLAYSURF.fill(color1) 314 | drawBoard(board, coveredBoxes) 315 | pygame.display.update() 316 | pygame.time.wait(300) 317 | 318 | 319 | def haswon(revealedBoxes): 320 | # Returns True if all the boxes have been revealed, otherwise False 321 | 322 | for i in revealedBoxes: 323 | if False in i: 324 | return False # return False if any boxes are covered. 325 | 326 | return True 327 | 328 | if __name__ == '__main__': 329 | main() 330 | --------------------------------------------------------------------------------