python-如何使对象在pygame中以随机模式移动?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-如何使对象在pygame中以随机模式移动?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2709字,纯文字阅读大概需要4分钟。
内容图文
![python-如何使对象在pygame中以随机模式移动?](/upload/InfoBanner/zyjiaocheng/670/180f8f2781db46d18f37016c8f8160c8.jpg)
我一直在进行“小行星”翻拍.但是,我无法使对象随机运动.我敢肯定,这是使用向量演变的,但是我不知道如何为每个小行星随机生成向量.
我正在寻找类似于此asteroid game中显示的动作的动作,但是我什至不知道如何开始.到目前为止,这是我的代码:
import pygame as game
import random as r
import math
game.init()
game.display.set_caption("Asteroids")
screen = game.display.set_mode([800,600])
time=0
gameon=True
bgcolor = game.color.Color("#f6cb39")
black=game.color.Color("black")
badguy = game.image.load("asteroid.png")
badguy = game.transform.scale(badguy, (50, 50))
badguys=[]
SPAWNENEMY=10
CLOCK=11
game.time.set_timer(SPAWNENEMY,800)
game.time.set_timer(CLOCK,1000)
font=game.font.Font(None,20)
timetext=font.render("Time: 0", 0, black)
while gameon:
screen.fill(bgcolor)
event=game.event.poll()
if event.type==SPAWNENEMY:
bgbox = game.Rect(badguy.get_rect())
bgbox.x = r.randint(50,800)
bgbox.y = r.randint(50,600)
badguys.append(bgbox)
if event.type==game.QUIT:
gameon=False;
for bg in badguys:
'''
This is where I tried to put the mocment code,
but I was unableto get randmom movments,
only for asteroids to randomly appear or consistently
move in one direction something like "bg.x+=2"
'''
for bg in badguys:
screen.blit(badguy,(bg.x,bg.y))
game.display.flip()
我很抱歉,如果它过长,我不知道我还能切出什么来创建MCV.
解决方法:
这是使用向量的方法. Badguy列表中的每个项目现在都是一对项目,其当前位置和关联的速度矢量.请注意,位置本身也是一个向量(也称为“位置向量”).
只需将每个坏家伙的速度向量加到其当前位置即可完成当前位置的更新.即bg [0] = bg [1].
import pygame as game
import pygame.math as math
from pygame.time import Clock
import random as r
game.init()
game.display.set_caption("Asteroids")
screen = game.display.set_mode([800, 600])
time = 0
gameon = True
bgcolor = game.color.Color("#f6cb39")
black = game.color.Color("black")
clock = Clock()
badguy = game.image.load("asteroid.png")
badguy = game.transform.scale(badguy, (50, 50))
badguys = []
SPAWNENEMY = 10
CLOCK = 11
game.time.set_timer(SPAWNENEMY, 800)
game.time.set_timer(CLOCK, 1000)
font=game.font.Font(None,20)
timetext=font.render("Time: 0", 0, black)
while gameon:
screen.fill(bgcolor)
event = game.event.poll()
if event.type == SPAWNENEMY:
# Select a random initial position vector.
posn = math.Vector2(r.randint(50, 800), r.randint(50, 600))
# Create a random speed vector.
speed = r.randint(1, 10)
dx = r.random()*speed * r.choice((-1, 1))
dy = r.random()*speed * r.choice((-1, 1))
vector = math.Vector2(dx, dy)
# Each badguy item is a [position, speed vector].
badguys.append([posn, vector])
if event.type == game.QUIT:
gameon = False;
for bg in badguys:
# Update positions.
bg[0] += bg[1] # Update position using speed vector.
for bg in badguys:
screen.blit(badguy, bg[0])
clock.tick(60)
game.display.flip()
内容总结
以上是互联网集市为您收集整理的python-如何使对象在pygame中以随机模式移动?全部内容,希望文章能够帮你解决python-如何使对象在pygame中以随机模式移动?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。