fork(1) download
  1. import pygame,sys,threading,math
  2. from pygame.locals import *
  3. from rgb_color import *
  4. pygame.init()
  5. pygame.init()
  6.  
  7. class MainWindow:
  8.  
  9. def __init__ (self, x,y, color):
  10.  
  11. self.screen = pygame.display.set_mode((x,y))
  12. self.running = True
  13. self.color = color
  14. self.clock = pygame.time.Clock()
  15.  
  16. def run (self):
  17.  
  18. screen = self.screen; blit = screen.blit
  19.  
  20. x = 0
  21. while self.running:
  22. for event in pygame.event.get():
  23. if event.type == MOUSEBUTTONDOWN:
  24. if pygame.mouse.get_pressed()[0]:
  25. dude.following = True
  26. if event.type == MOUSEBUTTONUP:
  27. if not pygame.mouse.get_pressed()[0]:
  28. dude.following = False
  29. if event.type == KEYDOWN:
  30. if event.key == K_RIGHT: dude.moving = "right"
  31. if event.key == K_LEFT: dude.moving = "left"
  32. if event.key == K_DOWN: dude.moving = "down"
  33. if event.key == K_UP: dude.moving = "up"
  34. if event.type == KEYUP:
  35. if event.key == K_RIGHT: dude.moving = False
  36. if event.key == K_LEFT: dude.moving = False
  37. if event.key == K_DOWN: dude.moving = False
  38. if event.key == K_UP: dude.moving = False
  39.  
  40. if event.type == QUIT:
  41. self.running = False
  42. pygame.quit(); sys.exit(); break
  43.  
  44. self.gameActions()
  45. self.updateSurface(screen, blit)
  46.  
  47. def gameActions (self):
  48. """ This is where to place all major game functions. """
  49.  
  50. self.time_passed = self.clock.tick(40)/1000.0
  51. if dude.moving: dude.move()
  52. if dude.following: dude.follow()
  53.  
  54. def updateSurface (self, screen, blit):
  55. screen.fill(self.color)
  56. blit(dude.img, (dude.xPos,dude.yPos))
  57. pygame.display.update()
  58.  
  59. class Dude:
  60. def __init__ (self, speed):
  61. self.img = pygame.image.load("littleman.png").convert_alpha()
  62. self.xPos = 375 # TODO: Get w/h and center dynamically
  63. self.yPos = 275 #
  64. self.speed = speed # Pixels/second
  65.  
  66. self.moving = False
  67. self.following = False
  68.  
  69. def move (self):
  70. pixels = self.getSpeed()
  71. if self.moving == "right": self.xPos += pixels
  72. if self.moving == "left": self.xPos -= pixels
  73. if self.moving == "up": self.yPos -= pixels
  74. if self.moving == "down": self.yPos += pixels
  75.  
  76. def follow (self):
  77. mouse_pos = pygame.mouse.get_pos()
  78. diff = (self.xPos-mouse_pos[0], self.yPos-mouse_pos[1])
  79. vector = math.sqrt(diff[0]**2 + diff[1]**2)
  80. distance = (self.xPos/vector, self.yPos/vector)
  81.  
  82. if (self.xPos, self.yPos) == mouse_pos:
  83. return
  84. if mouse_pos[0] >= self.xPos:
  85. self.xPos += distance[0]
  86. else:
  87. self.xPos -= distance[0]
  88. if mouse_pos[1] >= self.yPos:
  89. self.yPos += distance[1]
  90. else:
  91. self.yPos -= distance[1]
  92.  
  93. def getSpeed (self):
  94. """ Returns: (float) pixels to move for current frame. """
  95. return (game.time_passed * self.speed)
  96.  
  97. if __name__ == "__main__":
  98. game = MainWindow(800,600,WHITE)
  99. dude = Dude(100)
  100. game.run()
Runtime error #stdin #stdout 0.09s 10872KB
stdin
Standard input is empty
stdout
Standard output is empty