Advertisement
Guest User

Untitled

a guest
Jan 7th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 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.         self.pos = (self.pos[0] + self.vel[0],self.pos[1])
  28.  
  29.     def move_left(self):
  30.         self.vel = (self.vel[0] - self.accel, self.vel[1])
  31.         self.pos = (self.pos[0] + self.vel[0],self.pos[1])
  32.  
  33.     def move_up(self):
  34.         self.vel = (self.vel[0], self.vel[1] - self.accel)
  35.         self.pos = (self.pos[0],self.pos[1] + self.vel[1])
  36.  
  37.     def move_down(self):
  38.         self.vel = (self.vel[0], self.vel[1] + self.accel)
  39.         self.pos = (self.pos[0],self.pos[1] + self.vel[1])
  40.  
  41.     def move(self):
  42.         keys = pygame.key.get_pressed()
  43.         if keys[pygame.K_w]:
  44.             self.move_up()
  45.         if keys[pygame.K_a]:
  46.             self.move_left()
  47.         if keys[pygame.K_s]:
  48.             self.move_down()
  49.         if keys[pygame.K_d]:
  50.             self.move_right()
  51.  
  52.         if self.pos[0] <= 0 or self.pos[0] >= screen_width:
  53.             self.vel = (self.vel[0] * -1, self.vel[1])
  54.  
  55.         if self.pos[1] <= 0 or self.pos[1] >= screen_height:
  56.             self.vel = (self.vel[0], self.vel[1] * -1)
  57.  
  58.         self.character = pygame.Rect((self.pos[0], self.pos[1]), self.size)
  59.         self.character.clamp_ip(screen_rect)
  60.  
  61.     def display(self):
  62.         pygame.draw.rect(self.surface, (255, 255, 255), self.character)
  63.  
  64.     def reset(self):
  65.         (x_pos, y_pos) = pygame.mouse.get_pos()
  66.         self.pos = (x_pos, self.pos[1])
  67.         self.pos = (self.pos[0], y_pos)
  68.         self.x_vel = 0
  69.         self.y_vel = 0
  70.  
  71.     def apply_gravity(self):
  72.         self.timestep = 1/fps # or something else to your liking
  73.         self.vel = (self.vel[0], self.vel[1] + self.gravity * self.timestep)
  74.         self.pos = (self.pos[0], self.pos[1] + self.vel[1] * self.timestep + 0.5* self.gravity * self.timestep**2)
  75.  
  76. def main():
  77.     player1 = Character(screen, 10, .5)
  78.     while True:
  79.         for event in pygame.event.get():
  80.                 if event.type == pygame.QUIT:
  81.                     pygame.quit()
  82.                     sys.exit()
  83.                 elif event.type == pygame.MOUSEBUTTONDOWN:
  84.                     player1.reset()
  85.  
  86.         player1.apply_gravity()
  87.         player1.move()
  88.  
  89.         screen.fill((0, 0, 0))
  90.  
  91.         player1.display()
  92.  
  93.         pygame.display.update(screen_rect)
  94.         clock.tick(fps)
  95.  
  96. if __name__ == "__main__":
  97.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement