filhoweuler / pygame-snake Goto Github PK
View Code? Open in Web Editor NEWSimples jogo de Snake feito com PyGame.
Simples jogo de Snake feito com PyGame.
Você me ajudou muito, obrigado por disponibilizar esse projeto ainda com um passo a passo =)
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)
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!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.