My code is:
from random import randint
import time
import pygame as pg
pg.init()
win = pg.display.set_mode((500, 400))
run = True
img = pg.image.load('a.png')
plane = pg.image.load('b.png')
ball = pg.image.load('c.png')
blast = pg.image.load('d.png')
ball = pg.transform.scale(ball, (30, 30))
ball2 = pg.image.load('e.png')
ball2 = pg.transform.scale(ball2, (30, 30))
x1 = 0
y1 = 0
y2 = -400
px = 225
py = 300
velx = 3
vely = 2
evely = 2
enemies = []
previous_time = int(time.time())
class Enemy:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
def collision(self, px, py):
if px <= self.x+30 <= px+70 and py <= self.y <= py+30:
return True
count = 0
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
win.blit(img, (x1, y1))
win.blit(img, (x1, y2))
y1 += evely
y2 += evely
if y1 == 400:
y1 = -400
if y2 == 400:
y2 = -400
keys = pg.key.get_pressed()
if keys[pg.K_LEFT] and px > 0:
px -= velx
if keys[pg.K_RIGHT] and px < 450:
px += velx
if keys[pg.K_UP] and py > 0:
py -= vely
if keys[pg.K_DOWN] and py < 350:
py += vely
win.blit(plane, (px, py))
if count == 20:
enemies.append(Enemy(randint(0, 500), 0, randint(1, 2)))
count = 0
for i in range(len(enemies)):
if enemies[i].collision(px, py):
win.blit(blast, (px, py))
pg.display.flip()
pg.time.delay(12000)
run = False
break
enemies[i].y += evely
if enemies[i].color == 1:
win.blit(ball, (enemies[i].x, enemies[i].y))
else:
win.blit(ball2, (enemies[i].x, enemies[i].y))
count += 1
pg.display.flip()
if int(time.time()) - previous_time == 10:
print("Level Upgraded")
evely += 1
previous_time = int(time.time())
pg.quit()
score = 0
for i in range(len(enemies)):
if enemies[i].y > py:
score += 1
print(f"Your score is {score}")
This is small plane game. When i added the change the speed of enimies and background after every 10sec option then game is getting glithcy like this: Screen shot But when i only change the velocity of enimies then it works good. But when i change the velocity of background it becomes glitchy. How can i solve this issue completely?