#🔒 Pls someone help me
56 messages · Page 1 of 1 (latest)
@true kestrel
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
import pygame
import random
pygame.init()
#Titulo
pygame.display.set_caption('Proyecto Snake Game')
#Window dimentions | dimensiones de ventana
ventanaW = 720
ventanaH = 540
ventana = pygame.display.set_mode((ventanaW, ventanaH))
#Dividir la pantalla, defino el valor de cada cuadro. La serpiente y etc.
tile_size = 36
speed = 10
defining colors
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
Dimention Asignation
superficie = pygame.display.set_mode((ventanaW, ventanaH))
color = (95, 189, 235) # red
superficie.fill(color)
#Imgs:
img = pygame.image.load('Board.png')
snake1 = pygame.image.load('Snake2.png')
fruit1 = pygame.image.load('Fruit.png')
snake1 = pygame.image.load('Snake2.png')
img_rect = img.get_rect()
img_rect.center = (0,0)
#Definir los tamaños de la serpiente en base al "tile".
snake1 = pygame.transform.scale(snake1, (tile_size, tile_size))
fruit1 = pygame.transform.scale(fruit1, (tile_size, tile_size))
#Posision de la serpiente
posx = ventanaH // 2
posy = ventanaW // 2
posx_change = 0
posy_change = 0
#La serpiente:
snake_tile = []
distance_snake = 1
#La fruta: Fruit1 spawn
fruit1_x = round(random.randrange(0, ventanaH - tile_size) / 36.0) * 36.0
fruit1_y = round(random.randrange(0, ventanaW - tile_size) / 36.0) * 36.0
#La serpiente: Fruit1 spawn
snake1_x = round(random.randrange(0, ventanaH - tile_size) / 36.0) * 36.0
snake1_y = round(random.randrange(0, ventanaW - tile_size) / 36.0) * 36.0
#cursor
pos = pygame.mouse.get_pos()
#F PS CONTROL
fps = pygame.time.Clock()
fonts = pygame.font.get_fonts()
for font in fonts:
print(font)
Hey @true kestrel!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
system_font = pygame.font.SysFont('ayuthaya', 32)
texto = system_font.render('Snake Game', True, (255, 255, 255))
texto_rect = texto.get_rect()
texto_rect.center = (ventanaW//2, 30)
Window execution |Ejecución de la ventana
running = True
Execution Cycle | Ciclo de ejecución
while running:
#pass
for evento in pygame.event.get():
if evento.type == pygame.QUIT:
running = False
print(evento)
if evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_LEFT:
posx_change = -tile_size
posy_change = 0
elif evento.key == pygame.K_RIGHT:
posx_change = tile_size
posy_change = 0
elif evento.key == pygame.K_UP:
posx_change = -tile_size
posy_change = 0
elif evento.key == pygame.K_DOWN:
posx_change = tile_size
posy_change = 0
#Mover la serpiente:
posx += posx_change
posy += posy_change
#Si la serpiente toca el borde:
if posx >= ventanaH or posx < 0 or posy >= ventanaW or posy < 0:
running = False
#Coloca las imagenes:
superficie.blit(img, (0,0))
superficie.blit(fruit1, (fruit1_x, fruit1_y))
#La longitud de la serpiente cambia:
snake_head = []
snake_head.append(posx)
snake_head.append(posy)
snake_tile.append(snake_head)
if len(snake_tile) > distance_snake:
del snake_tile[0]
for segment in snake_tile[:-1]:
if segment == snake_head:
running = False
for segment in snake_tile:
superficie.blit(snake1, (segment[0], segment[1]))
#Collision
if posx == fruit1_x and posy == fruit1_y:
fruit1_x = round(random.randrange(0, ventanaH - tile_size) / 36.0) * 36.0
fruit1_y = round(random.randrange(0, ventanaW - tile_size) / 36.0) * 36.0
distance_snake += 1
score += 1
pygame.display.update()
fps.tick(speed)
Para terminar la ejecución
pygame.quit()
for some reason the "fruit" collides with the snake and dies
instead of increasing the lenght
besides the snake's lenght increasing without eating the fruit (no collision)
in order for to eat the fruit, the position of the snake head needs to be equal to the position of the fruit.
where do i change or ass that?
# Collision
if posx == fruit1_x and posy == fruit1_y:
fruit1_x = round(random.randrange(0, ventanaH - tile_size) / 36.0) * 36.0
fruit1_y = round(random.randrange(0, ventanaW - tile_size) / 36.0) * 36.0
distance_snake += 1
score += 1```
that is your code.
it checks if the coordinates are equal.
and if they are equal, then it moves the fruit, increases the length of the snake, and increases the score.
do i change smth of this or it is alr changed?
it's not fully correct. but currently it's not the biggest problem.
one problem here is that it might move the fruit into the snake.
what did you change?
i just wanted to point out that the positions have to match.
but in your game the positions never match.
the snake starts at position (270, 360) and every tile has the size (36, 36).
so the snake will never be positioned on a tile.
ok
270 is not divisible by 36.
where is the 270 thing?
#Window dimentions | dimensiones de ventana
ventanaW = 720
ventanaH = 540```
```py
#Posision de la serpiente
posx = ventanaH // 2
posy = ventanaW // 2```
yes, of course. can you spot all the errors?
you have defined the tile size like this:
tile_size = 36```
so it would make sense to have the width and height of the window a multiple of this.
something like py #Window dimentions | dimensiones de ventana ventanaW = tile_size * 20 # = 720 ventanaH = tile_size * 15 # = 540
hmm.. but that was fine already.
so the error is in the calculation of posx and posy.
in your code you ofter confuse ventanaW and ventanaH.
uh huh?
W is for "width". so it's meant to be used with x coordinates.
H is for "height". so it's meant to be used with y coordinates.
posx = ventanaH // 2 sets the x coordinate of the snake to half the window's height. that makes no sense.
instead, posx should be set to half the window's width. but also rounded, so that it's divisible by tile_size.
posx = ventanaW // tile_size // 2 * tile_size
so i fix it?
that would fix the initial posx.
aighty
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.