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 a 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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6859499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:17:28+00:00 2026-05-27T02:17:28+00:00

import pygame import random red = [255,0,0] green = [0,255,0] blue = [0,0,255] white

  • 0
import pygame
import random

red = [255,0,0]
green = [0,255,0]
blue = [0,0,255]
white = [255,255,255]
black = [0,0,0]
UP = [0,-1]
DOWN = [0,1]
LEFT = [-1,0]
RIGHT = [1,0]
NOTMOVING = [0,0]
#constants end
#classes
class collidable:
    x = 0
    y = 0
    w = 0
    h = 0
    rect = pygame.Rect(x,y,w,h)
    color = [0,0,0]
    def __init__(self,x,y,w,h,color):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.color = color
        self.rect = pygame.Rect(x,y,w,h)
    def draw(self):
        pygame.draw.rect(screen,self.color,[self.x,self.y,self.w,self.h],6)

class player:
    x = 0
    y = 0
    speed = 0
    rect = pygame.Rect(x,y,x+20,y+20)
    def __init__(self,x,y,speed):
        self.x = x
        self.y = y
        self.speed = speed
        self.rect = pygame.Rect(self.x,self.y,self.x+20,self.y+20)
    def draw(self):
        if player_moving==LEFT:
        pygame.draw.polygon(screen,black,[(self.x-10,self.y),(self.x+10,self.y-10),(self.x+10,self.y+10)])
        elif player_moving==RIGHT:
            pygame.draw.polygon(screen,black,[(self.x+10,self.y),(self.x-10,self.y-10),(self.x-10,self.y+10)])
        elif player_moving==UP:
            pygame.draw.polygon(screen,black,[(self.x,self.y-10),(self.x+10,self.y+10),(self.x-10,self.y+10)])
        elif player_moving==DOWN:
            pygame.draw.polygon(screen,black,[(self.x,self.y+10),(self.x+10,self.y-10),(self.x-10,self.y-10)])
        else:
            pygame.draw.rect(screen,black,pygame.Rect(self.x-10,self.y-10,20,20),6)
    def setpos(self,x,y):
        self.x = x
        self.y = y
    def move(self,direction):
        self.x = self.x + direction[0]*self.speed
        self.y = self.y + direction[1]*self.speed
#classes end

#globals
pygame.init()
screenSize = [800,600]
screenBGColor = white
screen=pygame.display.set_mode(screenSize)
pygame.display.set_caption("Move the Block")
player = player(screenSize[0]/2,screenSize[1]/2,9)
collidables = []
clock=pygame.time.Clock()
for i in range(10):
    collidables.append(collidable(random.randrange(0,screenSize[0]),random.randrange(0,screenSize[1]),random.randrange(10,200),random.randrange(10,200),blue))

running = True
#globals end

#functions
def render():
    screen.fill(screenBGColor)
    clock.tick(60)
    player.draw()
    for c in collidables:
        c.draw()
    pygame.display.flip()
def tick():                                           #----------------HERE
    for c in collidables:
        if player.rect.colliderect(c.rect):
            player_moving = NOTMOVING
            print("hit")
    player.move(player_moving)

#functions end

#main loop
while running==True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running = False
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_LEFT:
                player_moving = LEFT
            if event.key==pygame.K_RIGHT:
                player_moving = RIGHT
            if event.key==pygame.K_UP:
                player_moving = UP
            if event.key==pygame.K_DOWN:
                player_moving = DOWN
        else:
            player_moving = NOTMOVING
    tick()
    render()
#main loop end

pygame.quit()

I am trying to make a simple collision detection to disable player movement when touching a collidable object. But wherever the collidable is located, the collision is always fired. I have no idea why the player does not move, this code should only block movement if the player’s center is 10 pixels away from the border of the collidable. I am sure the approach is wrong, but i can’t think of any other ways of cheching collision.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:17:28+00:00Added an answer on May 27, 2026 at 2:17 am

    Ok, so, basicaly, you are true. Your program is good but:

    • in pygame doc:
      Rect = (PosX, PosY, Width, Height)

    So, your code (diff):

    - self.rect = pygame.Rect(self.x,self.y,self.x+20,self.y+20)
    + self.rect = pygame.Rect(self.x,self.y,self.20,self.20)
    

    And also:

      def move(self,direction):
          self.x = self.x + direction[0]*self.speed
          self.y = self.y + direction[1]*self.speed
    +     self.rect = pygame.Rect(self.x,self.y,self.20,self.20)
    

    There is another little problem in your code. For exemple, in your tick() function, you use player_moving, but, in the first iteration, this variable never exist.

    The code (which run):

    import pygame
    import random
    
    red = [255,0,0]
    green = [0,255,0]
    blue = [0,0,255]
    white = [255,255,255]
    black = [0,0,0]
    UP = [0,-1]
    DOWN = [0,1]
    LEFT = [-1,0]
    RIGHT = [1,0]
    NOTMOVING = [0,0]
    #constants end
    #classes
    class collidable:
        x = 0
        y = 0
        w = 0
        h = 0
        rect = pygame.Rect(x,y,w,h)
        color = [0,0,0]
        def __init__(self,x,y,w,h,color):
            self.x = x
            self.y = y
            self.w = w
            self.h = h
            self.color = color
            self.rect = pygame.Rect(x,y,w,h)
        def draw(self):
            pygame.draw.rect(screen,self.color,[self.x,self.y,self.w,self.h],6)
    
    class player:
        x = 0
        y = 0
        speed = 0
        rect = pygame.Rect(x,y,20,20)
        def __init__(self,x,y,speed):
            self.x = x
            self.y = y
            self.speed = speed
            self.rect = pygame.Rect(self.x,self.y,20,20)
        def draw(self):
            if player_moving==LEFT:
                    pygame.draw.polygon(screen,black,[(self.x-10,self.y),(self.x+10,self.y-10),(self.x+10,self.y+10)])
            elif player_moving==RIGHT:
                pygame.draw.polygon(screen,black,[(self.x+10,self.y),(self.x-10,self.y-10),(self.x-10,self.y+10)])
            elif player_moving==UP:
                pygame.draw.polygon(screen,black,[(self.x,self.y-10),(self.x+10,self.y+10),(self.x-10,self.y+10)])
            elif player_moving==DOWN:
                pygame.draw.polygon(screen,black,[(self.x,self.y+10),(self.x+10,self.y-10),(self.x-10,self.y-10)])
            else:
                pygame.draw.rect(screen,black,pygame.Rect(self.x-10,self.y-10,20,20),6)
        def setpos(self,x,y):
            self.x = x
            self.y = y
        def move(self,direction):
            self.x = self.x + direction[0]*self.speed
            self.y = self.y + direction[1]*self.speed
            self.rect = pygame.Rect(self.x,self.y,20,20)
    #classes end
    
    #globals
    pygame.init()
    screenSize = [800,600]
    screenBGColor = white
    screen=pygame.display.set_mode(screenSize)
    pygame.display.set_caption("Move the Block")
    player = player(screenSize[0]/2,screenSize[1]/2,9)
    collidables = []
    clock=pygame.time.Clock()
    for i in range(10):
        collidables.append(collidable(random.randrange(0,screenSize[0]),random.randrange(0,screenSize[1]),random.randrange(10,200),random.randrange(10,200),blue))
    
    running = True
    #globals end
    player_moving = NOTMOVING
    #functions
    def render():
        screen.fill(screenBGColor)
        clock.tick(60)
        player.draw()
        for c in collidables:
            c.draw()
        pygame.display.flip()
    def tick(player_moving):                                           #----------------HERE
        for c in collidables:
            if player.rect.colliderect(c.rect):
                player_moving = NOTMOVING
                print("hit"+str(c.rect)+" with "+str(player.rect))
        player.move(player_moving)
    
    #functions end
    
    #main loop
    while running==True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                running = False
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_LEFT:
                    player_moving = LEFT
                if event.key==pygame.K_RIGHT:
                    player_moving = RIGHT
                if event.key==pygame.K_UP:
                    player_moving = UP
                if event.key==pygame.K_DOWN:
                    player_moving = DOWN
            else:
                player_moving = NOTMOVING
        tick(player_moving)
        render()
    #main loop end
    
    pygame.quit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example: main.py: import pygame import mycolors color1 = mycolors.blue mycolors.py: import pygame blue
# INTIALISATION import pygame, math, sys from pygame.locals import * screen = pygame.display.set_mode((1024, 768))
when I import pygame using eclipse on my mac, there's a import error: However,
Here is how I'm implementing my simple pygames now: import pygame, sys from pygame.locals
import mymodule, ctypes #import pygame foo = ctypes.cdll.MyDll.foo print 'success' if i uncomment the
I made an MP3 player with pygame code: from Tkinter import * import pygame
I have the following Python (3.2) code: from pygame import * class Application: def
So, Iv'e got a pygame application. Right now, it takes a command line argument
I need to create a clickable 8 by 8 grid in pygame. Right now
Here is my small program, import pygame pygame.init() Here is my compilation command. python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.