Giter Site home page Giter Site logo

pygame-snake's Issues

Melhorias

Algumas sugestões de melhorias.

Esse trecho...

# Check if the snake has hit itself
    for i in range(1, len(snake) - 1):
        if snake[0][0] == snake[i][0] and snake[0][1] == snake[i][1]:
            game_over = True
            break

...poderia ser substituído por esse:

# Check if the snake has hit itself
    if snake[0] in snake[1:]:
        game_over = True
        break

E esse...

for i in range(len(snake) - 1, 0, -1):
        snake[i] = (snake[i-1][0], snake[i-1][1])
        
    # Actually make the snake move.
    if my_direction == UP:
        snake[0] = (snake[0][0], snake[0][1] - 10)
    if my_direction == DOWN:
        snake[0] = (snake[0][0], snake[0][1] + 10)
    if my_direction == RIGHT:
        snake[0] = (snake[0][0] + 10, snake[0][1])
    if my_direction == LEFT:
        snake[0] = (snake[0][0] - 10, snake[0][1])

...poderia ser substituído por esse:

snake.pop()

        # Actually make the snake move.
        if my_direction == UP:
            snake.insert(0, (snake[0][0], snake[0][1] - 10))
        if my_direction == DOWN:
            snake.insert(0, (snake[0][0], snake[0][1] + 10))
        if my_direction == RIGHT:
            snake.insert(0, (snake[0][0] + 10, snake[0][1]))
        if my_direction == LEFT:
            snake.insert(0, (snake[0][0] - 10, snake[0][1]))

Não sei se melhora o desempenho, mas o código fica levemente mais enxuto.
Um abraço!

Obrigado por isso

Você me ajudou muito, obrigado por disponibilizar esse projeto ainda com um passo a passo =)

Melhoria na geração de maçãs

Notei que ao executar o jogo, muitas vezes a maçã era gerada dentro do proprio corpo da cobra...

no trecho:
def on_grid_random():
x = random.randint(50, 550)
y = random.randint(50, 550)
return (x // 10 * 10, y // 10 * 10)

fiz a seguinte alteração fazendo com que a maçã nao seja mais gerada na posição em que a cobra está ocupando:

def on_grid_random():
x = random.randint(50, 550)
y = random.randint(50, 550)
if x or y in range(len(snake)):
x = random.randint(50, 550)
y = random.randint(50, 500)
return (x // 10 * 10, y // 10 * 10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.