From c0951368e967bad7f61093e6de801b27db2129e3 Mon Sep 17 00:00:00 2001 From: Usman Irfan <usmanirfan@USERs-MacBook-Pro.local> Date: Fri, 9 Nov 2018 18:56:24 -0500 Subject: [PATCH] Update food --- BlankProjectTemplate/src/Food.py | 34 ++++++++++++++++++ BlankProjectTemplate/src/Gameplay.py | 22 ++++++------ .../src/__pycache__/Food.cpython-37.pyc | Bin 0 -> 947 bytes .../src/__pycache__/Snake.cpython-37.pyc | Bin 0 -> 682 bytes .../src/__pycache__/init.cpython-37.pyc | Bin 0 -> 406 bytes 5 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 BlankProjectTemplate/src/Food.py create mode 100644 BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc create mode 100644 BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc create mode 100644 BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc diff --git a/BlankProjectTemplate/src/Food.py b/BlankProjectTemplate/src/Food.py new file mode 100644 index 0000000..561c0ff --- /dev/null +++ b/BlankProjectTemplate/src/Food.py @@ -0,0 +1,34 @@ +## @file Food.py +# @author Usman Irfan +# @brief implements an abstract data type for a snake's food +# @date 21/02/2018 +from random import randint +from init import * + +## @brief An Abstract Data type which represents a one-unit of food +class Food(): + + + ## @brief Food constructor + # @details Initializez the size of the food, this needs to be the same as snake's + # block size + # @param blockSize the width and height of the square block representing the food + def __init__(self, blockSize): + self.size = blockSize + + ## @brief Draw method uses pygame to draw the food object on the window + # @param location A list which consists the x and y location of the food + def draw_food(self, location): + pygame.draw.rect(win, blue , (location[0],location[1], self.size, self.size)) + + ## @brief redraw_food method redraws the food on the screen randomly + # @param x is the location of snake's x-axis head location + # @param y is the location of snake's y-axis head location + # @param location is a list that gives the location of present food + # @param screenSize is the size of the screen + def redraw_food(self, x, y, location,screenSize): + if( abs(x - location[0]) < 15 and abs(y - location[1]) < 15): + location[0] = randint(0, screenSize - self.size) + location[1] = randint(0, screenSize - self.size) + return location + diff --git a/BlankProjectTemplate/src/Gameplay.py b/BlankProjectTemplate/src/Gameplay.py index 52a24d3..68f0b2a 100644 --- a/BlankProjectTemplate/src/Gameplay.py +++ b/BlankProjectTemplate/src/Gameplay.py @@ -6,6 +6,7 @@ from random import randint from Snake import * +from Food import * from init import * # One size for all blocks created for snake @@ -27,15 +28,15 @@ direction = 0 # 0 - x-axis , 1 - y-axis axis = 0 +score = 0 location = [] - food_x = randint(0,screenSize - size) food_y = randint(0,screenSize - size) location = [food_x, food_y] ##initialize snake snake = Snake(size, 0, 20, 0) - +food = Food(size) #Loop through the events as long as the game is running run = True while run: @@ -83,13 +84,13 @@ while run: #all colors are defined in RGB with Pygame #consumption of food block - if( abs(x - food_x) < 15 and abs(y - food_y) < 15): - score += 1 - food_x = randint(0,screenSize - size) - food_y = randint(0,screenSize - size) - location = [food_x, food_y] - print('score = ', score) + + food.redraw_food(x, y, location, screenSize) + + if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15): + score += 1 + print(score) ## --------------------------DELETE -------------------------- #extend length or size depending on the direction of the snake ## if (axis): @@ -104,8 +105,9 @@ while run: win.fill(white) #Draw food item - pygame.draw.rect(win,blue, (location[0], location[1], size, size)) - + food.draw_food(location) + #food.update_score(x,y,location, score) + #Draw snake snake.draw(x,y) diff --git a/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..94d697adc16cf576c9da39ab7670614a7ab91a37 GIT binary patch literal 947 zcmZ8g&2G~`5Z*sKPTE32AeBRJ2-O_$q1OnNg5VY*B}gb7R*rXrocJf~I%*T;*1iER z07qVFuSiIJ1y0PYNtJf3nVp>(&wk&GeSUDzBe2%Tf4+PV2>FeN?c!jZf!G(I6j8Jw zB^^_WHY?bOd?1P|aZQxCVq>m?A&J8q$R#m#9g|inuNpAv%M6{{#lScNvClziGNy`* znWBna6DZ;fj)e+T2<Jd`plIa8`?^*czKIKWl(!%j@o(scNPq>obO1LBo3C}uVAsXe zbtgrg&4=LnMN9<gf8QjZtTr}T+A^(jGfk`HowoC)UL;nZUZ$tXd68E0kEWjKtQqNY zQKXGdtjQ7t@@lbik(7CrH&PxV2#<`e4}5eRxq}z)^>|OfXvmEIU=44?hAR3D@GAzI z%mS3D_{p)!z=jVDh5Q>-ap=Ou>T6nRCzMGqoG<{+@r%52Vp1%%fdP`3nGiM?0q3;I z>&i23RTl$Yd{<6^##1<Mkw5UrZ$lZD!iBp4@z6hm(O|PVf<7Ww0DDf%2z(5D{EiQZ zZ3NI^kcUy)C(V5y?~@H5wp7J`st@53vj-~Xj;9lAu(vI>pruAH9bK76;C4Nm8Lcav z?R}nmM&HsohFpsiAoPSjfoFZ_`R=3)puI5D3fe2B>q%MGYFVJYFXgvoTC@>^pPPA% z>IfAU-BH+KgZ)Co;vk|<6hx8t4PxO!{4MIzlX3#TARpQ(;Vb${Mg11TYaD{bs{1tJ F{{XGe%oG3s literal 0 HcmV?d00001 diff --git a/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f5fd95f0b33cb1498f06e45af003482d739bb8db GIT binary patch literal 682 zcmZuuy^hmB5T5<9&j|#H7E#ce0vFdJbP7mx5mJuE6syI%K~5aU>^iz6a>qRglsv(` z$CcY63SI#TiJ7$`Q83ngGwYq7{buH~*%VOj|NQ)LCjoxY;Vc53D^&jk!GK{6T*F&1 z+=yE+;tMaCkxR&vUw98W3+#DC-dQ+0SE&9ef(49*k{f3DEtJ9tBhgBeV1+a!%T{k} zm0%<ZSxSG68q)_cfP#S|vVkG;K_UtRn9dft42kcoH6b;%vsE9YuTM5eeW-n&;K??t za`Lg+RhuPVIBMXKFt`hZ@8BkAe$R{d-a23G{Z_ZNTj{oVWBsPzb%nQ=2Yp%08{KZ+ zx!oFjT-$Be=-w9IRYiRNi|!aQrRuism3oX7eTD!mjo<0f7<3M(Xuu?7DKN5qhE5M_ z_LcXdm&g@4uM^Uh{3aMKVq~0#v^##%TO?_mz6mnY7B_WU3TKT=u}&@=)eY<r*fCNZ zfp+d@2((#Gg9dh*j<2I)3mh5pXtBCdVXD-2XZ8*G50(17*Ufn29$;du^G|IWq>BxR j7CD~goLyuSM}HPM50WMnPyb5{*~_utE80zbUMBtmr%;un literal 0 HcmV?d00001 diff --git a/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c240177ee288458b3dcbdf44904b8803ebfeb896 GIT binary patch literal 406 zcmY*Tu};G<5VaF0O<OAQ39MZjiLMAi)fpib7Be6>zJi;?iELM;-T9Ap<aZdoB8Kiv zNC+-PMdBnszjyC+_eQ;5hhV*az27}kLf#s2D2C($pB*DeB56T50kWZ6LWvA`0AwC+ z7z7Z^f-U9IUdJ-rQuG;b$%rR1=55*H9Z4sopX_iGxl$i^YTce_k;BR9pz4RaZ|s}k z0R8Zn;}|gwVB?SPt$&UEMAygl>Oqv?nbOMnj?D~!o~SkWmQ=Pb#EK^t+<jR|@ZnNv z-fd(iYNsmgqvfM=Xc&;5O^XHaXj+IYe<7yZ<Y?gYGQG86Y`U<e(8|n&PRC$#SJkP7 z;gcAqqeAHX+EjDM+zpg?)<J4bmNuyd^~xt_7+_Jrd5jC~F+vh1l+j~)6f*o7{Q=KC BWt0E_ literal 0 HcmV?d00001 -- GitLab