Threadlocal值iList排序(Threadlocal values iList ordering)
嗨,我有一个创建2个线程的示例。 我的问题是,当我输出值时,它总是在999之前打印1000.是否可以在1000之前打印999.只是想知道它们是如何订购的?
static void Main(string[] args) { ThreadLocal<int> field = new ThreadLocal<int>(() => 0, true); Thread firstThread = new Thread(new ThreadStart(() => { field.Value = 999; })); Thread secondThread = new Thread(new ThreadStart(() => { field.Value = 1000; })); firstThread.Start(); secondThread.Start(); firstThread.Join(); secondThread.Join(); IList<int> valueList = field.Values; foreach (int arr in valueList) Console.WriteLine(arr); Console.Read(); }
Hi i have an example which creates 2 threads. My question is when i output the values it always prints 1000 before 999. Is it possible to print 999 before 1000. Just want to know how are they ordered?
static void Main(string[] args) { ThreadLocal<int> field = new ThreadLocal<int>(() => 0, true); Thread firstThread = new Thread(new ThreadStart(() => { field.Value = 999; })); Thread secondThread = new Thread(new ThreadStart(() => { field.Value = 1000; })); firstThread.Start(); secondThread.Start(); firstThread.Join(); secondThread.Join(); IList<int> valueList = field.Values; foreach (int arr in valueList) Console.WriteLine(arr); Console.Read(); }
原文:https://stackoverflow.com/questions/33978889
最满意答案
这真的不是很难......但是我在快速搜索中找到的最好的示例代码也是一个可用的库,可以为你工作:
spritesheet
,直接来自pygame wiki。所以,你可以从使用它开始。 我会给你一个根据你的用例量身定做的例子,但是你没有告诉我们你的代码是什么样的或者你想做什么,所以我不可能给你比那个页面更好的东西给你,所以:
import spritesheet ... ss = spritesheet.spritesheet('somespritesheet.png') # Sprite is 16x16 pixels at location 0,0 in the file... image = ss.image_at((0, 0, 16, 16)) images = [] # Load two images into an array, their transparent bit is (255, 255, 255) images = ss.images_at((0, 0, 16, 16),(17, 0, 16,16), colorkey=(255, 255, 255)) …
同时,您可以阅读
spritesheet
类中的(非常简单的)代码以了解它的工作原理。It really isn't very hard to do… but the best sample code I found in a quick search is also a usable library that does the work for you:
spritesheet
, right from the pygame wiki.So, you can start off by just using that. I'd give you an example tailored to your use case, but you haven't given us any idea what your code looks like or what you want to do, so I can't possibly give you anything better than is already on that page, so:
import spritesheet ... ss = spritesheet.spritesheet('somespritesheet.png') # Sprite is 16x16 pixels at location 0,0 in the file... image = ss.image_at((0, 0, 16, 16)) images = [] # Load two images into an array, their transparent bit is (255, 255, 255) images = ss.images_at((0, 0, 16, 16),(17, 0, 16,16), colorkey=(255, 255, 255)) …
Meanwhile, you can read the (very simple) code in that
spritesheet
class to understand how it works.
相关问答
更多-
全名是pygame.sprite.Sprite 。 如果你使用import pygame.sprite as Sprite那么正确的名称是player(Sprite.Sprite)而不是player(Sprite.sprite) - 在两个Sprite看到上面的S 至于我,最好使用class player(pygame.sprite.Sprite)和bullet(pygame.sprite.Sprite) - s一个sprite较低s和第二个Sprite上部S - 并且没有import pygame.spr ...
-
这条线 self.rect = self.image.get_rect() 因为类Player没有名为image的属性会崩溃。 如果你想使用pygame的Sprite类,你应该遵循它的文档并添加一个image和一个rect属性: 可见游戏对象的基类。 派生类将要覆盖Sprite.update()并分配Sprite.image和Sprite.rect属性。 初始化程序可以接受要添加的任意数量的Group实例。 使用pygame的Sprite类的好处是你可以使用Group类及其子类轻松管理你的sprite。 ...
-
在PyGame中移动一个Sprite(Moving a Sprite in PyGame)[2022-07-16]
Mario是一个班级。 方法handle_keys(self)是一个实例方法 - 意味着它只能针对Mario实例调用。 (有可能有一个classmethod ,但这不是你想要的,因为你需要修改self 。) 在顶部你做了一个b = Mario([0, 800]) - 我将b改为mario到处都是c和ladder 。 mario = Mario([0, 800]) 然后你将使用mario.handle_keys()而不是mario.handle_keys() 。 更多背景: 当你调用mario.handl ... -
我认为问题是你有两个变量来保持位置。 但我没有检查 - 我更改了代码,使其更好地组织和问题消失。 #!/usr/bin/env python3 import pygame # --- constants --- WIDTH = 800 HEIGHT = 600 FPS = 30 WHITE = (225, 225, 225) # --- classes --- class Invader(): def __init__(self, x, y): self.image ...
-
扩展pygame sprite类(Extending pygame sprite class)[2023-07-22]
只需从Sprite继承你的中间类,然后继承它: class my_custom_class(pygame.sprite.Sprite): def common_physics_stuff() pass class ShipSprite(my_custom_class): ... 如果您想将“custom_class”事物添加到一个不必像Sprite一样的抽象类中,并且可以在其他上下文中使用,那么您也可以使用多重继承 - class my_custom_class(obje ... -
发现的第一个问题是你必须修改 pygame.image.load(Mario_sideways_sprite_2xL.png) 有类似的东西。 pygame.image.load("Mario_sideways_sprite_2xL.png") 除此之外,代码还有许多阻碍其工作的问题。 例如, 你没有实例化你的水管工班 。 class plumber(sprite)应该是plumber(Sprite) (还是更好的Plumber(Sprite) ) 你需要这样的东西: myplumber = Plumb ...
-
它安静轻松, 使用Group.update()方法对sprite进行blitting。 all_sprites_list.draw(mainScreen) 不要将障碍添加到all_sprites_list。 然后他们永远不会在屏幕上搞砸了。 但是barrier_hit_list仍然可以用于碰撞。 its quiet easy, sprites were blitted with the Group.update() method. all_sprites_list.draw(mainScreen) Do ...
-
这真的不是很难......但是我在快速搜索中找到的最好的示例代码也是一个可用的库,可以为你工作: spritesheet ,直接来自pygame wiki。 所以,你可以从使用它开始。 我会给你一个根据你的用例量身定做的例子,但是你没有告诉我们你的代码是什么样的或者你想做什么,所以我不可能给你比那个页面更好的东西给你,所以: import spritesheet ... ss = spritesheet.spritesheet('somespritesheet.png') # Sprite is 16x16 ...
-
如何在Pygame中移动Sprite(How to move Sprite in Pygame)[2023-06-14]
正如ecline6所述,在这一点上,鸟是你最不担心的。 考虑阅读这本书 .. 现在,首先让我们清理你的代码... import pygame import os # let's address the class a little later.. pygame.init() screen = pygame.display.set_mode((640, 400)) # you only need to call the following once,so pull them out of the whi ... -
如何使用PyGame.sprite(How to use PyGame.sprite)[2022-03-21]
好吧,所以pygame sprite对象是一个bass类,可以用来创建其他类,然后你可以用它来做碰撞检测等事情: class Player(pygame.Sprite): def __init__(self): self.image = pygame.image.load("player.png").convert_alpha() self.rect = self.image.get_rect() self.pos = self.rect.x, sel ...