Posted By

AFlyingNunAttack on 03/05/12


Tagged


Versions (?)

Broken Snake Game


 / Published in: Python
 

Snake game, appears to be broken since addition of random module related things.

  1. #! /usr/bin/env python
  2.  
  3. import pygame, random
  4.  
  5. width = 640
  6. height = 400
  7. color = (0, 0, 255)
  8. color1 = (255, 0, 0)
  9. screen = pygame.display.set_mode((width, height))
  10. clock = pygame.time.Clock()
  11. running = True
  12. running1 = True
  13. keyset = ['pygame.K_UP', 'pygame.K_DOWN', 'pygame.K_LEFT', 'pygame.K_RIGHT']
  14. keyset1 = ['pygame.K_w', 'pygame.K_s', 'pygame.K_a', 'pygame.K_d']
  15.  
  16. class Worm:
  17. """ A worm. """
  18.  
  19. def __init__(self, surface, x, y, length, color, keyset):
  20. self.surface = surface
  21. self.x = x
  22. self.y = y
  23. self.length = length
  24. self.dir_x = 0
  25. self.dir_y = -1
  26. self.body = []
  27. self.crashed = False
  28. self.color = color
  29. self.keyset = keyset
  30.  
  31. def key_event(self, event):
  32. """ Handle key events that affect the worm. """
  33. if event.key == eval(self.keyset[0]):
  34. self.dir_x = 0
  35. self.dir_y = -1
  36. elif event.key == eval(self.keyset[1]):
  37. self.dir_x = 0
  38. self.dir_y = 1
  39. elif event.key == eval(self.keyset[2]):
  40. self.dir_x = -1
  41. self.dir_y = 0
  42. elif event.key == eval(self.keyset[3]):
  43. self.dir_x = 1
  44. self.dir_y = 0
  45.  
  46. def move(self):
  47. """ Move the mouse. """
  48. self.x += self.dir_x
  49. self.y += self.dir_y
  50.  
  51. r, g, b, a = self.surface.get_at((self.x, self.y))
  52. if (r, g, b) != (0, 0, 0):
  53. self.crashed = True
  54.  
  55. self.body.insert(0, (self.x, self.y))
  56.  
  57. if len(self.body) > self.length:
  58. self.body.pop()
  59.  
  60. def draw(self):
  61. for x, y in self.body:
  62. self.surface.set_at((x, y), self.color)
  63.  
  64.  
  65. randlsx = []
  66. randlsy = []
  67. ralmx = 0
  68. ralmy = 0
  69. while ralmx < 50:
  70. obx = random.randint(0, width-1)
  71. randlsx.append(obx)
  72. ralmx + 1
  73.  
  74. while ralmy < 50:
  75. oby = random.randint(0, height-1)
  76. randlsy.append(oby)
  77. ralmy + 1
  78.  
  79. print len(randlsx)
  80. # The Worm
  81.  
  82. w = Worm(screen, width/2, height/2, 200, color, keyset)
  83. w1 = Worm(screen, width/2, height/2, 200, color1, keyset1)
  84.  
  85. while running or running1:
  86. screen.fill((0, 0, 0))
  87.  
  88. for i in range(50):
  89. screen.set_at((randlsx[i], randlsy[i]), (255, 255, 255))
  90.  
  91. w.draw()
  92. w1.draw()
  93. w.move()
  94. w1.move()
  95.  
  96. for q in range(20):
  97. screen.set_at((obsx, obsy), (255, 255, 255))
  98.  
  99. if w.crashed or w.x <= 0 or w.x >= width-1 or w.y >= height-1:
  100. w = Worm(screen, width/2, height/2, 0, (0, 0, 0), keyset)
  101. running = False
  102.  
  103. if w1.crashed or w1.x >= width-1 or w1.y <= 0 or w1.y >= height-1:
  104. w1 = Worm(screen, width/2, height/2, 0, (0, 0, 0), keyset1)
  105. running1 = False
  106.  
  107. for event in pygame.event.get():
  108. if event.type == pygame.QUIT:
  109. running = False
  110. elif event.type == pygame.KEYDOWN:
  111. w.key_event(event)
  112. w1.key_event(event)
  113.  
  114. pygame.display.flip()
  115. clock.tick(200)
  116.  
  117. pygame.quit()

Report this snippet  

You need to login to post a comment.