I'm having problems with blitting images to rect objects in pygame. I have a background image blitted to my main pygame window and also an image blitted on a rect object on the screen which moves. The problem is taht the rect object is overlapping my background image when it's moving around. I was looking for only be able to see the green helicopter shape and not the black outline around it.
The .convert_alpha()
worked for my helicopter image to remove the rectangle outline. I'm having the same problem with my missile moving wall objects now. Where I can see the rect object outline around my missiles.
import pygame as pg
import random as r
import time
import openpyxl as op
pg.init()
def load_best ():
file = 'best.xlsx'
wb= op.load_workbook(file)
sheet = wb['Sheet1']
best = sheet.cell(row=1, column=1).value
return best
def save_best (score):
file = 'best.xlsx'
wb= op.load_workbook(file)
sheet = wb['Sheet1']
sheet['a1'] = score
wb.save(file)
MAX_X = 1190
MAX_Y = 590
MIN_X = 10
MIN_Y = 10
SIZE = 100
SPEED = 2
COLOR = (255,145,0)
move_amount = 0
MISSILE_THICK = 20
wn = pg.display.set_mode((1200, 600))
#### only heli should be convert_alpha #################
BG_IMG = pg.image.load('desert.png').convert()
BG_IMG = pg.transform.scale(BG_IMG, (1200, 600))
MOVING_WALL_IMG = pg.transform.scale(pg.image.load('missile.png').convert_alpha(), (SIZE, MISSILE_THICK))
STILL_WALL_IMG = pg.transform.scale(pg.image.load('wall.gif').convert(), (MAX_X, 10))
class Wall (pg.Rect):
def __init__(self, posX, posY):
self.xcor = posX
self.ycor = posY
self.rect = None
class Heli (pg.Rect):
def __init__(self, posX, posY):
self.image = pg.transform.scale(pg.image.load('li.png').convert_alpha(), (150,100))
self.rect = self.image.get_rect()
self.rect.x = posX
self.rect.y = posY
# top and bottom constant walls
TOP = pg.Rect(MIN_X, MIN_Y-10, MAX_X, 10)
BOTTOM = pg.Rect(MIN_X, MAX_Y, MAX_X, 10)
heli = Heli(MIN_X + 100, MAX_Y //2)
# keep moving walls in a list
moving_walls = [Wall(MAX_X, r.randint((MIN_Y + 10), (MAX_Y - 10)))]
# count is a control for how many iterations of the main loop have past
count = 0
best = load_best()
# main loop
while True:
count += 1
# fill screen
wn.fill('black')
# show score text on screen
font = pg.font.Font('freesansbold.ttf', 32)
text = font.render(f' Score : {count//2}', True, 'cyan')
textRect = text.get_rect()
textRect.center = (100,50)
# show best score text on screen
best_font = pg.font.Font('freesansbold.ttf', 32)
best_text = best_font.render(f' Best : {best}', True, 'cyan')
best_textRect = best_text.get_rect()
best_textRect.center = (100,100)
#wn.blit(text, textRect)
# editing objects to move
# blitting must happen before everything else
pg.draw.rect(wn,COLOR, heli.rect)
wn.blit(BG_IMG, (0,0))
wn.blit(heli.image, heli.rect)
wn.blit(text, textRect)
wn.blit(best_text, best_textRect)
heli.rect.y += move_amount
heli.rect.y += 1
# dealing with the moving walls and their conditions
for wall in moving_walls :
wall.rect = pg.Rect(wall.xcor, wall.ycor, SIZE, MISSILE_THICK)
#wall.rect = MOVING_WALL_IMG.get_rect()
pg.draw.rect(wn, COLOR, wall.rect)
wn.blit(MOVING_WALL_IMG, wall.rect)
wall.xcor -= SPEED
if wall.xcor < MIN_X + 10:
moving_walls.remove(wall)
# COLLISIONS
collide = [pg.Rect.colliderect(heli.rect, wall.rect), pg.Rect.colliderect(heli.rect, BOTTOM), pg.Rect.colliderect(heli.rect, TOP)]
if collide[0] or collide[1] or collide[2] :
# taking care of best score when collided
best = load_best()
if count//2 > best:
save_best(count//2)
time.sleep(2)
pg.quit()
# THIS Part controls the speed at which moving_walls respawn
## LEVEL 1
if count < 3000 :
if count%250 == 0 :
moving_walls.append(Wall(MAX_X, r.randint((MIN_Y + 10), (MAX_Y - 10))))
## LEVEL 2
if count > 3000 and count < 6000 :
# count controls frequency of wall spawns
if count%150 == 0 :
moving_walls.append(Wall(MAX_X, r.randint((MIN_Y + 10), (MAX_Y - 10))))
## LEVEL 2
if count > 6000 and count < 1000000 :
# count controls frequency of wall spawns
if count%50 == 0 :
moving_walls.append(Wall(MAX_X, r.randint((MIN_Y + 10), (MAX_Y - 10))))
# drawing all objects back to the screen
pg.draw.rect(wn, COLOR, TOP)
pg.draw.rect(wn, COLOR, BOTTOM)
wn.blit(STILL_WALL_IMG, TOP)
wn.blit(STILL_WALL_IMG, BOTTOM)
#pg.draw.rect(wn,COLOR, heli.rect)
# check for collisions
collide = pg.Rect.colliderect(heli.rect, wall.rect)
# update window
pg.display.update()
# event handling
for ev in pg.event.get():
# key presses
if ev.type == pg.KEYDOWN:
if ev.key == pg.K_SPACE:
move_amount = -3
if ev.type == pg.KEYUP:
move_amount = 0
# quit button
if ev.type == pg.QUIT:
pg.quit()
time.sleep(0.01)