Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 4046
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T10:10:15+00:00 2022-06-03T10:10:15+00:00

python – problems with overlapping images in pygame

  • 0

[ad_1]

previous question was :
Im 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 to a rect object on the screen which moves. the problem i am having is the rect object is overlapping my background image when its moving around. i was looking to only be able to see the green helicopter shape and not the black outline around it. sorry if i havent explained this very well. will try to include all files im using. Thanks for any help

new part:
the .convert_alpha() worked for my helicopter image to remove the rectangle outline. im having the same problem with my missile moving wall objects now. where i can see the rect object outline around my missiles.

thanks for any help.

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)

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8087 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1871 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1957 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.