san560

myine

May 9th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. import pygame as pg
  2. from pygame.math import Vector2 as vec
  3. import random
  4. import time
  5. pg.init()
  6. WIDTH, HEIGHT, = 1000, 600
  7. YELLOW = pg.Color('yellow')
  8. GREEN = pg.Color('green')
  9. GRAY = pg.Color('gray11')
  10. RED = pg.Color(255,0,0,0)
  11. screen = pg.display.set_mode((WIDTH,HEIGHT))
  12. clock = pg.time.Clock()
  13. FPS = 60
  14. PLAYER_FRICTION = .95
  15. PLAYER_ACC = .2
  16.  
  17.  
  18. class Player(pg.sprite.Sprite):
  19.  
  20. def __init__(self):
  21. pg.sprite.Sprite.__init__(self)
  22. self.image = pg.Surface((30, 40))
  23. self.image.fill(YELLOW)
  24. self.rect = self.image.get_rect(center=(WIDTH/2, HEIGHT))
  25. self.pos = vec(WIDTH/2, HEIGHT/2)
  26. self.vel = vec(0,0)
  27. self.acc = vec(0,0)
  28.  
  29. def jump(self):
  30. self.rect.y += 1
  31. hits = pg.sprite.spritecollide(self, platforms, False)
  32. self.rect.y -= 1
  33. if hits:
  34. self.vel.y = -13
  35.  
  36. def update(self):
  37. self.acc = vec(0, 0.5)
  38. keys = pg.key.get_pressed()
  39. if keys[pg.K_a]:
  40. self.acc.x = -PLAYER_ACC
  41. if keys[pg.K_d]:
  42. self.acc.x = PLAYER_ACC
  43.  
  44. # apply friction
  45. self.vel.x *= PLAYER_FRICTION
  46. self.vel += self.acc
  47. self.pos += self.vel
  48. # wrap around the sides of the screen
  49. if self.pos.x > WIDTH:
  50. self.pos.x = 0
  51. if self.pos.x < 0:
  52. self.pos.x = WIDTH
  53.  
  54. self.rect.midbottom = self.pos
  55.  
  56.  
  57. class Platform(pg.sprite.Sprite):
  58. def __init__(self, x, y, w, h):
  59. pg.sprite.Sprite.__init__(self)
  60. self.image = pg.Surface((w, h))
  61. self.image.fill(GREEN)
  62. self.rect = self.image.get_rect(topleft=(x, y))
  63.  
  64. class Mob(pg.sprite.Sprite):
  65. def __init__(self):
  66. pg.sprite.Sprite.__init__(self)
  67. self.image = pg.Surface((40,40))
  68. self.image.fill(RED)
  69. self.rect = self.image.get_rect()
  70. self.rect.x = random.randrange(WIDTH - self.rect.width)
  71. self.rect.y = random.randrange(-100, -40)
  72. self.speedy = random.randrange(1, 8)
  73. def update(self):
  74. self.rect.y += self.speedy
  75.  
  76. all_sprites = pg.sprite.Group()
  77. platforms = pg.sprite.Group()
  78. MOBS = pg.sprite.Group()
  79. # spawns the sprites ands adds them to the group
  80. player = Player()
  81. all_sprites.add(player)
  82. # mobs = Mob()
  83. #all_sprites.add(mobs)
  84. #MOBS.add(mobs)
  85. #time.sleep(40)
  86.  
  87. # spawns and adds platforms to group
  88. p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
  89. p2 = Platform(WIDTH / 2 - 50, HEIGHT - 300, 100, 20)
  90. p3 = Platform(WIDTH / 2 - 100, HEIGHT - 150, 200, 20)
  91. all_sprites.add(p1, p2, p3)
  92. platforms.add(p1, p2, p3)
  93.  
  94. running = True
  95. while running:
  96. clock.tick(FPS)
  97. for event in pg.event.get():
  98. if event.type == pg.QUIT:
  99. running = False
  100. if event.type == pg.KEYDOWN:
  101. if event.key == pg.K_SPACE:
  102. player.jump()
  103.  
  104. all_sprites.update()
  105. # Check if we hit a wall/platform.
  106. hits = pg.sprite.spritecollide(player, platforms, False)
  107. for platform in hits: # Iterate over the collided platforms.
  108. if player.vel.y > 0: # We're falling.
  109. player.rect.bottom = platform.rect.top
  110. player.vel.y = 0
  111. elif player.vel.y < 0: # We're jumping.
  112. player.rect.top = platform.rect.bottom
  113. player.vel.y = 3
  114.  
  115. player.pos.y = player.rect.bottom
  116.  
  117. #Draw / render
  118. screen.fill(GRAY)
  119. all_sprites.draw(screen)
  120. pg.display.flip()
  121.  
  122.  
  123. pg.quit()
Advertisement
Add Comment
Please, Sign In to add comment