让精灵随机出现

发布于 2024-10-21 21:34:57 字数 580 浏览 1 评论 0原文

我试图让精灵随机出现在屏幕上,而不使用 OOP 原则,

此代码来自 AI

        if randint(1, 10) == 1:
            leaf = Leaf(world, leaf_image)
            leaf.location = Vector2(randint(0, w), randint(0, h))
            world.add_entity(leaf)



        world.process(time_passed)
        world.render(screen)

        pygame.display.update()

问题的蚂蚁演示: 如何让精灵随机出现在屏幕上? 我知道要复制它们 但是如果不使用面向对象,

这是我的代码唯一缺少精灵随机出现的方法的部分 这是 antstate.py 的代码,我在其中获取代码: http://www.mediafire.com/?5tjswcyl9xt5huj

I am trying to have sprites randomly appear on the screen with out using OOP principles

this code is from a ants demo for AI

        if randint(1, 10) == 1:
            leaf = Leaf(world, leaf_image)
            leaf.location = Vector2(randint(0, w), randint(0, h))
            world.add_entity(leaf)



        world.process(time_passed)
        world.render(screen)

        pygame.display.update()

Question:
How do I get Sprites on the screen randomly?
I know to blit them
but how without using Object-Oriented

this is the only part my code is missing a way for sprites to randomly appear
this the code to the antstate.py where im getting the code:
http://www.mediafire.com/?5tjswcyl9xt5huj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2024-10-28 21:34:57

精灵是一个对象。所以你需要使用一些 OOP 来处理精灵。这是一个例子:

# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples

import pygame
import random

# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)

# This class represents the ball       
# It derives from the "Sprite" class in Pygame
class Block(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()

# Initialize Pygame
pygame.init()

# Set the height and width of the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width,screen_height])

# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'RenderPlain.'
block_list = pygame.sprite.RenderPlain()

for i in range(50):
    # This represents a block
    block = Block(black, 20, 15)

    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)

    # Add the block to the list of objects
    block_list.add(block)

A sprite is an object. So you need to use some OOP to work with a sprite. Here's an example:

# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples

import pygame
import random

# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)

# This class represents the ball       
# It derives from the "Sprite" class in Pygame
class Block(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()

# Initialize Pygame
pygame.init()

# Set the height and width of the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width,screen_height])

# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'RenderPlain.'
block_list = pygame.sprite.RenderPlain()

for i in range(50):
    # This represents a block
    block = Block(black, 20, 15)

    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)

    # Add the block to the list of objects
    block_list.add(block)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文