/ Published in: Python
Snake game, appears to be broken since addition of random module related things.
Expand |
Embed | Plain Text
#! /usr/bin/env python import pygame, random width = 640 height = 400 color = (0, 0, 255) color1 = (255, 0, 0) screen = pygame.display.set_mode((width, height)) clock = pygame.time.Clock() running = True running1 = True keyset = ['pygame.K_UP', 'pygame.K_DOWN', 'pygame.K_LEFT', 'pygame.K_RIGHT'] keyset1 = ['pygame.K_w', 'pygame.K_s', 'pygame.K_a', 'pygame.K_d'] class Worm: """ A worm. """ def __init__(self, surface, x, y, length, color, keyset): self.surface = surface self.x = x self.y = y self.length = length self.dir_x = 0 self.dir_y = -1 self.body = [] self.crashed = False self.color = color self.keyset = keyset def key_event(self, event): """ Handle key events that affect the worm. """ if event.key == eval(self.keyset[0]): self.dir_x = 0 self.dir_y = -1 elif event.key == eval(self.keyset[1]): self.dir_x = 0 self.dir_y = 1 elif event.key == eval(self.keyset[2]): self.dir_x = -1 self.dir_y = 0 elif event.key == eval(self.keyset[3]): self.dir_x = 1 self.dir_y = 0 def move(self): """ Move the mouse. """ self.x += self.dir_x self.y += self.dir_y r, g, b, a = self.surface.get_at((self.x, self.y)) if (r, g, b) != (0, 0, 0): self.crashed = True self.body.insert(0, (self.x, self.y)) if len(self.body) > self.length: self.body.pop() def draw(self): for x, y in self.body: self.surface.set_at((x, y), self.color) randlsx = [] randlsy = [] ralmx = 0 ralmy = 0 while ralmx < 50: obx = random.randint(0, width-1) randlsx.append(obx) ralmx + 1 while ralmy < 50: oby = random.randint(0, height-1) randlsy.append(oby) ralmy + 1 print len(randlsx) # The Worm w = Worm(screen, width/2, height/2, 200, color, keyset) w1 = Worm(screen, width/2, height/2, 200, color1, keyset1) while running or running1: screen.fill((0, 0, 0)) for i in range(50): screen.set_at((randlsx[i], randlsy[i]), (255, 255, 255)) w.draw() w1.draw() w.move() w1.move() for q in range(20): screen.set_at((obsx, obsy), (255, 255, 255)) if w.crashed or w.x <= 0 or w.x >= width-1 or w.y >= height-1: w = Worm(screen, width/2, height/2, 0, (0, 0, 0), keyset) running = False if w1.crashed or w1.x >= width-1 or w1.y <= 0 or w1.y >= height-1: w1 = Worm(screen, width/2, height/2, 0, (0, 0, 0), keyset1) running1 = False for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: w.key_event(event) w1.key_event(event) pygame.display.flip() clock.tick(200) pygame.quit()
You need to login to post a comment.
