Advertisement
polectron

Untitled

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