SHARE
TWEET

Untitled

a guest Dec 23rd, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import time
  3.  
  4. pygame.init()
  5.  
  6. class player(pygame.sprite.Sprite):
  7.     def __init__(self,image,x,y,firerate):
  8.         self.x = x
  9.         self.y = y
  10.         self.image = pygame.image.load(image)
  11.         self.iWidth = self.image.get_rect().width
  12.         self.iHeight = self.image.get_rect().height
  13.         self.rect = pygame.Rect(self.x, self.y,
  14.          self.iWidth, self.iHeight)
  15.         self.firerate = firerate
  16.  
  17.     def getRect(self):
  18.         self.rect = pygame.Rect(self.x, self.y,
  19.          self.iWidth, self.iHeight)
  20.  
  21.         return self.rect
  22.  
  23.     def render(self):
  24.         window.blit(self.image,(self.x,self.y))
  25.  
  26.     def is_collided_with(self,sprite1,sprite2):
  27.         return pygame.sprite.collide_rect(sprite1,sprite2)
  28.  
  29.     def movement(self):
  30.         keys = pygame.key.get_pressed()
  31.         if keys[pygame.K_w] and self.y !=0:
  32.             player.y -=vel
  33.         if keys[pygame.K_a] and self.x != 0:
  34.             player.x -=vel
  35.         if keys[pygame.K_s] and self.y != 540:
  36.             player.y+=vel
  37.         if keys[pygame.K_d] and self.x != 660:
  38.             player.x +=vel
  39.  
  40.  
  41.     def shoot(self):
  42.         shot = obj("shot.png",self.x+35,self.y+33)
  43.         t = (time.time()-(1/self.firerate))
  44.         keys = pygame.key.get_pressed()
  45.         if keys[pygame.K_x] and time.time()-t>=(1/self.firerate):
  46.             while shot.x < 720:
  47.                 shot.render()
  48.                 shot.x+=8
  49.                 pygame.time.delay(int(1000/60))
  50.                 pygame.display.update()
  51.             t = time.time()
  52.  
  53. class obj(pygame.sprite.Sprite):
  54.     def __init__(self,image,x,y):
  55.         self.x = x
  56.         self.y = y
  57.         self.image = pygame.image.load(image)
  58.         self.iWidth = self.image.get_rect().width
  59.         self.iHeight = self.image.get_rect().height
  60.         self.rect = pygame.Rect(self.x, self.y,
  61.          self.iWidth, self.iHeight)
  62.        
  63.     def render(self):
  64.         window.blit(self.image,(self.x,self.y))
  65.  
  66.     def collison_check(self,sprite1):
  67.         return self.rect.colliderect(sprite1.getRect())
  68.  
  69. window = pygame.display.set_mode((720,600))
  70. pygame.display.set_caption("GAME")
  71.  
  72. x = 50
  73. y = 50
  74. vel = 5
  75.  
  76. FPS = 60
  77.  
  78. white = (255,255,255)
  79. black= (0,0,0)
  80.  
  81. player = player("slime.png", 50,50,2)
  82. death = obj("death.png", 300, 300)
  83.  
  84. run = True
  85. while run:
  86.     pygame.time.delay(int(1000/FPS))
  87.     for event in pygame.event.get():
  88.         if event.type == pygame.QUIT:
  89.             run = False
  90.     window.fill(white)
  91.     player.render()
  92.     death.render()
  93.     player.movement()
  94.     player.shoot()
  95.     if death.collison_check(player):
  96.         #print("dead")
  97.         print(death.rect, player.rect)
  98.     pygame.display.update()
  99.  
  100.  
  101. pygame.quit()
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