【问题标题】:Placing objects at random places in pygame将对象放置在pygame中的随机位置
【发布时间】:2020-09-24 18:19:46
【问题描述】:

您好,我正在使用 pygame 在 python 中编写一个简单的 Snake 游戏。有一件事我不太明白,那就是:如何将一个对象放在屏幕上的任意位置,而不会闪烁。

这是我的一些代码:

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()

我知道问题在于我每次调用apple.food_drawing() 时都会调用random.randrange(0,800,1),因此每次迭代都会出现新位置。但是,如果不使用 randrange,我找不到如何使食物位置随机化的方法。

您知道如何进行这项工作吗?

如果你想尝试一下,这是我的全部代码:

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()

感谢所有帮助。

【问题讨论】:

  • 在构造函数 (__init__) 中而不是在 food_drawing() 中调用 food_spawn()

标签: python random pygame


【解决方案1】:

在构造函数 (__init__) 中而不是在 food_drawing() 中调用 food_spawn()。所以food_xfood_y属性是在构造对象实例时设置的,并不是每一帧都改变的:

class Food(object):

    def __init__(self):
        self.eaten = False
        self.food_spawn()

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

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

【讨论】:

  • 谢谢,这解决了问题,再次感谢您!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多