Skip to content
Snippets Groups Projects
Commit 7019c57a authored by 's avatar
Browse files

add code

parent 40e93468
No related branches found
No related tags found
No related merge requests found
#standard set up
import pygame
from random import randint
pygame.init()
#setting width and height of window
screen_x = 500
screen_y = 500
win = pygame.display.set_mode((screen_x,screen_y))
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
speed = 40
# 0 - (- direction) , 1 - (+ direction)
direction = 0
# 0 - x-axis , 1 - y-axis
axis = 0
run = True
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
if x < 0:
#x = 0
x = screen_x - height
if y < 0:
#y = 0
y = screen_y - width
if y > screen_y - width:
#y = 500 - width
y = 0
if x > screen_x - height:
#x = 500 - height
x = 0
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()
#importing necessary libraries
import pygame, sys
#a function made to execute other files from the system
def runfile(runfilename):
with open(runfilename,"r") as rnf:
exec(rnf.read())
pygame.init()
#while loop required to always refresh the page
while True:
game = pygame.display.set_mode((800, 610))
game.fill([213, 219, 219])
mousepos = pygame.mouse.get_pos() #checking mouse position
mouseclick = pygame.mouse.get_pressed()#checking mouse pressed
pygame.display.set_caption("Lets Play")
title_font = pygame.font.SysFont("monospace", 80)
#Adding the title
gamename = title_font.render('SNAKE GAME', True, (255, 0, 0))
game.blit(gamename,(150,80))
#Adding the play game button
if (270 <= mousepos[0] <= 270+250 and 505 <= mousepos[1] <= 555 ):
#checks if the mouse is hovering over the button
pygame.draw.rect(game,[100,100,100], [270,505,250,50], 0)
#checking if the button is clicked
if mouseclick[0] == 1:
runfile('Snake_2.o_Demo.py')
else:
pygame.draw.rect(game,[180,180,180], [270,505,250,50], 0)
playgame_font = pygame.font.SysFont("comicsansms", 40)
gamebutton = playgame_font.render('GAME TIME', True, (0, 0, 200))
game.blit(gamebutton,(275,500))
#Taking user name
UserName = playgame_font.render('NAME:', True, (0, 250, 0))
game.blit(UserName,(50,200))
#Asking user for theme
ThemeOption = playgame_font.render('THEMES:', True, (0, 200, 0))
game.blit(ThemeOption,(50,300))
#Asking user for speed
SpeedOption = playgame_font.render('SPEED:', True, (0, 150, 0))
game.blit(SpeedOption,(50,400))
#If user wants to quit
pygame.draw.rect(game,[180,180,180], [650,550,130,55], 0)
Quit = playgame_font.render('QUIT', True, (0, 0, 0))
game.blit(Quit,(650,550))
if ( 650 <= mousepos[0] <= 650+130 and 550 <= mousepos[1] <= 550 +55):
#checks if the mouse is hovering over the button
#checking if the button is clicked
if mouseclick[0] == 1:
#print ("quiting")
#pygame.display.quit()
pygame.quit()
sys.exit()
pygame.display.update()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment