1

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?

Umer Asif
  • 33
  • 4
  • `win.fill(0)` is missing. (right after `while run:`) – Rabbid76 Sep 04 '22 at 08:57
  • but this time the screen doesnot glitch but the background is not blitted and the speed continues to increase. Why? – Umer Asif Sep 04 '22 at 09:00
  • What is the background image? – Rabbid76 Sep 04 '22 at 09:02
  • Oh i figured it out. When i increase the evely then when it is odd the answer in condition ` if y1 == 400: y1 = -400 if y2 == 400: y2 = -400 ` is never attained so it put here: ` if y1 >= 400: y1 = -400 if y2 >= 400: y2 = -400` Now it is working fine. But what was the logic of win.fill(0) – Umer Asif Sep 04 '22 at 09:05
  • You have to do `win.fill(0)` directly after `while run:` before you `blit` the background – Rabbid76 Sep 04 '22 at 09:06
  • Read [How can I move the ball instead of leaving a trail all over the screen in pygame?](https://stackoverflow.com/questions/65494890/how-can-i-move-the-ball-instead-of-leaving-a-trail-all-over-the-screen-in-pygame) – Rabbid76 Sep 04 '22 at 09:06
  • Can you please tell me how can i correctly detect the collision between the ball and the plane. The height adn width of plane is 50 by 50 and that of ball is 30 by 30? – Umer Asif Sep 04 '22 at 09:45
  • The comment section is not intended to ask follow up questions. If you have another question, you must [Ask a public question](https://stackoverflow.com/questions/ask). This may help you: [Sometimes the ball doesn't bounce off the paddle in pong game](https://stackoverflow.com/questions/62864205/sometimes-the-ball-doesnt-bounce-off-the-paddle-in-pong-game) – Rabbid76 Sep 04 '22 at 15:56

0 Answers0