You are on page 1of 6

import random

import sys

# Set up the game window

win_width = 40

win_height = 20

win = [' ' for _ in range(win_width * win_height)]

def print_win():

for i in range(0, len(win), win_width):

print(''.join(win[i:i+win_width]))

# Set up the snake

snake = [(2, 10), (2, 11), (2, 12)]

direction = (0, 1)

# Set up the food

food = (10, 10)

while True:

# Handle user input

key = sys.stdin.read(1)

if key == 'q':

break

if key == 'w':
direction = (0, -1)

if key == 's':

direction = (0, 1)

if key == 'a':

direction = (-1, 0)

if key == 'd':

direction = (1, 0)

# Move the snake

head = snake[0]

new_head = (head[0] + direction[0], head[1] + direction[1])

if new_head[0] < 0 or new_head[0] >= win_height or \

new_head[1] < 0 or new_head[1] >= win_width or \

new_head in snake[1:]:

break

if new_head == food:

# Eat the food and generate a new one

snake.append(new_head)

food = (random.randint(0, win_height-1) * win_width + random.randint(0, win_width-1),)

else:

# Remove the tail

snake.pop()

snake.insert(0, new_head)
# Clear the screen

for i in range(win_height):

for j in range(win_width):
import turtle

win = turtle.Screen()

win.title("Pong Game")

win.bgcolor("black")

win.setup(width=800, height=600)

# Paddle A

paddle_a = turtle.Turtle()

paddle_a.speed(0)

paddle_a.shape("square")

paddle_a.color("white")

paddle_a.shapesize(stretch_wid=6, stretch_len=1)

paddle_a.penup()

paddle_a.goto(-350, 0)

# Paddle B

paddle_b = turtle.Turtle()

paddle_b.speed(0)

paddle_b.shape("square")

paddle_b.color("white")

paddle_b.shapesize(stretch_wid=6, stretch_len=1)

paddle_b.penup()

paddle_b.goto(350, 0)
# Ball

ball = turtle.Turtle()

ball.speed(1)

ball.shape("square")

ball.color("white")

ball.penup()

ball.goto(0, 0)

ball.dx = 3

ball.dy = -3

# Listen for keyboard presses

win.listen()

# Move paddle A up

def paddle_a_up():

y = paddle_a.ycor()

y += 20

paddle_a.sety(y)

# Move paddle A down

def paddle_a_down():

y = paddle_a.ycor()

y -= 20

paddle_a.sety(y)
# Move paddle B up

def paddle_b_up():

y = paddle_b.ycor()

y += 20

paddle

You might also like