我在2D游戏中遇到问题,但我不知道为什么。 当玩家到达游戏画面的中间时,我想移动精灵,但精灵开始以较慢的速度移动,当它们离屏幕1px时(这对我来说没有意义......)
这是我的代码中唯一的部分,我在其中定义了精灵的这种运动:
if self.player.rect.centerx >= WIDTH/2:
# Ensuring the player stays in the middle of the screen
self.player.pos.x -= abs(self.player.vel.x)
for obj in self.movables:
# Relocating Sprite when Player moves
obj.rect.x -= abs(self.player.vel.x)
# Killin Sprite when it is out of the screen
if obj.rect.x < 0 - obj.rect.width:
obj.kill()
# Reseting Location of the Ground
if self.ground.rect.centerx <= 0:
self.ground.rect.x = 0
编辑:清理代码
以下是计算玩家速度的部分:
def event(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.acc.x = -PLAYER_ACC
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.acc.x = PLAYER_ACC
def update(self):
self.acc = vec(0, PLAYER_GRAV)
self.event()
self.acc.x += self.vel.x * PLAYER_FRICTION
self.vel += self.acc
# Die Vel auf null setzen wenn sie ~0 ist
if abs(self.vel.x) < 0.1:
self.vel.x = 0
# Physik
self.pos += self.vel + 0.5 * self.acc
self.rect.midbottom = self.pos
答案 0 :(得分:0)
我添加了我的问题的代码示例。但是在这段代码中,玩家在几秒钟之后消失了,我认为它必须以我处理与平台碰撞的方式进行,但是现在我也不知道如何解决这个问题:D
要清楚地看到移动精灵的问题:按住“d”,当平台离屏幕一半时不要按“d”键,你可以看到精灵的移动速度比较慢其余的精灵在屏幕上
import { Injectable } from "@angular/core";
import * as sql from "mssql";
@Injectable()
export class MyService {
getMyData():Array<MyItem> {
let myData:Array<MyItem> = [];
let config = {
user: "sa",
password: "xxx",
server: "localhost",
database: "mydb"
};
const pool1 = new sql.ConnectionPool(config, err => {
if (err) {
console.log("connect erro: " + err);
}
let q:string = `SELECT TOP 10 * FROM MyTable;`;
let final = pool1.request()
.query<MyItem>(q, (err, result) => {
if (err) {
console.log("request err: " + err);
}
console.log("db result count: " + result.recordsets[0].length);
result.recordsets[0].forEach(row => {
myData.push(row);
});
});
});
return myData;
}
}