Advertisement
Guest User

Untitled

a guest
Jan 7th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import sys
  2. import pygame
  3.  
  4. pygame.init()
  5.  
  6. screen_width = 640
  7. screen_height = 480
  8. screen = pygame.display.set_mode((screen_width, screen_height))
  9. screen_rect = screen.get_rect()
  10.  
  11. clock = pygame.time.Clock()
  12.  
  13. fps = 30
  14.  
  15.  
  16. class Character(object):
  17.     def __init__(self, surface, accel, gravity):
  18.         self.surface = surface
  19.         self.accel = accel
  20.         self.gravity = gravity
  21.         self.vel = (0, 0)
  22.         self.pos = ((screen_width / 2),(screen_height / 2))
  23.         self.size = (10, 10)
  24.  
  25.     def move_right(self):
  26.         self.vel = (self.vel[0] + self.accel, self.vel[1])
  27.  
  28.     def move_left(self):
  29.         self.vel = (self.vel[0] - self.accel, self.vel[1])
  30.  
  31.     def move_up(self):
  32.         self.vel = (self.vel[0], self.vel[1] - self.accel)
  33.  
  34.  
  35.     def move_down(self):
  36.         self.vel = (self.vel[0], self.vel[1] + self.accel)
  37.  
  38.     def move(self):
  39.         keys = pygame.key.get_pressed()
  40.         if keys[pygame.K_w]:
  41.             self.move_up()
  42.         if keys[pygame.K_a]:
  43.             self.move_left()
  44.         if keys[pygame.K_s]:
  45.             self.move_down()
  46.         if keys[pygame.K_d]:
  47.             self.move_right()
  48.  
  49.  
  50.     def display(self):
  51.         pygame.draw.rect(self.surface, (255, 255, 255), self.character)
  52.  
  53.     def reset(self):
  54.         (x_pos, y_pos) = pygame.mouse.get_pos()
  55.         self.pos = (x_pos,  y_pos)
  56.         self.x_vel = 0
  57.         self.y_vel = 0
  58.  
  59.     def time_step(self):
  60.         # In here is everything that makes the scene advance by a timestep
  61.         self.timestep = 1./fps # or something else to your liking
  62.         self.damping = 0.98 # makes your character slow down when you don't press movment buttons
  63.         self.vel = (self.damping * self.vel[0], self.damping * self.vel[1] + self.gravity * self.timestep)
  64.         self.pos = (self.pos[0] + self.vel[0] * self.timestep, self.pos[1] + self.vel[1] * self.timestep + 0.5* self.gravity * self.timestep**2)
  65.  
  66.         if self.pos[0] <= 0 or self.pos[0] >= screen_width:
  67.             self.vel = (self.vel[0] * -1, self.vel[1])
  68.  
  69.         if self.pos[1] <= 0 or self.pos[1] >= screen_height:
  70.             self.vel = (self.vel[0], self.vel[1] * -1)
  71.  
  72.         self.character = pygame.Rect((self.pos[0], self.pos[1]), self.size)
  73.         self.character.clamp_ip(screen_rect)
  74.  
  75. def main():
  76.     player1 = Character(screen, 10, 50)
  77.     while True:
  78.         for event in pygame.event.get():
  79.                 if event.type == pygame.QUIT:
  80.                     pygame.quit()
  81.                     sys.exit()
  82.                 elif event.type == pygame.MOUSEBUTTONDOWN:
  83.                     player1.reset()
  84.  
  85.         player1.time_step()
  86.         player1.move()
  87.  
  88.         screen.fill((0, 0, 0))
  89.  
  90.         player1.display()
  91.  
  92.         pygame.display.update(screen_rect)
  93.         clock.tick(fps)
  94.  
  95. if __name__ == "__main__":
  96.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement