Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import pygame
  2. import sys
  3.  
  4. WIDTH = 800
  5. HEIGHT = 800
  6.  
  7. FPS = 10
  8.  
  9. COLOR_BLACK = (0, 0, 0)
  10. COLOR_RED = (255, 0, 0)
  11.  
  12. WIDTH_STEPS = WIDTH / 40
  13. HEIGHT_STEPS = HEIGHT / 40
  14.  
  15. class Brick(object):
  16. x = 0
  17. y = 0
  18. color = None
  19. def __init__(self, x, y, color):
  20. self.x = x
  21. self.y = y
  22. self.color = color
  23.  
  24. def show(self, pygame, background):
  25. pygame.draw.rect(background,
  26. self.color,
  27. pygame.Rect(self.x * WIDTH_STEPS, self.y * HEIGHT_STEPS, WIDTH_STEPS, HEIGHT_STEPS))
  28.  
  29.  
  30.  
  31. class Snake(object):
  32. x = 5
  33. y = 5
  34. xspeed = 1
  35. yspeed = 0
  36. tail = []
  37. total = 0
  38. pygame = None
  39.  
  40. def __init__(self, pygame):
  41. self.pygame = pygame
  42. self.tail.append(Brick(self.x, self.y, COLOR_RED))
  43. self.total = 1;
  44.  
  45. def update(self):
  46. print len(self.tail)
  47. if (self.total == len(self.tail)):
  48.  
  49. for i in xrange(0, len(self.tail) - 1):
  50. self.tail[i] = self.tail[i+1]
  51. self.tail.append(Brick(self.x, self.y, COLOR_RED))
  52.  
  53.  
  54. self.tail[self.total - 1] = Brick(self.x, self.y, COLOR_RED)
  55. self.x = self.x + self.xspeed
  56. self.y = self.y + self.yspeed
  57.  
  58. #self.total += 1
  59.  
  60. def show(self, background):
  61. for i in self.tail:
  62. i.show(self.pygame, background)
  63.  
  64. def setSpeed(self, xspeed, yspeed):
  65. self.xspeed = xspeed
  66. self.yspeed = yspeed
  67.  
  68.  
  69. def main():
  70.  
  71.  
  72. # Initialize imported pygame modules
  73. pygame.init()
  74.  
  75. # Set the window's caption
  76. pygame.display.set_caption("Snake")
  77.  
  78. clock = pygame.time.Clock()
  79.  
  80. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  81.  
  82. background = pygame.Surface((WIDTH, HEIGHT))
  83. background = background.convert()
  84. background.fill(COLOR_BLACK)
  85.  
  86. # Blit everything to screen
  87. screen.blit(background, (0, 0))
  88.  
  89. # Update the screen
  90. pygame.display.flip()
  91.  
  92. snk = Snake(pygame)
  93. print snk
  94.  
  95. # Main loop
  96. while True:
  97. clock.tick(FPS)
  98.  
  99. # Erase everything drawn at last step by filling the background
  100. # with color black
  101. background.fill(COLOR_BLACK)
  102.  
  103. # Check for Quit event
  104. for event in pygame.event.get():
  105. if event.type == pygame.QUIT:
  106. pygame.quit()
  107. sys.exit()
  108.  
  109. # Check for key presses and update paddles accordingly
  110. keys_pressed = pygame.key.get_pressed()
  111. if keys_pressed[pygame.K_w]:
  112. snk.setSpeed(0, -1)
  113. # Do something
  114. pass
  115. if keys_pressed[pygame.K_s]:
  116. snk.setSpeed(0, 1)
  117. # Do something
  118. pass
  119. if keys_pressed[pygame.K_a]:
  120. snk.setSpeed(-1, 0)
  121. # Do something
  122. pass
  123. if keys_pressed[pygame.K_d]:
  124. snk.setSpeed(1, 0)
  125. # Do something
  126. pass
  127.  
  128. snk.update()
  129. snk.show(background)
  130. # Update game state
  131.  
  132. pygame.display.flip()
  133. screen.blit(background, (0, 0))
  134.  
  135.  
  136. if __name__ == '__main__':
  137. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement