Background in Pygame causes graphical issues - background

When I run my game which has a scrolling background it periodically starts to glitch out the right side of the screen. The screen will do it even if the speed of the background is 4x slower than previously tested. After the glitchy part has moved for a while everything goes back to normal until it happens again.
The piece of code that controls the animation is this (got this somewhere off the Internet):
def background():
global screen, bgOne, bgTwo, bgOne_x, bgTwo_x
screen.blit(bgOne, (bgOne_x, 0))
screen.blit(bgTwo, (bgTwo_x, 0))
bgOne_x -= 1
bgTwo_x -= 1
if bgOne_x == -1 * bgOne.get_width():
bgOne_x = bgTwo_x + bgTwo.get_width()
if bgTwo_x == -1 * bgTwo.get_width():
bgTwo_x = bgOne_x + bgOne.get_width()
Picture of the glitch:

(Posted on behalf of the OP).
The cause turned out to be a simple oversight. For anyone else encountering this problem: please check the dimensions of the background picture you're using and the dimensions of the display which Pygame uses. In this case the width was shorter (807) than the screen itself (1024). I hope this helps beginners like me in the future.

Related

How to fix problem with 'getColor' getting every car being green?

I'm making a program that makes the cars running in the simulation pass on their colors to other cars, to achieve that I'm using the TraCi function 'getColor'. The problem is that every car that I ask the color returns (255,255,0,255) doesn't matter what the actual color is. However, using 'getColor' inside a condition for the "contamination" makes the program work, maybe out of sheer luck. Please help me understand how to fix it and how it works.
I'm on Ubuntu 18.04.3 LTS, SUMO 0.32.0 and using the traci library. I've tried modifying the program and running the simulation step by step, even running the same line in different code with the same idea in mind.
This is the program in which the "contamination" works although it gets the wrong colors:
def run():
step = 0
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
step += 1
if step > 2:
if distancia("veh1","veh0") < 5:
traci.vehicle.setColor("veh1",(255,0,0,255))
if distancia("veh0","veh2") < 5 :
traci.vehicle.setColor("veh2",(255,0,0,255))
if traci.vehicle.getColor("veh2") == (255,0,0,255):
if distancia("veh1","veh2") < 5 :
traci.vehicle.setColor("veh1",(255,0,0,255))
print(traci.vehicle.getColor("veh1"))
traci.close()
sys.stdout.flush()
I hoped when I selected the red car I would get (255,0,0,255), but I got (255,255,0,0). But it doesn't get any error messages, just shows the worng color.
It seems that the default color for traci is yellow, I'd to set every car to its own color from the python code to start doing what I wanted.

frame rendering time measurement anomaly

I draw 2d content on GlSurfaceView using VBO/IBO with following function:
override fun onDrawFrame(gl: GL10?) {
val renderTime= measureTimeMillis {
GLES20.glClearColor(bgComps[0], bgComps[1], bgComps[2], 1f)
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT or GLES20.GL_COLOR_BUFFER_BIT)
bindAttributes()
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo)
//draw half of the polygons
val pieces = GameDataLoader.GameData.piecesByDiff(gameData.diff)
piecesProgram!!.setUniforms(projMatrix,imageTextureId,maskTextureId,PointF(4f,4f),0f)
GLES20.glDrawElements(
GLES20.GL_TRIANGLE_STRIP,
pieces * MeshBuilder.INDICES_PER_PIECE,
GLES20.GL_UNSIGNED_SHORT,
0
)
//draw another half
piecesProgram!!.setUniforms(projMatrix,imageTextureId,maskTextureId,PointF(10f,10f),sin(frame.toFloat() / 60 * 2 * PI).toFloat())
GLES20.glDrawElements(
GLES20.GL_TRIANGLE_STRIP,
pieces * MeshBuilder.INDICES_PER_PIECE,
GLES20.GL_UNSIGNED_SHORT,
pieces * MeshBuilder.INDICES_PER_PIECE * MeshBuilder.BYTES_PER_SHORT
)
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0)
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0)
}
val newFrameTS=System.currentTimeMillis()
if (lastFrameTS!=-1L)
println("frame time:${newFrameTS - lastFrameTS},render time:$renderTime")
lastFrameTS=newFrameTS
frame++
}
There are about 5.6K polygons on the screen.When I run this code I see in the console frame time: ~33ms,render time: 0-2ms(!).In most cases render time=0.If I comment everything except glClearColor and glClear render time jumps to 30ms.How can it be that the short code executes faster than long one?
UPD:The question relates to android OS.I observe such behavior on both emulator and real device.
The question is not why onDrawFrame call occurs too often or too seldom(in fact time between 2 last onDraw calls is measured ok.The question is how can I measure time for opengl calls within onDrawFrame function?How can these calls take 0ms?
This is usually caused by V-sync. The driver waits at the end of every frame until the next image can be presented on the monitor and delays execution of your program.
Depending on your hardware, you can force enable/disable this. Most PC hardware allows the application to choose if it wants to wait. On others, especially on mobile devices, you cannot.

Memory error on raspberry pi in for loop taking dark frame images with Picamera

ok, so I'm trying to take 100 dark frames with the picamera. exposure_mode is set to "off" and the shutter speed is set at 5 milseconds I'm doing this in a fairly straightforward for loop:
for i in range(NUM_DARK_FRAMES):
print ('loop %s' % (i+1))
camera.capture(output, 'jpeg', bayer=True )
arr = output.array
print ('saving array')
numpy.save('%sDarkFrame_%s' % (dark_frames_path, i+1), arr)
gc.collect()
from the print statements I'm seeing that it's getting to loop 38, and returns a memoryerror. I added the explicit garbage collect because it helped someone else with a similar issue, to no avail. any ideas?
It seems that this is due to a limitation of the Raspberry Pi, in the advanced recipes pasted below, they suggest using streaming to capture a series of images, but this doesn't work with jpeg format, which is necessary for raw bayer data captures:
https://picamera.readthedocs.io/en/release-1.10/recipes2.html
I solved this by making an outer for loop that controls the camera instantiation and an inner loop that runs for 25 images. this clears the cache and prevents the problem from arising (it usually occurred between loop 37-39).

Processing JS Hit Box Not Working

I am trying to make a hit box that causes a program restart when triggered.
Here is the code I am using:
if(snakey.position.x < mos.position.x + 20 && snakey.position.y > mos.position.y - 20 || snakey.position.x > mos.position.x - 20 && snakey.position.y < mos.position.y + 20){
Program.restart();
}
The problem is that instead of triggering the program restart when the hit box is entered it seems to be triggering randomly or at least very erratically. I have checked this several times and am getting no error messages so the syntax is fine.
Me and a friend have also both gone over the logic several times to make sure that the conditions can actually be met. We have found no problems, but it is still not activating the program restart at the right time or even at a consistent time I will link the full program bellow if anyone can tell me why it is acting so strange I would appreciate it.
https://www.khanacademy.org/computer-programming/spin-off-of-project-computational-creatures/5001415574814720
you need to wrap the conditions in brackets properly
if((snakey.position.x < mos.position.x + 20 && snakey.position.y > mos.position.y - 20) || (snakey.position.x > mos.position.x - 20 && snakey.position.y < mos.position.y + 20))

GML Alarm event not working second time

I have my game setup so that it starts and goes back to a loading screen room for 45 steps after which the next room is randomized. So at alarm[0] the following code activates:
randomize();
chosenRoom = choose(rm_roomOne, rm_roomTwo, rm_roomThree, rm_roomFour);
room_goto(chosenRoom);
The code here works fine the first time, but when it goes back from the randomly chosen room to the loading screen room it stays there and doesn't execute the code again.
Any help would be very much appreciated.
This may sound stupid but did you remember to set the alarm again after it's gone off? I know I've done this several times without thinking. Without seeing your code, I assume that after the alarm goes off it's not being set again, so it won't go off again.
I'm guessing the control object is "persistant", thus the Control Object only exists once and will remain forever (also after swithcing rooms) - thus thie create event only gets fired once - thus the alarm only gets set once.
Try to move your code to the event "Room Start" in your controller and it will work.
you can use event_perform(ev_alarm,0);.
The code here performs alarm[0] after 45 steps. after 45 steps again it triggers alarm[0]. Note that you have to put it in step event. And you have to initialize wait variable and times to zero in create event.
times is the repeat and wait is distance between events.
if(wait == 45 && times !=2){
event_perform(ev_alarm,0);
times++;
wait = 0;
}
else{
wait++;
}