Commit bc75c478 authored by Julian Rudolf's avatar Julian Rudolf
Browse files

added buttons to toggle shield, user can now control snake despite shield but...

added buttons to toggle shield, user can now control snake despite shield but it gets overwritten if input would lead to crash
parent 0ab94a3b
......@@ -281,10 +281,18 @@ class Snake:
if prob == 0.0:
allowed_dirs.append(dir)
if len(allowed_dirs) > 0:
dir = random.choice(allowed_dirs)
else:
dir = smallest_prob[1]
correct_input = False
for dirs in allowed_dirs:
if self.angle == dirs:
dir = dirs
correct_input = True
break
if not correct_input:
if len(allowed_dirs) > 0:
dir = random.choice(allowed_dirs)
else:
dir = smallest_prob[1]
self.turn(dir)
self.angle = dir
......@@ -390,6 +398,7 @@ class Snake:
self.process.start()
#print("normal_print ps: nodes " + str(self.shield_snake.nodes) + " edges " + str(self.shield_snake.edges) + ' dist ' + str(self.shield_snake.dist) +'\n' + 'es: nodes ' + str(self.enemy_snake.nodes) + " edges " + str(self.enemy_snake.edges) + 'dist ' + str(self.enemy_snake.dist) + '\n' + 'process ' + str(self.process.pid) + " started snake: " + str(self.id))
self.wrap_around()
# build the snake
......@@ -740,6 +749,9 @@ def game_intro():
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit_game()
if event.key == pygame.K_SPACE:
select_snake()
return
Display.fill(white)
message_screen("Snake Game", green, -120, "large")
......@@ -774,7 +786,7 @@ def game_loop():
# create snakes
snake1 = Snake(startsnake1["pos"], startsnake1["vel"], startsnake1["angle"], 0, img=p1_snake[0], color=p1_snake[1],
apple_img=green_apple, act_shield=False)
apple_img=green_apple, act_shield=True)
snake2 = Snake(startsnake2["pos"], startsnake2["vel"], startsnake2["angle"], 1, img=p2_snake[0], color=p2_snake[1],
apple_img=purple_apple, act_shield=True)
snake1.set_enemy_snake(snake2.shield_snake)
......@@ -827,6 +839,11 @@ def game_loop():
snake1.key_event("down")
if event.key == pygame.K_UP:
snake1.key_event("up")
if event.key == pygame.K_1:
if snake1.shield:
snake1.shield = False
elif not snake1.shield:
snake1.shield = True
if event.key == pygame.K_a: # Snake2
snake2.key_event("left")
......@@ -836,6 +853,11 @@ def game_loop():
snake2.key_event("down")
if event.key == pygame.K_w:
snake2.key_event("up")
if event.key == pygame.K_2:
if snake2.shield:
snake2.shield = False
elif not snake2.shield:
snake2.shield = True
if event.key == pygame.K_SPACE:
pause()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment