daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest May 17th, 2018 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import random
  3. import time
  4. # Settings
  5. ticks = 60
  6. window_size = 800, 600
  7. player_size = 100, 100
  8. window_name = 'Game'
  9. show_hitboxes = False
  10. debug = False
  11. bullets = []
  12. meteors = []
  13. files = {
  14.     'background':'background.png',
  15.     'player':'player.png',
  16.     'bullet':'bullet.png',
  17.     'meteor':'meteor.png',
  18.     'background_music':'music.mp3',
  19.     'mouse':'mouse.xbm'
  20. }
  21.  
  22. class Player(pygame.sprite.Sprite):
  23.     def __init__(self):
  24.         pygame.sprite.Sprite.__init__(self)
  25.         self.x = 350
  26.         self.y = 500
  27.         self.surface = pygame.image.load(files['player'])
  28.         self.rect = self.surface.get_rect()
  29.  
  30.     def update(self):
  31.         if show_hitboxes: pygame.draw.rect(self.surface, (255,0,0), self.rect, 1)
  32.         self.rect.topleft = (self.x, self.y)
  33.         window.blit(self.surface, (self.x, self.y))
  34.  
  35.     def move(self, x = 0, y = 0):
  36.         self.x += x
  37.         self.y += y
  38.  
  39. class Bullet(pygame.sprite.Sprite):
  40.     def __init__(self, player):
  41.         pygame.sprite.Sprite.__init__(self)
  42.         bullet_group.add(self)
  43.         self.x = player.x + player_size[0] / 2 - 10
  44.         self.y = player.y
  45.         self.surface = pygame.image.load(files['bullet'])
  46.         self.rect = self.surface.get_rect()
  47.         #print(self, 'I got created')
  48.  
  49.     def update(self):
  50.         #print(self.x,self.y)
  51.         if show_hitboxes: pygame.draw.rect(self.surface, (255,0,0), self.rect, 1)
  52.         self.rect.topleft = (self.x, self.y)
  53.         self.y -= 5
  54.         window.blit(self.surface, (self.x, self.y))
  55.         self.rect = self.surface.get_rect()
  56.  
  57. class Meteor(pygame.sprite.Sprite):
  58.     def __init__(self):
  59.         pygame.sprite.Sprite.__init__(self)
  60.         meteor_group.add(self)
  61.         self.x = -100
  62.         self.y = random.randint(0, 200)
  63.         self.surface = pygame.image.load(files['meteor'])
  64.         self.rect = self.surface.get_rect()
  65.         self.speed = random.randint(2,5)
  66.  
  67.     def update(self):
  68.         if show_hitboxes: pygame.draw.rect(self.surface, (255,0,0), self.rect, 1)
  69.         self.rect.topleft = (self.x, self.y)
  70.         window.blit(self.surface, (self.x, self.y))
  71.  
  72.         #self.rect = self.surface.get_rect()
  73.         self.x += self.speed
  74.         self.check_collide()
  75.         #self.surface = pygame.transform.rotate(self.surface, random.random()*360)
  76.         # for bullet in bullets:
  77.         #   self.rect.colliderect(bullet.rect)
  78.  
  79.     def hit(self):
  80.         if debug: print(clock.get_time(), self, 'I\'m hit')
  81.  
  82.     def __del__(self):
  83.         if debug: print(clock.get_time(), self, 'I\'m about to get deleted.')
  84.         meteors.pop(meteors.index(self))
  85.  
  86.     def check_collide(self):
  87.         pass
  88.         #print(clock.get_time(), bool(pygame.sprite.spritecollideany(self, bullet_group)))
  89.  
  90.  
  91. # Program
  92.  
  93. pygame.init()
  94. window = pygame.display.set_mode(window_size)
  95. pygame.display.set_caption(window_name)
  96. background = pygame.image.load(files['background'])
  97. player = Player()
  98. #cursor = pygame.cursors.load_xbm(files['mouse'], files['meteor'])
  99. #pygame.mouse.set_cursor(cursor)
  100. pygame.mixer.music.load(files['background_music'])
  101. pygame.mixer.music.play(-1)
  102. pygame.mixer.music.set_volume(0.25)
  103. clock = pygame.time.Clock()
  104. bullet_group = pygame.sprite.Group()
  105. meteor_group = pygame.sprite.Group()
  106. font = pygame.font.SysFont(pygame.font.get_default_font(), 40)
  107. shooted_frame = 30
  108. shoot_ticker = 0
  109. pygame.time.set_timer(1, 1000)
  110.  
  111. while True:
  112.     shoot_ticker += 1
  113.     #pygame.display.flip()
  114.     pygame.display.update()
  115.     clock.tick(ticks)
  116.     pressed_keys = pygame.key.get_pressed()
  117.     # Controls
  118.     #print(player.x)
  119.     if (pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]) and player.x >= 0:
  120.         if debug: print('Event LEFT')
  121.         player.move(-8)
  122.  
  123.     elif (pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]) and player.x <= window_size[0] - player_size[0]:
  124.         if debug: print('Event RIGHT')
  125.         player.move(8)
  126.  
  127.     if pressed_keys[pygame.K_DELETE]:
  128.         for meteor in meteors:
  129.             if debug: print('Deleting:', meteor)
  130.             #for i in range(0,240,20):
  131.                 #meteor.surface.set_alpha(240-i*2)
  132.                 #print(meteor.surface.get_alpha())
  133.                 #meteor.update()
  134.                 #time.sleep(0.01)
  135.             meteors.pop(meteors.index(meteor))
  136.  
  137.     if pressed_keys[pygame.K_SPACE]:
  138.         #print(clock.get_time())
  139.         if shoot_ticker >= 12:
  140.             bullets.append(Bullet(player))
  141.             shoot_ticker = 0
  142.         #else:
  143.         #   shooted_frame = clock.get_time()
  144.  
  145.     for event in pygame.event.get():
  146.         if event.type == pygame.QUIT:
  147.             if debug: print('Event QUIT')
  148.             quit()
  149.  
  150.         elif event.type == 1:
  151.             meteors.append(Meteor())
  152.  
  153.         #elif event.type == pygame.KDN_SPACE:
  154.         #   print('Event')
  155.  
  156.     window.blit(background, (0,0))
  157.  
  158.     for bullet in bullets:
  159.         bullet.update()
  160.         if bullet.y < -50:
  161.             bullets.pop(bullets.index(bullet))
  162.     for meteor in meteors:
  163.         meteor.update()
  164.     colliding = []
  165.     window.blit(font.render(str(int(clock.get_fps())), True, (255,255,100)), (0,0))
  166.     hits = pygame.sprite.groupcollide(bullet_group, meteor_group, False, False)
  167.     if hits: print(clock.get_time(), hits)
  168.     #if colliding: print(clock.get_time(), colliding)
  169.     #if colliding:
  170.     #   print(colliding)
  171.     player.update()
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