san560

Untitled

Jan 3rd, 2018
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #this is a platformer
  2. import pygame as pg
  3. from settings import *
  4.  
  5. vec = pg.math.Vector2
  6.  
  7. #initializes pygame
  8. pg.init()
  9. pg.mixer.init()
  10. screen = pg.display.set_mode((WIDTH,HEIGHT))
  11. pg.display.set_caption("My Game")
  12. clock = pg.time.Clock()
  13. #makes the sprites
  14. class Player(pg.sprite.Sprite):
  15. #sprite for the PLAYER
  16. def __init__(self):
  17. pg.sprite.Sprite.__init__(self)
  18. self.image = pg.Surface((30,40))
  19. self.image.fill(YELLOW)
  20. self.rect = self.image.get_rect()
  21. self.rect.center = (WIDTH / 2, HEIGHT - 30)
  22. self.pos = vec(WIDTH / 2, HEIGHT / 2)
  23. self.vel = vec(0,0)
  24. self.acc = vec(0,0)
  25.  
  26. def jump(self):
  27. self.rect.x += 1
  28. hits = pg.sprite.spritecollide(self, platforms, False)
  29. self.rect.x -= 1
  30. if hits:
  31. self.vel.y = -13
  32.  
  33. def update(self):
  34. self.acc = vec(0, 0.5)
  35. keys = pg.key.get_pressed()
  36. if keys[pg.K_a]:
  37. self.acc.x = -PLAYER_ACC
  38. if keys[pg.K_d]:
  39. self.acc.x = PLAYER_ACC
  40.  
  41.  
  42.  
  43. # apply friction
  44. self.acc.x += self.vel.x * PLAYER_FRICTION
  45.  
  46.  
  47. # equations of motion
  48. self.vel += self.acc
  49. self.pos += self.vel + 0.5 * self.acc
  50. # wrap around the sides of the screen
  51. if self.pos.x > WIDTH:
  52. self.pos.x = 0
  53. if self.pos.x < 0:
  54. self.pos.x = WIDTH
  55.  
  56.  
  57. self.rect.midbottom = self.pos
  58. # makes the platform sprite
  59. class Platform(pg.sprite.Sprite):
  60. def __init__(self, x, y, w, h):
  61. pg.sprite.Sprite.__init__(self)
  62. self.image = pg.Surface((w, h))
  63. self.image.fill(GREEN)
  64. self.rect = self.image.get_rect()
  65. self.rect.x = x
  66. self.rect.y = y
  67.  
  68. class Mob(pg.sprite.Sprite):
  69. def __init__(self):
  70. pg.sprite.Sprite.__init__(self)
  71. self.image = pg.Surface((50,50))
  72. self.image.fill(RED)
  73. self.rect = self.image.get_rect()
  74. self.rect.x = WIDTH / 2
  75. self.rect.y = 100
  76. def update(self):
  77. self.rect.y += 5
  78.  
  79. # makes the group to add sprites in group
  80. all_sprites = pg.sprite.Group()
  81. platforms = pg.sprite.Group()
  82. mobs = pg.sprite.Group()
  83.  
  84. #adds the sprites to group and spawns the sprites
  85. player = Player()
  86. all_sprites.add(player)
  87. mob = Mob()
  88. all_sprites.add(mob)
  89. mobs.add(mob)
  90. # spawns and adds platforms to group
  91. p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
  92. all_sprites.add(p1)
  93. platforms.add(p1)
  94. p2 = Platform(WIDTH / 2 - 50, HEIGHT - 300, 100, 20)
  95. all_sprites.add(p2)
  96. platforms.add(p2)
  97. p3 = Platform(WIDTH / 2 - 100, HEIGHT - 150, 200, 20)
  98. all_sprites.add(p3)
  99. platforms.add(p3)
  100.  
  101.  
  102. #Game loop
  103. running = True
  104. while running:
  105. #keep loop running at the right speed
  106. clock.tick(FPS)
  107. #proccess input(events)
  108. for event in pg.event.get():
  109. #check for closing the window
  110. if event.type == pg.QUIT:
  111. running = False
  112. if event.type == pg.KEYDOWN:
  113. if event.key == pg.K_SPACE:
  114. player.jump()
  115.  
  116.  
  117. #Update
  118. all_sprites.update()
  119. # check if the player hits the platform - only if falling
  120. if player.vel.y > 0:
  121. hits = pg.sprite.spritecollide(player, platforms, False)
  122. if hits:
  123. if player.rect.top == hits[0].rect.bottom:
  124. player.vel.y = 10
  125. else:
  126. player.pos.y = hits[0].rect.top + 1
  127. player.vel.y = 0
  128.  
  129.  
  130. #Draw / render
  131. screen.fill(BLACK)
  132. all_sprites.draw(screen)
  133. #after drawing everything_flip the display
  134. pg.display.flip()
  135.  
  136. pg.quit()
Add Comment
Please, Sign In to add comment