What can I use to move a Rect. space in Pygame?

36 Views Asked by At

How can you move a Rect space in pygame?

I tried using .left, .right, .up, .bottom, .center

Surely it´s an easy thing but I couldn´t find the error in the program

I thought it´d move but nothing happened and the apple didn´t move. Maybe the error is that I put: screen.blit(newastronaut0, newastronaut)

The important things have this: #WITH UPPER CASE

Thank you.


Here is my code, newastronaut0 is surface and newastronaut is Rect. Also tapples is a list of surface, and rectApples is a list of Rect:

import pygame, sys
from pygame.locals import *
import time
import copy

from Programation_image_imports import newastronaut0, wood_panel, lion_king, newastronaut, screen, newbackground, cajas, boxes, finish
from Programation_variables import touching_apples, color, display_width, display_height, game_start, time_buy, finish_buy, x, y, level, exo, eyo, send_apples, send_tomatos, send_milk, send_sushi, suma, eyo
from Programation_levels import __all__, tapples, ttomatos, tmilk, tsushi, tl1, tl2, tl3, tl4, tl5, tl6, tl7, tl8, tl9, tl10, tl11, rectApples, rectTomatos, rectMilks, rectSushis

pygame.init()  


smallfont = pygame.font.SysFont("comicsansms", 20)

def text_objects(text, color, size = "small"):
    if size == "small":
        textSurface = smallfont.render(text, True, color)
        return textSurface, textSurface.get_rect()


def message_to_screen(msg, color, y_displace = 0, size="small"):
    textSurf, textRect = text_objects(msg, color, size)
    textRect.center = (int(display_width / 2), int(display_height / 2) + y_displace)
    screen.blit(textSurf, textRect)

**#HERE I MAKE THE FUNCTION BUT IT DOESN´T WORK**

def touch_apples():
    
    rectApples[0].center = newastronaut.center
    
#Now the real game starts, with the infinite loop
while True:
    
    #So this is to make the screen update itself
    pygame.display.update()
    
    #Here I see which keys are being pressed in that moment and after I'll program them
    k = pygame.key.get_pressed()
    
    #Checking the w, a, s, d and changing the y and the x of the austronaut (player)
    if k[pygame.K_w] or k[K_UP] == True:
        newastronaut.top -= 2
    if k[pygame.K_a] or k[K_LEFT] == True:
        newastronaut.left -= 2
    if k[pygame.K_s] or k[K_DOWN] == True:
        newastronaut.bottom += 2
    if k[pygame.K_d] or k[K_RIGHT] == True:
        newastronaut.right += 2
    
    #If the pressed key is i, and you haven't started the game, it makes it start
    if k[pygame.K_i] == True or game_start == True:
    
        game_start = True
        level = 1
        
        screen.blit(newbackground, (0,0))
        screen.blit(newastronaut0, newastronaut)
    
    #This looks out when the "buying time" starts
    if time_buy == True:
        
        screen.blit(wood_panel, (500, -20))
        screen.blit(lion_king, (520, -2))
    
    #And this makes the levels pass and the time_buy repeat
    if finish_buy == True:
        
        level += 1
        
        finish_buy = False
        time.sleep(2)
        time_buy = True
    
    #This controls that the player dosen't go out of the screen
    if newastronaut.left < -39:
        newastronaut.left = -38
    if newastronaut.top < -64:
        newastronaut.top = -63.2
    if newastronaut.right > 750:
        newastronaut.right = 749
    if newastronaut.bottom > 690:
        newastronaut.bottom = 689
    
    #Here we start seing the levels   
    if level == 1:
 
        screen.blit(cajas[0], (5, 15))
        screen.blit(boxes[0], (5, 530))
        
        send_apples = 2
        
        message_to_screen(tl1, color, y_displace = 10, size = "small")
                
    if level == 2:

        screen.blit(cajas[1], (130, 15))
        screen.blit(boxes[1], (130, 530))
        
        send_tomatos = 2
        
        message_to_screen(tl2, color, y_displace = 10, size = "small")
        
        
    if level == 3:
        
        send_apples = 7
        
        message_to_screen(tl3, color, y_displace = 10, size = "small")
        
    if level == 4:

        send_tomatos = 5
        
        message_to_screen(tl4, color, y_displace = 10, size = "small")

    if level == 5:

        screen.blit(cajas[2], (250, 15))
        screen.blit(boxes[2], (250, 530))
        
        send_milk = 2
        
        message_to_screen(tl5, color, y_displace = 10, size = "small")
      
    if level == 6:
        
        send_apples = 10
        
        message_to_screen(tl6, color, y_displace = 10, size = "small")

    if  level == 7:

        send_tomatos = 7
        
        message_to_screen(tl7, color, y_displace = 10, size = "small")
        
    if level == 8:

        send_milk = 6
        
        message_to_screen(tl8, color, y_displace = 10, size = "small")
        
    if level == 9:

        screen.blit(cajas[3], (370, 15))
        screen.blit(boxes[3], (370, 530))
        
        send_sushi = 1
        
        message_to_screen(tl9, color, y_displace = 10, size = "small")
        
        
    if level == 10:
        
        send_milk = 6
        
        message_to_screen(tl10, color, y_displace = 10, size = "small")
        
        
    if level == 11:

        send_sushi = 3
        
        message_to_screen(tl11, color, y_displace = 10, size = "small")
        
    if level == 12:
        
        screen.blit(finish, (0, 0))
        
        time.sleep(4)
        
        pygame.quit()
        quit

    #send_something variables let us know how many things they will send us
    if send_apples != 0:
        
        #Now we look out if there are too many apples
        if send_apples > 5:
            
            suma = 10
            
        else:
            suma = 20
        
        exo = 40
        
        #Now we control it so the apples go to the right position
        for i in range(send_apples):
            
            rectApples[i].right = exo
            rectApples[i].top = eyo
            
            screen.blit(tapples[i], rectApples[i])
            exo += suma
            

        time_buy = True   
        send_apples = 0
        
    #This is the same but with tomatos 
    if send_tomatos != 0:
        
        if send_tomatos > 5:
            
            suma = 10
            
        else:
            suma = 20
        
        exo = 180
        
        for i in range(send_tomatos):
            
            rectTomatos[i].right = exo
            rectTomatos[i].top = eyo
            
            screen.blit(ttomatos[i], rectTomatos[i])
            
            exo += suma
            
            if exo > 500:
                exo = 500

        time_buy = True   
        send_tomatos = 0
    
    #And with milk
    if send_milk != 0:
        
        if send_milk > 5:
            
            suma = 10
            
        else:
            suma = 20
        
        exo = 310        
        for i in range(send_milk):
            
            rectMilks[i].right = exo
            rectMilks[i].top = eyo
            
            screen.blit(tmilk[i], rectMilks[i])
            exo += suma
            if exo > 500:
                exo = 500

        time_buy = True   
        send_milk = 0
    
    if send_sushi != 0:
        
 
        if send_sushi > 5:
            
            suma = 10
            
        else:
            suma = 20
        
        exo = 450
        
        for i in range(send_sushi):
            
            rectSushis[i].right = exo
            rectSushis[i].top = eyo
            
            screen.blit(tsushi[i], rectSushis[i])
            exo += suma
            if exo > 500:
                exo = 500

        time_buy = True
        send_sushi = 0

**#HERE I LOOK IF THERE IS COLLISION AND I CALL THE FUNCTION BUT NOTHING HAPPENS  **  

    if pygame.Rect.colliderect( rectApples[0], newastronaut ) == True:
        
        touching_apples = True
        touch_apples()
        
    if pygame.Rect.colliderect( rectTomatos[0], newastronaut ) == True:
        
        print("")
    
    if pygame.Rect.colliderect( rectMilks[0], newastronaut ) == True:
        
        print("")
        
    if pygame.Rect.colliderect( rectSushis[0], newastronaut ) == True:
        
        print("")
    
    

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            pygame.quit()
            sys.exit()
pygame.quit()
quit()
1

There are 1 best solutions below

0
ShadowOfHassen On

You can just use rect.x += speed to change the rects x by speed. Then you just blit it every main loop.

The issue could be because you only move the object two pixels. Two pixels isn't that much, and so you could not see it.