Note that there are some explanatory texts on larger screens.

plurals
  1. POIncreasing sprite position incrementally very fast without lag - Python
    text
    copied!<p>I'm making a PONG game for a school project using Kivy in Python. So far thanks to this forum I've made myself some AI for the NPC paddle. This is the code:</p> <pre><code> if self.ball.y &lt; self.player2.center_y: self.player2.center_y = self.player2.center_y - 4 if self.ball.y &gt; self.player2.center_y: self.player2.center_y = self.player2.center_y + 4 </code></pre> <p>This is in a method of PongGame() class called ArtificialIntelligence().</p> <p>I use this to call it:</p> <pre><code>Clock.schedule_interval(game.ArtificialIntelligence, 1/300) </code></pre> <p>This allows me to call it once every 1/300th of a second. However, anything more than 1/300, I seem to have no difference. I.e. 1/9001 does not call it once every 1/9001th of a second.</p> <p>The way it works is that it increases the y coordinate 4 pixels relative to the balls position, and it does this once every 1/300th of a second, hence why it doesn't "lag" at this rate. This is basically an "easy" mode for the player. If I want to do a "hard" mode, I need to make the NPC more accurate. I can do this by doing</p> <pre><code>self.player2.center_y = self.player2.center_y + 20 </code></pre> <p>Something like this. This would be extremely accurate. HOWEVER, it does NOT look "fluid", it looks "laggy". I assume I could get the same amount of movement by <em>calling the method more often</em> instead of changing the amount it moves via altering the pixel movement. However, I don't know how to do this, because, as I said, changing it from anywhere above 1/300 seems to make no difference. </p> <p>This is how I use my paddle:</p> <pre><code>if touch.x &lt; self.width/3: self.player1.center_y = touch.y </code></pre> <p>and I can move it as fast as I want because this updates as I move the mouse. And it looks fluid because it updates as often as it needs to update. I don't know how to do this with my AI.</p> <p>Does anyone know how I could basically make the NPC paddle more accurate, allowing me to do Easy-Normal-Hard, etc, while retaining fluidity and no lag? I see only one way I could do it: Increase the amount the method is called. </p> <p>However I bet there is a better way and I don't know how to do it. Does anyone have any Idea how I could do this? Thanks.</p> <p>Edit: it looks like I can do it like this:</p> <pre><code>Clock.schedule_interval(game.ArtificialIntelligence, 1/300) Clock.schedule_interval(game.ArtificialIntelligence, 1/300) Clock.schedule_interval(game.ArtificialIntelligence, 1/300) Clock.schedule_interval(game.ArtificialIntelligence, 1/300) Clock.schedule_interval(game.ArtificialIntelligence, 1/300) </code></pre> <p>But that seems REALLY ugly and REALLY inefficient... I'd MUCH prefer a cleaner way.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload