polectron

Untitled

May 28th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import pygame
  2.  
  3. class NPC:
  4. def __init__(self, x, y, dx, dy):
  5. self.x = x
  6. self.y = y
  7. self.dx = dx
  8. self.dy = dy
  9. self.bounces = 0
  10. self.rect = pygame.Rect(0, 0, 0, 0)
  11.  
  12. def update(self, player):
  13. self.x += self.dx * (self.bounces+1)
  14. self.y += self.dy * (self.bounces+1)
  15.  
  16. if self.rect.colliderect(player.rect):
  17. self.dy = -self.dy
  18. self.dx = -self.dx
  19.  
  20. if self.y > 400 or self.y < 0:
  21. self.dy = -self.dy
  22. self.bounces += 1
  23.  
  24. if self.x > 600 or self.x < 0:
  25. self.dx = -self.dx
  26. self.bounces += 1
  27.  
  28. if self.bounces > 10:
  29. self.bounces = 0
  30.  
  31. def draw(self, surface):
  32. self.rect = pygame.draw.rect(surface, (255,0,0), (self.x, self.y, 20, 20))
  33.  
  34. #=================================================
  35.  
  36. class Paddle:
  37. def __init__(self, x,y, speed, window):
  38. self.x = x
  39. self.y = y
  40. self.speed = speed
  41. self.window = window
  42. self.right = False
  43. self.left = False
  44. self.points = 0
  45. self.rect = pygame.Rect(0, 0, 0, 0)
  46.  
  47. def draw(self, surface):
  48. self.rect = pygame.draw.rect(surface, (0,0,255), (self.x, self.y, 100, 20))
  49.  
  50. def update(self):
  51. if self.right and self.x < self.window[0] - 100:
  52. self.x += self.speed
  53. elif self.left and self.x > 0:
  54. self.x -= self.speed
  55.  
  56.  
  57. #===============================================
  58.  
  59. def main():
  60. pygame.init()
  61. FPSCLOCK = pygame.time.Clock()
  62. DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
  63. pygame.display.set_caption('Juego 1')
  64.  
  65. BFONT = pygame.font.Font('freesansbold.ttf', 24)
  66. SFONT = pygame.font.Font('freesansbold.ttf', 12)
  67.  
  68. npcs = [NPC(0, 0, 5, 10), NPC(20, 0, 5, 10), NPC(0, 20, 1, 10), NPC(190, 0, 8, 2)]
  69. player = Paddle(300, 380, 15, (WINWIDTH, WINHEIGHT))
  70.  
  71. while True:
  72. DISPLAYSURF.fill(WHITE)
  73.  
  74. player.update()
  75. player.draw(DISPLAYSURF)
  76.  
  77. for npc in npcs:
  78. npc.draw(DISPLAYSURF)
  79. npc.update(player)
  80.  
  81. for event in pygame.event.get(): # event handling loop
  82. if event.type == QUIT:
  83. terminate()
  84. elif event.type == KEYDOWN:
  85. if event.key == K_RIGHT:
  86. player.right = True
  87. elif event.key == K_LEFT:
  88. player.left = True
  89. elif event.type == KEYUP:
  90. if event.key == K_RIGHT:
  91. player.right = False
  92. elif event.key == K_LEFT:
  93. player.left = False
  94.  
  95. pygame.display.update()
  96. FPSCLOCK.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment