Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Project Proposal - Softdes MP4.pdf
Binary file not shown.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# InteractiveProgramming
This is the base repo for the interactive programming project for Software Design, Spring 2018 at Olin College.

Installation Guide:

pip install pygame
pip install numpy
pip install random
pip install sys
pip install time
pip install copy

to run game, run command: python3 Tetris.py
287 changes: 287 additions & 0 deletions Tetris.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"import pygame\n",
"from pygame.locals import *\n",
"import numpy\n",
"import random\n",
"import sys\n",
"import time\n",
"\n",
"FPS = 25\n",
"WindowWidth = 450 #used to be 415\n",
"WindowHeight = 900 #used to be 815\n",
"BoxSize = 30\n",
"BoardWidth = 20\n",
"BoardHeight = 40\n",
"BorderColor = (255,255,255)\n",
"BGColor = (0, 0, 0)\n",
"TemplateWidth = 15\n",
"TemplateHeight = 15\n",
"\n",
"XMargin = int((WindowWidth - BoardWidth * BoxSize)/2)\n",
"TopMargin = WindowHeight - (BoardHeight * BoxSize) -5\n",
"\n",
"#Templates of pieces:\n",
"\n",
"S_Shape = [[[180,0], [225,0], [135,45], [180,45]], [[180,0],[180,45],[225,45],[225,90]]]\n",
"\n",
"Z_Shape = [[[135,0],[180,0],[180,45],[225,45]],[[225,0],[225,45],[180,45],[180,90]]]\n",
"\n",
"I_Shape = [[[180,0],[180,45],[180,90],[180,135]],[[135,0],[180,0],[225,0],[270,0]]]\n",
"\n",
"O_Shape = [[180,45],[225,45],[180,90],[225,90]]\n",
"\n",
"J_Shape = [[[180,45],[180,90],[225,90],[270,90]], [[180,0],[225,0],[180,45],[180,90]],[[180,45],[225,45],[270,45],[270,90]], [[225,0],[225,45],[225,90],[180,90]]]\n",
"\n",
"L_Shape = [[[180,45],[225,45],[270,45],[270,0]],[[180,0],[180,45],[180,90],[225,90]],[[180,45],[225,45],[270,45],[180,90]], [[180,0],[225,0],[225,45],[225,90]]]\n",
"\n",
"T_Shape = [[[180,0],[135,45],[180,45],[225,45]],[[180,0],[180,45],[180,90],[225,45]], [[180,45],[225,45],[270,45],[225,90]],[[225,0],[225,45],[225,90],[180,45]]]\n",
"\n",
"\n",
" \n",
"Pieces = {'S': [[[180,0], [225,0], [135,45], [180,45]], [[180,0],[180,45],[225,45],[225,90]]],\n",
" 'Z': Z_Shape,\n",
" 'J': J_Shape,\n",
" 'L': L_Shape,\n",
" 'I': I_Shape,\n",
" 'O': O_Shape,\n",
" 'T': T_Shape}\n",
"\n",
"Coordinates = {'1': [135,0],\n",
" '2': [180,0],\n",
" '3': [225,0],\n",
" '4': [270,0],\n",
" '5': [135,45],\n",
" '6': [180,45],\n",
" '7': [225,45],\n",
" '8': [270,45],\n",
" '9': [135,90],\n",
" '10': [180,90],\n",
" '11': [225,90],\n",
" '12': [270,90],\n",
" '13': [135,135],\n",
" '14': [180,135],\n",
" '15': [225,135],\n",
" '16': [270,135]}\n",
"\n",
"\n",
"\n",
"\n",
"class Block():\n",
" def __init__(self,width,height,x1,y1,val):\n",
" self.width = width\n",
" self.height = height\n",
" self.x1 = x1\n",
" self.y1 = y1\n",
" self.val = val\n",
" \n",
" def convertToPixelCoords(self):\n",
" return (XMargin + (self.x1 * BoxSize),(TopMargin +(self.y1 * BoxSize)))\n",
" \n",
" def drawBlock(self, pixelx = None, pixely = None):\n",
" if self.val == 0:\n",
" return \n",
" if pixelx == None and pixely == None:\n",
" pixelx,pixely = convertToPixelCoords(self.x1, self.y1)\n",
" pygame.draw.rect(DisplaySurf, (0,0,0), (pixelx + 1, pixely + 1, BoxSize - 1, BoxSize - 1))\n",
" pygame.draw.rect(DisplaySurf, (34,89,233), (pixelx + 1, pixely + 1, BoxSize - 4, BoxSize - 4))\n",
" \n",
" def __str__(self):\n",
" return 'Coords are: (%.d, %.d) with val %.d' %(self.x1,self.y1,self.val)\n",
" \n",
"class Piece():\n",
" def __init__(self):\n",
" self.shape = random.choice(list(Pieces.keys()))\n",
" self.rotation = int(random.randint(0,len(Pieces[self.shape])-1))\n",
" \n",
" self.coordinates = []\n",
" \n",
" \n",
" \n",
" b1 = Block(20,20,Pieces[self.shape][self.rotation][0][0],Pieces[self.shape][self.rotation][0][1],1)\n",
" b2 = Block(20,20,Pieces[self.shape][self.rotation][1][0],Pieces[self.shape][self.rotation][1][1],1)\n",
" b3 = Block(20,20,Pieces[self.shape][self.rotation][2][0],Pieces[self.shape][self.rotation][2][1],1)\n",
" b4 = Block(20,20,Pieces[self.shape][self.rotation][3][0],Pieces[self.shape][self.rotation][3][1],1)\n",
" print(b1,b2,b3,b4)\n",
" self.blocks = [b1,b2,b3,b4]\n",
" print(self.blocks)\n",
" \n",
" def convertToShapeCoords(self):\n",
" \n",
" for i in self.shape[self.rotation]:\n",
" self.coordinates = self.coordinates.append(Coordinates[Shapes[self.rotation][i]])\n",
" \n",
" def drawPiece(self,pixelx=None,pixely=None):\n",
" \n",
" if b.val == 0:\n",
" return \n",
" if pixelx == None and pixely == None:\n",
" pixelx = b.x1\n",
" pixely = b.y1\n",
"\n",
" pygame.draw.rect(DisplaySurf, (0,0,0), (pixelx + 1, pixely + 1, BoxSize - 1, BoxSize - 1))\n",
" pygame.draw.rect(DisplaySurf, (34,89,233), (pixelx + 1, pixely + 1, BoxSize - 4, BoxSize - 4))\n",
" \n",
" def translate():\n",
" #Loop through blocks and change x y coords\n",
" #localize block to its own coordinate system, so that it can be defined by one point\n",
" #Need a get-block positions () function to get block x and y coords\n",
" pass\n",
" \n",
" \n",
" \n",
"\n",
"\n",
"\n",
"class Grid():\n",
" def __init__(self, width, height):\n",
" self.width = width\n",
" self.height = height\n",
" self.grid = []\n",
" def append(self, thing):\n",
" self.grid.append(thing)\n",
" def make_grid(self):\n",
" grid = []\n",
" for i in range(self.width):\n",
" self.append([0] * self.height)\n",
" return grid\n",
" \n",
" def draw_grid(self):\n",
" #Draws the grid\n",
" pygame.draw.rect(DisplaySurf, BorderColor, (XMargin - 3, TopMargin - 7, (BoardWidth * BoxSize) + 8, (BoardHeight * BoxSize) + 8), 5)\n",
" \n",
" #Fill Background of board\n",
" pygame.draw.rect(DisplaySurf, BGColor, (XMargin, TopMargin, BoxSize * BoardWidth, BoxSize * BoardHeight))\n",
" \n",
" #Draw individual boxes\n",
" #or x in range (BoardWidth):\n",
" #or y in range(BoardHeight):\n",
" #elf.block.drawBlock(x,y)\n",
" \n",
" def addToBoard(self, block):\n",
" for x in range(TemplateWidth):\n",
" for y in range(TemplateHeight):\n",
" if block.val == 1:\n",
" block.drawBlock(x + block.x1, y + block.y1)\n",
" \n",
" def addShape(self,piece):\n",
" for block in piece.blocks:\n",
" for x in range(TemplateWidth):\n",
" for y in range(TemplateHeight):\n",
" if block.val == 1:\n",
" block.drawBlock(x + block.x1, y + block.y1)\n",
" \n",
"def checkForQuit():\n",
" for event in pygame.event.get(QUIT): # get all the QUIT events\n",
" terminate() # terminate if any QUIT events are present\n",
" for event in pygame.event.get(KEYUP): # get all the KEYUP events\n",
" if event.key == K_ESCAPE:\n",
" terminate() # terminate if the KEYUP event was for the Esc key\n",
" pygame.event.post(event) # put the other KEYUP event objects back\n",
" \n",
"def checkForKeyPress():\n",
" # Go through event queue looking for a KEYUP event.\n",
" # Grab KEYDOWN events to remove them from the event queue.\n",
" checkForQuit()\n",
"\n",
" for event in pygame.event.get([KEYDOWN, KEYUP]):\n",
" if event.type == KEYDOWN:\n",
" continue\n",
" return event.key\n",
" return None\n"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Coords are: (135, 0) with val 1 Coords are: (180, 0) with val 1 Coords are: (180, 45) with val 1 Coords are: (225, 45) with val 1\n",
"[<__main__.Block object at 0x7fb702296a90>, <__main__.Block object at 0x7fb702296f98>, <__main__.Block object at 0x7fb702296e10>, <__main__.Block object at 0x7fb7022962e8>]\n",
"<__main__.Piece object at 0x7fb70226e6a0>\n"
]
}
],
"source": [
"def main():\n",
" global FPSClock, DisplaySurf, BasicFont, BigFont, BorderColor\n",
" pygame.init()\n",
" DisplaySurf = pygame.display.set_mode((WindowWidth, WindowHeight))\n",
" runGame()\n",
" \n",
"def runGame():\n",
" \n",
" piece = Piece()\n",
" print(piece)\n",
" grid = Grid(20, 40)\n",
" grid.make_grid()\n",
" grid.draw_grid()\n",
" grid.addShape(piece)\n",
" \n",
" \n",
" pygame.display.update()\n",
"\n",
"def terminate():\n",
" pygame.quit()\n",
" sys.exit()\n",
" \n",
" \n",
"if __name__ == '__main__':\n",
" main()\n",
" \n",
" \n",
"\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added Tetris.pdf
Binary file not shown.
Loading