Guest User

Untitled

a guest
Aug 20th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3.  
  4. WIDTH = 800
  5. HEIGHT = 600
  6.  
  7. SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
  8.  
  9. BLACK = (0, 0, 0)
  10. WHITE = (255, 255, 255)
  11.  
  12.  
  13. class player_class:
  14. def __init__(self, x, y, size, color):
  15. self.rect = pygame.Rect(x, y, size, size)
  16. self.color = color
  17. self.changeX = 0
  18. self.changeY = 0
  19.  
  20. def update(self):
  21. self.rect.x += self.changeX
  22. self.rect.y += self.changeY
  23.  
  24. # Friction
  25. self.changeY = self.changeY * 0.85
  26. self.changeX = self.changeX * 0.85
  27.  
  28. def draw(self, SCREEN):
  29. pygame.draw.rect(SCREEN, self.color, self.rect)
  30.  
  31. def up(self):
  32. self.changeY -= 1
  33.  
  34. def down(self):
  35. self.changeY += 1
  36.  
  37. def right(self):
  38. self.changeX += 1
  39.  
  40. def left(self):
  41. self.changeX -= 1
  42.  
  43.  
  44. player = player_class(400, HEIGHT - 100, 25, WHITE)
  45.  
  46. clock = pygame.time.Clock()
  47. playing = True
  48. while playing:
  49. for event in pygame.event.get():
  50. if event.type == pygame.QUIT:
  51. playing = False
  52. # Handle Keyboard inputs
  53. keys = pygame.key.get_pressed()
  54. if keys[pygame.K_LEFT]:
  55. player.left()
  56. if keys[pygame.K_RIGHT]:
  57. player.right()
  58. if keys[pygame.K_UP]:
  59. player.up()
  60. if keys[pygame.K_DOWN]:
  61. player.down()
  62.  
  63. # Update sprites
  64. player.update()
  65.  
  66. SCREEN.fill(BLACK)
  67. player.draw(SCREEN)
  68. pygame.display.update()
  69. clock.tick(60)
  70. pygame.quit()
  71.  
  72. # We now have a functioning player
  73. # Make an enemy class and make enemies spawn
  74. # from the top of the screen with random
  75. # speed and size
  76. # Challenge: make the player die when they touch
  77. # an enemy
  78.  
Advertisement
Add Comment
Please, Sign In to add comment