如何在pygame中为矩形指定唯一值?

2024-06-16 12:16:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在过去的一周里,我下载了pygame,并尝试制作不同的游戏,主要遵循教程。作为一个项目理念,我决定制作一个游戏/模拟,让老鼠和老鹰在屏幕上四处移动,进食、繁殖并试图生存

游戏的一个重要部分是,每只鼠标都必须有各自的信息,比如健康、饥饿和年龄的变量。然而,使用我目前的代码,我不知道我将如何做到这一点,因为我希望有新的老鼠产卵,并添加到列表中的所有老鼠时,某些事件发生,他们的个人信息

换句话说,我在问如何给每个“给定的鼠标”唯一的变量,必要时可以更改这些变量

我一直在尝试不同的方法,并做了一些谷歌搜索,但我还没有遇到soloution,提前感谢

这是我目前的代码:

import pygame
import time
import random
import sys
import os


def mouse_animation(given_mouse):
    global mouse_movement_counter, can_move_left, can_move_right, can_move_up, can_move_down


    if given_mouse.x <= 20:
        can_move_left = False

    if given_mouse.x >= 1100:
        can_move_right = False

    if given_mouse.y <= 20:
        can_move_up = False

    if given_mouse.y >= 600:
        can_move_down = False


    direction = random.randint(1, 4)

    if direction == 1 and mouse_movement_counter <= 0 and can_move_right:

        given_mouse.x += 60
        mouse_movement_counter += 30
        

    elif direction == 2 and mouse_movement_counter <= 0 and can_move_up: #UP

        given_mouse.y -= 60
        mouse_movement_counter += 30
        

    elif direction == 3 and mouse_movement_counter <= 0 and can_move_down:  # DOWN

        given_mouse.y += 60
        mouse_movement_counter += 30
        

    elif direction == 4 and mouse_movement_counter <= 0 and can_move_left:  # LEFT

        given_mouse.x -= 60
        mouse_movement_counter += 30
        
    elif direction == 5 and mouse_movement_counter <= 0:
        mouse_movement_counter += 30
    else:
        mouse_movement_counter -= 1

        

    
    pygame.display.update()


def random_postion():
    global x_location, y_location
    randomx_postion = random.randint(1,6)
    if randomx_postion == 1:
        x_location = 60
    if randomx_postion == 2:
        x_location = 120
    if randomx_postion == 3:
        x_location = 180
    if randomx_postion == 4:
        x_location = 240
    if randomx_postion == 5:
        x_location = 300
    if randomx_postion == 6:
        x_location = 360

    randomy_postion = random.randint(1,6)

    if randomy_postion == 1:
        y_location = 60
    if randomy_postion == 2:
        y_location = 120
    if randomy_postion == 3:
        y_location = 180
    if randomy_postion == 4:
        y_location = 240
    if randomy_postion == 5:
        y_location = 300
    if randomy_postion == 6:
        y_location = 360




pygame.init()

clock = pygame.time.Clock()

FPS = 10

screen_width, screen_height = 1160, 680

screen = pygame.display.set_mode((screen_width, screen_height))

MOUSE_IMAGE = pygame.image.load(os.path.join("assets", "mouse.png"))


BG = pygame.transform.scale(pygame.image.load(os.path.join("assets", "background.png")), (screen_width,
                                                                                          screen_height))

mice = []

add_mouse = True

x_location = 0
y_location = 0

for i in range(40):
    random_postion()
    mouse = pygame.Rect(x_location, y_location, 40, 40)
    mice.append(mouse)

mouse_movement_counter = 0

while True:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(BG, (0, 0))

    for certain_mouse in range(len(mice)):
        pygame.draw.rect(screen, (200, 200, 200), mice[certain_mouse])

    mouse_loop = 0

    while mouse_loop < len(mice):
        can_move_right = True
        can_move_left = True
        can_move_up = True
        can_move_down = True
        mouse_animation(mice[mouse_loop])
        mouse_loop += 1

    pygame.display.flip()


Tags: andmoveifcounterlocationrandomscreenpygame
1条回答
网友
1楼 · 发布于 2024-06-16 12:16:44

我可以想到三种方法。第一个是使用鼠标类

class Mouse:
    def __init__(self, age, health, hunger):
        self.age = age
        self.health = health
        self.hunger = hunger

mice = []
if some_event_happens:
    mice.append(Mouse(some_age, some_health, some_hunger))

如果您还不熟悉类,可以使用二维数组

mice = []
if some_event_happens:
    mice.append([some_age, some_health, some_hunger])

mice[index]允许您访问每个鼠标mice[index][0]是该指数下的小鼠年龄,mice[index][1]是该指数下的小鼠健康状况,[index][2]是该指数下的小鼠饥饿感

第三种方法是使用dictionary

mice = []
if some_event_happens:
    mice.append({"age": some_age, "health": some_health, "hunger": some_hunger})

我个人更喜欢使用这个二维数组,因为索引的含义没有歧义,因为使用的是单词而不是更清晰的数字。范例

mice[index]["age"]

相关问题 更多 >