简体   繁体   中英

Why cant i place more than one rectangle in pygame?

I am trying to make a 2D game where the player can walk around with trees. But for some reason when the program tries to place a tree it doesn't appear. I have tried everything like moving the code to different sections and even setting the specific area of the rectangle to 300 300 but it won't show. If you have any ideas on how to fix it or even some suggestions that would be great. The code for the tree is in line 26.

Thank you!

CODE:

import pygame
import random

player=(255,0,0)
grass = (0,150,0)
tree = (0,100,0)

dis_height = 600
dis_width = 600

pygame.init()
dis=pygame.display.set_mode((600,600))

x1 = 300
y1 = 300
x1_change = 0
y1_change = 0
tree_placex = random.randint(1, 600)
tree_placey = random.randint(1, 600)

pygame.display.set_caption('testing stuff')
game_over=False
while not game_over:
    for event in pygame.event.get():
        for i in range(10):
            pygame.draw.rect(dis,player,[tree_placex,tree_placey,20,20])   
        
        





#/////////////////PLAYER MOVEMENT////////////////////
        if event.type==pygame.QUIT:
            game_over=True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                y1_change = -0.5
                x1_change = 0
                
            elif event.key == pygame.K_DOWN:
                y1_change = 0.5
                x1_change = 0
            elif event.key == pygame.K_LEFT:
                y1_change = 0
                x1_change = -0.5
            elif event.key == pygame.K_RIGHT:
                y1_change = 0
                x1_change = 0.5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                y1_change = 0
                x1_change = 0
            elif event.key == pygame.K_DOWN: 
                y1_change = 0
                x1_change = 0
            elif event.key == pygame.K_LEFT:
                y1_change = 0
                x1_change = 0
            elif event.key == pygame.K_RIGHT:
                y1_change = 0
                x1_change = 0
        
    
    
    if y1 < 0:
        pygame.draw.rect(dis,player,[x1,0,15,15])
        y1 = 600
        pygame.display.update()
        dis.fill(grass)
    elif x1 < 0:
        pygame.draw.rect(dis,player,[0,x1,15,15])
        x1 = 600
        pygame.display.update()
        dis.fill(grass)
    elif y1 >= dis_height:
        pygame.draw.rect(dis,player,[x1,600,15,15])
        y1 = 0
        pygame.display.update()
        dis.fill(grass)
            
    elif x1 >= dis_width:
        pygame.draw.rect(dis,player,[600,y1,15,15])
        x1 = 0
        pygame.display.update()
        dis.fill(grass)

    
    x1 += x1_change
    y1 += y1_change
    dis.fill(grass)
    pygame.draw.rect(dis,player,[x1,y1,15,15])
    pygame.display.update()
 
pygame.quit()
quit()

You are rendering the grass ontop of everything else. Try only calling dis.fill(grass) once, before anything else has drawn

Maybe here

while not game_over:
    for event in pygame.event.get():
        dis.fill(grass)

Preferabllly after you have handled the events

The typical PyGame application loop has to:

You must clear the display in the application loop before drawing the rectangles:

clock = pygame.time.Closk()

# application loop
while not game_over:
    clock.tick(100)

    # event loop
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            game_over=True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                y1_change = -0.5
                x1_change = 0            
            elif event.key == pygame.K_DOWN:
                y1_change = 0.5
                x1_change = 0
            elif event.key == pygame.K_LEFT:
                y1_change = 0
                x1_change = -0.5
            elif event.key == pygame.K_RIGHT:
                y1_change = 0
                x1_change = 0.5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                y1_change = 0
                x1_change = 0
            elif event.key == pygame.K_DOWN: 
                y1_change = 0
                x1_change = 0
            elif event.key == pygame.K_LEFT:
                y1_change = 0
                x1_change = 0
            elif event.key == pygame.K_RIGHT:
                y1_change = 0
                x1_change = 0

    # move player
    x1 += x1_change
    y1 += y1_change
    if y1 < 0:
        y1 = 600
    elif x1 < 0:
        x1 = 600
    elif y1 >= dis_height:
        y1 = 0
    elif x1 >= dis_width:
        x1 = 0  

    # clear the display    
    dis.fill(grass)    
    
    # draw all objects in the scene
    for i in range(10):
        pygame.draw.rect(dis,player,[tree_placex,tree_placey,20,20])   
    pygame.draw.rect(dis,player,[x1,y1,15,15])

    # update display
    pygame.display.update()
 
pygame.quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM