jeudi 24 septembre 2020

Placing objects at random places in pygame

Hi I am writing a simple Snake game in python using pygame. There is one thing I cannot quite figure out, that is: how to put an object at a random place on the screen, without it flickering.

Here is a bit of my code:

class Food(object):

    def __init__(self):
        self.eaten = False

    def food_spawn(self):
        self.food_x = random.randrange(0, 800, 1)
        self.food_y = random.randrange(0, 800, 1)

    def food_drawing(self):
        self.food_spawn()
        pygame.draw.circle(win, (0, 255, 0), (self.food_x, self.food_y), 15)


def window_drawing():
    # Filling the window with the background color to make the block move and not to draw longer and longer stripes
    win.fill((0,0,0))


    # Drawing the Food and the snake
    player.snake_drawing()
    apple.food_drawing()

    pygame.display.update()



player = Snake(300, 50)
apple = Food()

I know that the issue is that I call random.randrange(0,800,1) every time I call apple.food_drawing(), and hence the new position with every iteration. However, I cannot find a way how to make the food position random without using randrange.

Do You have any idea how to make this work?

This is my entire code if you want to try it out:

import pygame
import random

pygame.init()
screen_widht = 800
screen_height = 800

pygame.display.set_caption('Snake Game')
win = pygame.display.set_mode((screen_widht, screen_height))

clock = pygame.time.Clock()

snake_vel = 5
mainLoop = True
snake_moving = True




class Snake(object):

    def __init__(self, snake_x, snake_y):
        # self.colour = colour
        self.snake_x = snake_x
        self.snake_y = snake_y
        self.snake_widht = 25
        self.snake_height = 25

    def snake_drawing(self):
        pygame.draw.rect(win, (255,0,0),(self.snake_x, self.snake_y, self.snake_widht, self.snake_height))


    def move_snake(self):
        pass


class Food(object):

    def __init__(self):
        self.eaten = False

    def food_spawn(self):
        self.food_x = random.randrange(0, 800, 1)
        self.food_y = random.randrange(0, 800, 1)

    def food_drawing(self):
        self.food_spawn()
        pygame.draw.circle(win, (0, 255, 0), (self.food_x, self.food_y), 15)


def window_drawing():
    # Filling the window with the background color to make the block move and not to draw longer and longer stripes
    win.fill((0,0,0))


    # Drawing the Food and the snake
    player.snake_drawing()
    apple.food_drawing()

    pygame.display.update()



player = Snake(300, 50)
apple = Food()

snake_dir_x = False
snake_dir_y = False
snake_dir_neg_x = False
snake_dir_neg_y = False


while mainLoop:
    clock.tick(30)

    # creates a list of pressed keys
    keys = pygame.key.get_pressed()


    # creates a list of events, ex: mouse click events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False

    # Pauses the game(more accureatly the snakes movment)
    if keys[pygame.K_p]:
        snake_dir_x = False
        snake_dir_y = False
        snake_dir_neg_x = False
        snake_dir_neg_y = False


    # Control of the snake: the for loop and the break statments are so that the snake can not move side ways
    for _ in range(1):

        if keys [pygame.K_RIGHT] and player.snake_x < screen_widht - player.snake_widht:
            player.snake_x += snake_vel
            snake_dir_x = True
            snake_dir_y = False
            snake_dir_neg_x = False
            snake_dir_neg_y = False
            break

        if keys[pygame.K_LEFT] and player.snake_x > 0:
            player.snake_x -= snake_vel
            snake_dir_x = False
            snake_dir_y = False
            snake_dir_neg_x = True
            snake_dir_neg_y = False
            break

        if keys[pygame.K_DOWN] and player.snake_y < screen_height - player.snake_height:
            player.snake_y += snake_vel
            snake_dir_x = False
            snake_dir_y = False
            snake_dir_neg_x = False
            snake_dir_neg_y = True
            break

        if keys[pygame.K_UP] and player.snake_y > 0:
            player.snake_y -= snake_vel
            snake_dir_x = False
            snake_dir_y = True
            snake_dir_neg_x = False
            snake_dir_neg_y = False
            break

        else:
            if snake_dir_x and player.snake_x < screen_widht - player.snake_widht:
                player.snake_x += snake_vel
            if snake_dir_neg_x and player.snake_x > 0:
                player.snake_x -= snake_vel
            if snake_dir_neg_y and player.snake_y < screen_height - player.snake_height:
                player.snake_y += snake_vel
            if snake_dir_y and player.snake_y > 0:
                player.snake_y -= snake_vel


    window_drawing()

All help is appreciated thank You.




Aucun commentaire:

Enregistrer un commentaire