Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys
- pygame.init()
- sc = pygame.display.set_mode((600,400))
- clock = pygame.time.Clock()
- FPS = 60
- backCol = (50,30,80)
- class Hero:
- def __init__(self,surf,color = (80,90,30)):
- self.w, self.h = 15,15
- self.x, self.y = 300,350
- self.speed = 4
- self.motion = "STOP"
- self.shot = False
- self.surf = surf
- self.color = color
- def left(self):
- self.x-=self.speed
- def right(self):
- self.x+=self.speed
- def draw(self):
- pygame.draw.rect(self.surf,self.color,(self.x,self.y,self.w,self.h))
- class Bullet:
- def __init__(self,surf,x,y):
- self.x = x
- self.y = y
- self.speed = 6
- self.surf = surf
- self.color = (166,146,148)
- def fly(self):
- self.y-=self.speed
- def draw(self):
- self.form = [(self.x,self.y),(self.x-5,self.y+10),(self.x+5,self.y+10)]
- pygame.draw.polygon(self.surf,self.color,self.form)
- hero = Hero(sc)
- bullet = Bullet(sc,hero.x,hero.y)
- while True:
- sc.fill(backCol)
- hero.draw()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- hero.motion = "LEFT"
- elif event.key == pygame.K_RIGHT:
- hero.motion = "RIGHT"
- if event.key == pygame.K_SPACE:
- hero.shot = True
- bullet.x = hero.x
- bullet.y = hero.y
- elif event.type == pygame.KEYUP:
- if event.key in [pygame.K_LEFT,pygame.K_RIGHT]:
- hero.motion = "STOP"
- if hero.motion == "LEFT":
- hero.left()
- elif hero.motion == "RIGHT":
- hero.right()
- if hero.shot:
- print(hero.shot)
- bullet.draw()
- bullet.fly()
- if bullet.y < 0:
- hero.shot = False
- clock.tick(FPS)
- pygame.display.update()
- pygame.display.set_caption('FPS: %s'%(str(clock).replace('<Clock(fps=','').replace(')>','')))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement