具有不同“速度"的pygame元素

编程入门 行业动态 更新时间:2024-10-25 08:24:24
本文介绍了具有不同“速度"的pygame元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚制作了一个太空入侵游戏,里面的东西掉到地上,你必须避免崩溃等.

I just made a space-invadish game, where things fall to the ground and you have to avoid crashing, etc.

我成功地创建了 2 个同时下落的对象,但我无法让它们以不同的速度这样做.

I succeeded in creating 2 objects falling down simultaneously but I cannot make them doing this with different speed.

这是第一个对象的属性.

This the first object's attributes.

thing_startx = random.randrange(0, display_width-100) thing_starty = -700 thing_speed = 4

现在它掉了

thing_starty += thing_speed

在每次 while 循环迭代中.

in each while loop iteration.

对于下一个对象,我只是在原始 X 和 Y 坐标上添加了随机数,因此它的位置不同.(如果mult == True,请参见下面的函数以创建两个矩形对象)

For the next object I just added random numbers to the original X and Y coordinates so it gets it different position. (cf function below to create two rect objects if mult == True)

def things_mult(thingx, thingy, thingw, thingh, color, mult, xopt, yopt, wopt, col2): if mult == False: pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) else: pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw , thingh]) pygame.draw.rect(gameDisplay, col2, [thingx + xopt, thingy + yopt, thingw + wopt, thingh])

现在,我假设我只需要定义

Now, I assume I just need to define

thingy_new = thing_starty + thing_yopt thingy_new = thingy_new + thing_speed* someconstant #(to make it faster or slower)

不幸的是,它不是那样工作的.有人可以向我解释为什么我以某种方式在这个简单的逻辑上有缺陷吗?

Unfortunately, it does not work out like that. Can someone please explain to me why I am somehow shortcoming that simple logic?

推荐答案

最简单的解决方案是将代表你的游戏对象的列表中的 rect、speed 和其他数据组合起来,然后将这些对象放入另一个列表中并使用 for 循环更新位置并绘制它们.

The simplest solution is to combine the rect, speed and other data in lists which represent your game objects, and then put these objects into another list and use for loops to update the positions and to draw them.

您也可以使用字典而不是列表来使代码更具可读性.

You could also use dictionaries instead of lists to make the code more readable.

import pygame as pg pg.init() screen = pg.display.set_mode((640, 480)) clock = pg.time.Clock() BG_COLOR = pg.Color('gray12') # The objects consist of a pygame.Rect, the y-speed and a color. objects = [ [pygame.Rect(150, -20, 64, 30), 5, pg.Color('dodgerblue')], [pygame.Rect(350, -20, 64, 30), 3, pg.Color('sienna1')], ] done = False while not done: for event in pg.event.get(): if event.type == pg.QUIT: done = True for obj in objects: # [0] is the rect, [1] is the y-speed. # Move the objects by adding the speed to the rect.y coord. obj[0].y += obj[1] screen.fill(BG_COLOR) # Draw the rects. for obj in objects: pg.draw.rect(screen, obj[2], obj[0]) pg.display.flip() clock.tick(60) pg.quit()

如果您知道类的工作原理并且您的对象也需要特殊的行为,那么最好为您的对象定义一个类.

If you know how classes work and your objects also need special behavior, then better define a class for your objects.

更多推荐

具有不同“速度"的pygame元素

本文发布于:2023-07-18 04:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141064.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   速度   quot   pygame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!