如何解决如何在 pygame 中为矩形分配唯一值?
上周我下载了pygame,并尝试制作不同的游戏,主要是按照教程。作为一个项目创意,我决定制作一个游戏/模拟,让老鼠和老鹰在屏幕上移动、进食、繁殖并试图生存。
游戏的一个重要部分是每只老鼠都必须拥有个人信息,例如健康、饥饿和年龄的变量。然而,使用我当前的代码,我不知道我将如何做到这一点,因为我希望在某些事件发生时产生新的老鼠并添加到所有老鼠的列表中,并附上它们的个人信息。
换句话说,我在问如何为每个“given_mouse”提供独特的变量,我可以在必要时更改这些变量。
我一直在尝试不同的方法,并进行了一些谷歌搜索,但我还没有找到解决方案,提前致谢!
这是我目前的代码:
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()
解决方法
我能想到的三种方法。第一个是使用鼠标类。
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_hunger])
mice[index]
允许您访问每只鼠标。 mice[index][0]
是该索引处鼠标的年龄,mice[index][1]
是该索引处鼠标的健康状况,而 mouse[index][2]
是该索引处鼠标的饥饿度。
第三种方法是使用 dictionary
。
mice = []
if some_event_happens:
mice.append({"age": some_age,"health": some_health,"hunger": some_hunger})
我个人更喜欢将其用于 2D 数组,因为索引的含义没有歧义,因为使用单词而不是更清晰的数字。示例
mice[index]["age"]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。