Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.0 kB
8
Indexable
Never
import pygame
import random

height = 2200
width = 1000
red = (255,0,0)
blue = (0,0,255)
green =(0, 255, 0)
white = (255,255,255)
yellow = (255,255,0)
fps = 70

class player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(blue)
        self.rect = self.image.get_rect()
        self.rect.centerx = width/2
        self.rect.bottom = height - 10
        
        
    def update(self):
            pos = pygame.mouse.get_pos()
            self.rect.x = pos[0]
            # stop the object from moving out of the edge
            if self.rect.right > width:
                self.rect.right = width
            if self.rect.left < 0:
               self.rect.left = 0
               
    def shoot(self):
      bullet = Bullet(self.rect.centrex,self.rect.top)
      all_sprite.add(bullet)
      bull.add(bullet)
      
 # create an enemy class
class Mob(pygame.sprite.Sprite):
     def __init__(self):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.Surface((50,40))
         self.image.fill(green)
         self.rect = self.image.get_rect()
         self.rect.x = random.randrange(width - self.rect.width)
         self.rect.y = random.randrange(-100,-40)
         self.speedy = random.randrange(2,16)
         self.speedx = random.randrange(-4,7)
         
     def update(self):
             self.rect.y += self.speedy
             self.rect.x += self.speedx
             if self.rect.top > height + 10 or self.rect.left < -10 or self.rect.right > width +10:
                 self.rect.y = random.randrange(-100,-40)
                 self.rect.x = random.randrange(width - self.rect.width)
                 
class Bullet(pygame.sprite.Sprite):
 def __init__(self,x,y):
     pygame.sprite.Sprite.__init__(self)
     self.image = pygame.Surface((10,20))
     self.image.fill(yellow)
     self.rect = self.image.get_rect()
     self.rect.bottom = y
     self.rect.centerx = x
     self.speedy = -10
     
 def update(self):
     self.rect.y += self.speedy
     #kill if it moves off the top
     if self.rect.bottom < 0:
         self.kill()
 # creating a group for all sprite           
all_sprite = pygame.sprite.Group()
mob = pygame.sprite.Group()
bull = pygame.sprite.Group()
#bullet = Bullet(self.rect.centerx,self.rect.top)
b = player()
all_sprite.add(b)
for i in range(8):
    a = Mob()
    mob.add(a)
    all_sprite.add(a)
 # initializing the pygame
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
 
 
# Game loop
while True:
 for ev in pygame.event.get():
     if ev.type == pygame.QUIT:
         break
     elif ev.type == pygame.MOUSEBUTTONDOWN:
         player().shoot()
     
     
 all_sprite.update()
 hits = pygame.sprite.spritecollide(b,mob,False)
 if hits:
     break
 clock.tick(fps)
 screen.fill(red)
 all_sprite.draw(screen)
 
 pygame.display.flip()

pygame.quit