Showing posts with label pygame. Show all posts
Showing posts with label pygame. Show all posts

Balloon Shooter Game

import pygame

import sys

import random

from math import *

pygame.init()

width = 700

height = 600

display = pygame.display.set_mode((width, height))

pygame.display.set_caption("CopyAssignment - Balloon Shooter Game")

clock = pygame.time.Clock()

margin = 100

lowerBound = 100

score = 0

white = (230, 230, 230)

lightBlue = (4, 27, 96)

red = (231, 76, 60)

lightGreen = (25, 111, 61)

darkGray = (40, 55, 71)

darkBlue = (64, 178, 239)

green = (35, 155, 86)

yellow = (244, 208, 63)

blue = (46, 134, 193)

purple = (155, 89, 182)

orange = (243, 156, 18)

font = pygame.font.SysFont("Arial", 25)

class Balloon:

    def __init__(self, speed):

        self.a = random.randint(30, 40)

        self.b = self.a + random.randint(0, 10)

        self.x = random.randrange(margin, width - self.a - margin)

        self.y = height - lowerBound

        self.angle = 90

        self.speed = -speed

        self.proPool= [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]

        self.length = random.randint(50, 100)

        self.color = random.choice([red, green, purple, orange, yellow, blue])

    def move(self):

        direct = random.choice(self.proPool)

        if direct == -1:

            self.angle += -10

        elif direct == 0:

            self.angle += 0

        else:

            self.angle += 10

        self.y += self.speed*sin(radians(self.angle))

        self.x += self.speed*cos(radians(self.angle))

        if (self.x + self.a > width) or (self.x < 0):

            if self.y > height/5:

                self.x -= self.speed*cos(radians(self.angle)) 

            else:

                self.reset()

        if self.y + self.b < 0 or self.y > height + 30:

            self.reset()         

    def show(self):

        pygame.draw.line(display, darkBlue, (self.x + self.a/2, self.y + self.b), (self.x + self.a/2, self.y + self.b + self.length))

        pygame.draw.ellipse(display, self.color, (self.x, self.y, self.a, self.b))

        pygame.draw.ellipse(display, self.color, (self.x + self.a/2 - 5, self.y + self.b - 3, 10, 10))   

    def burst(self):

        global score

        pos = pygame.mouse.get_pos()

        if isonBalloon(self.x, self.y, self.a, self.b, pos):

            score += 1

            self.reset()                

    def reset(self):

        self.a = random.randint(30, 40)

        self.b = self.a + random.randint(0, 10)

        self.x = random.randrange(margin, width - self.a - margin)

        self.y = height - lowerBound 

        self.angle = 90

        self.speed -= 0.002

        self.proPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]

        self.length = random.randint(50, 100)

        self.color = random.choice([red, green, purple, orange, yellow, blue])   

balloons = []

noBalloon = 10

for i in range(noBalloon):

    obj = Balloon(random.choice([1, 1, 2, 2, 2, 2, 3, 3, 3, 4]))

    balloons.append(obj)

def isonBalloon(x, y, a, b, pos):

    if (x < pos[0] < x + a) and (y < pos[1] < y + b):

        return True

    else:

        return False   

def pointer():

    pos = pygame.mouse.get_pos()

    r = 25

    l = 20

    color = lightGreen

    for i in range(noBalloon):

        if isonBalloon(balloons[i].x, balloons[i].y, balloons[i].a, balloons[i].b, pos):

            color = red

    pygame.draw.ellipse(display, color, (pos[0] - r/2, pos[1] - r/2, r, r), 4)

    pygame.draw.line(display, color, (pos[0], pos[1] - l/2), (pos[0], pos[1] - l), 4)

    pygame.draw.line(display, color, (pos[0] + l/2, pos[1]), (pos[0] + l, pos[1]), 4)

    pygame.draw.line(display, color, (pos[0], pos[1] + l/2), (pos[0], pos[1] + l), 4)

    pygame.draw.line(display, color, (pos[0] - l/2, pos[1]), (pos[0] - l, pos[1]), 4)

def lowerPlatform():

    pygame.draw.rect(display, darkGray, (0, height - lowerBound, width, lowerBound))

def showScore():

    scoreText = font.render("Balloons Bursted : " + str(score), True, white)

    display.blit(scoreText, (150, height - lowerBound + 50))

def close():

    pygame.quit()

    sys.exit()    

def game():

    global score

    loop = True

    while loop:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                close()

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_q:

                    close()

                if event.key == pygame.K_r:

                    score = 0

                    game()

            if event.type == pygame.MOUSEBUTTONDOWN:

                for i in range(noBalloon):

                    balloons[i].burst()

        display.fill(lightBlue)        

        for i in range(noBalloon):

            balloons[i].show()

        pointer()        

        for i in range(noBalloon):

            balloons[i].move()        

        lowerPlatform()

        showScore()

        pygame.display.update()

        clock.tick(60)

game()


Snake game using python program

 #To install pygame package "pip install pygame" in python Terminal

 
import pygame, random, sys
from pygame.locals import *
def collide(x1, x2, y1, y2, w1, w2, h1, h2):
      if x1+w1>x2 and x1<x2+w2 and y1+h1>y2 and y1<y2+h2:return True
      else:return False
def die(screen, score):
      f=pygame.font.SysFont('Arial', 30);t=f.render('Your score was: '+str(score), True, (0, 0, 0));screen.blit(t, (10, 270));pygame.display.update();pygame.time.wait(2000);sys.exit(0)
xs = [290, 290, 290, 290, 290];ys = [290, 270, 250, 230, 210];dirs = 0;score = 0;applepos = (random.randint(0, 590), random.randint(0, 590));pygame.init();s=pygame.display.set_mode((600, 600));pygame.display.set_caption('Snake');appleimage = pygame.Surface((20, 20));appleimage.fill((0, 0, 255));img = pygame.Surface((20, 20));img.fill((255, 0, 0));f = pygame.font.SysFont('Arial', 20);clock = pygame.time.Clock()
while True:
      clock.tick(10)
      for e in pygame.event.get():
            if e.type == QUIT:
                  sys.exit(0)
            elif e.type == KEYDOWN:
                  if e.key == K_UP and dirs != 0:dirs = 2
                  elif e.key == K_DOWN and dirs != 2:dirs = 0
                  elif e.key == K_LEFT and dirs != 1:dirs = 3
                  elif e.key == K_RIGHT and dirs != 3:dirs = 1
      i = len(xs)-1
      while i >= 2:
            if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):die(s, score)
            i-= 1
      if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):score+=1;xs.append(700);ys.append(700);applepos=(random.randint(0,590),random.randint(0,590))
      if xs[0] < 0 or xs[0] > 580 or ys[0] < 0 or ys[0] > 580: die(s, score)
      i = len(xs)-1
      while i >= 1:
            xs[i] = xs[i-1];ys[i] = ys[i-1];i -= 1
      if dirs==0:ys[0] += 20
      elif dirs==1:xs[0] += 20
      elif dirs==2:ys[0] -= 20
      elif dirs==3:xs[0] -= 20     
      s.fill((255, 255, 255))
      for i in range(0, len(xs)):
            s.blit(img, (xs[i], ys[i]))
      s.blit(appleimage, applepos);t=f.render(str(score), True, (0, 0, 0));s.blit(t, (10, 10));pygame.display.update()