SHARE
TWEET

Untitled

a guest Jan 25th, 2020 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame, sys
  2.  
  3. pygame.init()
  4.  
  5. sc = pygame.display.set_mode((600,400))
  6. clock = pygame.time.Clock()
  7. FPS = 60
  8. backCol = (50,30,80)
  9.  
  10. class Hero:
  11.  
  12.     def __init__(self,surf,color = (80,90,30)):
  13.         self.w, self.h = 15,15
  14.         self.x, self.y = 300,350
  15.         self.speed = 4
  16.         self.motion = "STOP"
  17.         self.shot = False
  18.         self.surf = surf
  19.         self.color = color
  20.  
  21.     def left(self):
  22.         self.x-=self.speed
  23.  
  24.     def right(self):
  25.         self.x+=self.speed
  26.  
  27.     def draw(self):
  28.         pygame.draw.rect(self.surf,self.color,(self.x,self.y,self.w,self.h))
  29.  
  30. class Bullet:
  31.     def __init__(self,surf,x,y):
  32.         self.x = x
  33.         self.y = y
  34.         self.speed = 6
  35.         self.surf = surf
  36.         self.color = (166,146,148)
  37.  
  38.     def fly(self):
  39.         self.y-=self.speed
  40.  
  41.     def draw(self):
  42.         self.form = [(self.x,self.y),(self.x-5,self.y+10),(self.x+5,self.y+10)]
  43.         pygame.draw.polygon(self.surf,self.color,self.form)
  44.        
  45. hero = Hero(sc)
  46. bullet = Bullet(sc,hero.x,hero.y)
  47.    
  48. while True:
  49.     sc.fill(backCol)
  50.     hero.draw()
  51.     for event in pygame.event.get():
  52.         if event.type == pygame.QUIT:
  53.             pygame.quit()
  54.             sys.exit()
  55.         elif event.type == pygame.KEYDOWN:
  56.             if event.key == pygame.K_LEFT:
  57.                 hero.motion = "LEFT"
  58.             elif event.key == pygame.K_RIGHT:
  59.                 hero.motion = "RIGHT"
  60.             if event.key == pygame.K_SPACE:
  61.                 hero.shot = True
  62.                 bullet.x = hero.x
  63.                 bullet.y = hero.y
  64.  
  65.         elif event.type == pygame.KEYUP:
  66.             if event.key in [pygame.K_LEFT,pygame.K_RIGHT]:
  67.                 hero.motion = "STOP"
  68.  
  69.     if hero.motion == "LEFT":
  70.         hero.left()
  71.     elif hero.motion == "RIGHT":
  72.         hero.right()
  73.     if hero.shot:
  74.         print(hero.shot)
  75.         bullet.draw()
  76.         bullet.fly()
  77.         if bullet.y < 0:
  78.             hero.shot = False
  79.  
  80.     clock.tick(FPS)
  81.     pygame.display.update()
  82.     pygame.display.set_caption('FPS: %s'%(str(clock).replace('<Clock(fps=','').replace(')>','')))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top