diff --git a/BlankProjectTemplate/src/Snake_2.o_Demo.py b/BlankProjectTemplate/src/Snake_2.o_Demo.py index 7dde949d6f882fe846034401a2eaa01d39e8497b..5e0b834c7088d272de27fa6075e16b04aa1f425f 100644 --- a/BlankProjectTemplate/src/Snake_2.o_Demo.py +++ b/BlankProjectTemplate/src/Snake_2.o_Demo.py @@ -1,6 +1,7 @@ #standard set up import pygame from random import randint + pygame.init() #setting width and height of window @@ -14,11 +15,13 @@ pygame.display.set_caption("My Game") white = (255,255,255) black = (0,0,0) -x = randint(40,400) -y = randint(40,400) + width = 20 height = 20 vel = 10 +score = 0 +x = randint(0,screen_x - width) +y = randint(0, screen_y - height) speed = 40 # 0 - (- direction) , 1 - (+ direction) @@ -26,8 +29,17 @@ direction = 0 # 0 - x-axis , 1 - y-axis axis = 0 -run = True +location = [] + +food_x = randint(0,screen_x - width) +food_y = randint(0,screen_y - height) +location = [food_x, food_y] + + + +run = True + while run: pygame.time.delay(speed) #create a delay to prevent any unwanted behaviour @@ -71,9 +83,21 @@ while run: #x = 500 - height x = 0 - win.fill(white) + #all colors are defined in RGB with Pygame + + if( abs(x - food_x) < 15 and abs(y - food_y) < 15): + score += 1 + food_x = randint(0,screen_x - width) + food_y = randint(0,screen_y - height) + location = [food_x, food_y] + print('score = ', score) + + + win.fill(white) + pygame.draw.rect(win,(0,0,255), (location[0], location[1], width, height)) pygame.draw.rect(win,(255,0,0), (x,y,width, height)) + #we have to update the display to see the drawing of our object. Since it does #not automatically update pygame.display.update()