From 3f3330eedcc866f6f5e53c0e434ff14140921b58 Mon Sep 17 00:00:00 2001 From: andyh98 <hameea1@mcmaster.ca> Date: Fri, 12 Oct 2018 19:46:16 -0400 Subject: [PATCH] Game Window, Snake body & movement --- .../POC_Demo/Snake_2.o_Demo.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 BlankProjectTemplate/POC_Demo/Snake_2.o_Demo.py diff --git a/BlankProjectTemplate/POC_Demo/Snake_2.o_Demo.py b/BlankProjectTemplate/POC_Demo/Snake_2.o_Demo.py new file mode 100644 index 0000000..28bf90d --- /dev/null +++ b/BlankProjectTemplate/POC_Demo/Snake_2.o_Demo.py @@ -0,0 +1,70 @@ +#standard set up +import pygame +pygame.init() + +#setting width and height of window +win = pygame.display.set_mode((500,500)) + +pygame.display.set_caption("My Game") + +white = (255,255,255) +black = (0,0,0) + +x = 50 +y = 50 +width = 40 +height = 40 +vel = 10 + +speed = 40 +# 0 - (- direction) , 1 - (+ direction) +direction = 0 +# 0 - x-axis , 1 - y-axis +axis = 0 + +run = True + +def changeXY(x,y,axis,direction): + if not (axis): + x += vel*direction +## else: +## y += vel*direction + + +while run: + pygame.time.delay(speed) #create a delay to prevent any unwanted behaviour + + #events are anything that happens from the user + #we loop through the list of events that happen by the user's actions + for event in pygame.event.get(): + + if event.type == pygame.QUIT: + run = False + + #Each event type has an integer assigned to it. KEYDOWN has the code 2 + if event.type == pygame.KEYDOWN: + if (event.key == pygame.K_LEFT): + axis = 0;direction=-1 + if (event.key == pygame.K_RIGHT): + axis = 0;direction=1 + if (event.key == pygame.K_UP): + axis = 1;direction=-1 + if (event.key == pygame.K_DOWN): + axis = 1;direction=1 + + #Snake moving depending on axis and direction + if (axis): + y += vel*direction + else: + x += vel*direction + + + win.fill(white) + #all colors are defined in RGB with Pygame + 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() + +#Quit the game +pygame.quit() -- GitLab