Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame Rect Not Responding
#1
In my Code I try to move the yellow Rectangles over my Pygame Window but they dont respond. The green and the red rectangle respond to my commands. Only 1 of my 3 yellow rectangles respond.

This is my Code:
import pygame
from random import *

pygame.init()

run = True
score = 0
lives = 3

game_over_counter = 0
spawn_protection = 0

spike_increase = 5
smove_counter = 0

# Colors -----------------------------------------------------------------------

white = (255, 255, 255)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)
black = (0, 0, 0)
orange = (255, 100, 10)
yellow = (255, 255, 0)
blue_green = (0, 255, 170)
maroon = (115, 0, 0)
lime = (180, 255, 100)
pink = (255, 100, 180)
purple = (240, 0, 255)
gray = (127, 127, 127)
magenta = (255, 0, 230)
brown = (100, 40, 0)
forest_green = (0, 50, 0)
navy_blue = (0, 0, 100)
rust = (210, 150, 75)
dandylion_yellow = (255, 200, 0)
highlighter = (255, 255, 100)
sky_blue = (0, 255, 255)
light_gray = (200, 200, 200)
dark_gray = (50, 50, 50)

# Display ----------------------------------------------------------------------

SCREEN_WIDTH = 950
SCREEN_HEIGHT = 950

WIN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Eat Game')

clock = pygame.time.Clock()
FPS = 60

# Writing ----------------------------------------------------------------------

font = pygame.font.SysFont('comicsans', 40)
game_over_font = pygame.font.SysFont('comicsans', 100)

# Character --------------------------------------------------------------------

x = 100
y = 100
width = 90
height = 90
vel = 3

# Food

fx = randint(41, SCREEN_WIDTH - width)
fy = randint(41, SCREEN_HEIGHT - height)
fwidth = 30
fheight = 30

# Spikes

sx = randint(41, SCREEN_WIDTH - width)
sy = randint(41, SCREEN_HEIGHT - height)
swidth = 50
sheight = 50
sx2 = randint(41, SCREEN_WIDTH - width)
sy2 = randint(41, SCREEN_HEIGHT - height)
s2width = 50
s2height = 50
sx3 = randint(41, SCREEN_WIDTH - width)
sy3 = randint(41, SCREEN_HEIGHT - height)
s3width = 50
s3height = 50

# Main Loop --------------------------------------------------------------------

while run:
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    WIN.fill(white)

    spike = pygame.draw.rect(WIN, yellow, (sx, sy, swidth, sheight))
    spike2 = pygame.draw.rect(WIN, yellow, (sx2, sy2, s2width, s2height))
    spike3 = pygame.draw.rect(WIN, yellow, (sx3, sy3, s3width, s3height))
    food = pygame.draw.rect(WIN, green, (fx, fy, fwidth, fheight))
    character = pygame.draw.rect(WIN, red, (x, y, width, height))

# Texts --------------------------------------------------------------------------

    score_text = font.render(f'Score: {score}', True, lime)
    lives_text = font.render(f'Lives: {lives}', True, red)
    game_over_text = game_over_font.render('Game Over', True, red)
    WIN.blit(score_text, (10, 10))
    WIN.blit(lives_text, (823, 10))

# Player Movement ----------------------------------------------------------------

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > 0:
        x -= vel
    if keys[pygame.K_RIGHT] and x < SCREEN_WIDTH - width:
        x += vel
    if keys[pygame.K_UP] and y > 0:
        y -= vel
    if keys[pygame.K_DOWN] and y < SCREEN_HEIGHT - height:
        y += vel
    if keys[pygame.K_ESCAPE]:
        run = False
    if keys[pygame.K_SPACE]:
        spawn_protection = 0

# Food Movement --------------------------------------------------------------------------------

    if keys[pygame.K_LEFT] and fx > 0:
        fx -= vel/2
    if keys[pygame.K_RIGHT] and fx < SCREEN_WIDTH - fwidth:
        fx += vel/2
    if keys[pygame.K_UP] and fy > 0:
        fy -= vel/2
    if keys[pygame.K_DOWN] and fy < SCREEN_HEIGHT - fheight:
        fy += vel/2

# Spikes Movement -----------------------------------------------------------------------------

    if smove_counter < 130:
        sx += vel
        sy -= vel
        sx2 -= vel
        sx2 += vel
        sx3 += vel
        sx3 -= vel
    else:
        sx -= vel
        sy += vel
        sx2 += vel
        sx2 -= vel
        sx3 -= vel
        sx3 += vel

    if smove_counter > 260:
        smove_counter = 0

# Checking for Collision ----------------------------------------------------------------------

    if character.colliderect(food):
        spawn_protection = 0
        score += 1
        fx = randint(41, SCREEN_WIDTH - fwidth)
        fy = randint(41, SCREEN_HEIGHT - fheight)
        sx = randint(41, SCREEN_WIDTH - fwidth)
        sy = randint(41, SCREEN_HEIGHT - fheight)
        sx2 = randint(41, SCREEN_WIDTH - fwidth)
        sy2 = randint(41, SCREEN_HEIGHT - fheight)
        sx3 = randint(41, SCREEN_WIDTH - fwidth)
        sy3 = randint(41, SCREEN_HEIGHT - fheight)
        swidth += spike_increase
        sheight += spike_increase
        s2width += spike_increase
        s2height += spike_increase
        s3width += spike_increase
        s3height += spike_increase

    spawn_protection += 1
    smove_counter += 1

    if spawn_protection > 80:

        if character.colliderect(spike):
            lives -= 1
            fx = randint(41, SCREEN_WIDTH - swidth)
            fy = randint(41, SCREEN_HEIGHT - sheight)
            sx = randint(41, SCREEN_WIDTH - swidth)
            sy = randint(41, SCREEN_HEIGHT - sheight)
            sx2 = randint(41, SCREEN_WIDTH - swidth)
            sy2 = randint(41, SCREEN_HEIGHT - sheight)
            sx3 = randint(41, SCREEN_WIDTH - swidth)
            sy3 = randint(41, SCREEN_HEIGHT - sheight)
        if character.colliderect(spike2):
            lives -= 1
            fx = randint(41, SCREEN_WIDTH - swidth)
            fy = randint(41, SCREEN_HEIGHT - sheight)
            sx = randint(41, SCREEN_WIDTH - swidth)
            sy = randint(41, SCREEN_HEIGHT - sheight)
            sx2 = randint(41, SCREEN_WIDTH - swidth)
            sy2 = randint(41, SCREEN_HEIGHT - sheight)
            sx3 = randint(41, SCREEN_WIDTH - swidth)
            sy3 = randint(41, SCREEN_HEIGHT - sheight)
        if character.colliderect(spike3):
            lives -= 1
            fx = randint(41, SCREEN_WIDTH - swidth)
            fy = randint(41, SCREEN_HEIGHT - sheight)
            sx = randint(41, SCREEN_WIDTH - swidth)
            sy = randint(41, SCREEN_HEIGHT - sheight)
            sx2 = randint(41, SCREEN_WIDTH - swidth)
            sy2 = randint(41, SCREEN_HEIGHT - sheight)
            sx3 = randint(41, SCREEN_WIDTH - swidth)
            sy3 = randint(41, SCREEN_HEIGHT - sheight)

# Checking for Game Over -----------------------------------------------------------------------

    if lives <= 0:
        WIN.blit(game_over_text, (SCREEN_WIDTH/5, SCREEN_HEIGHT/3))

        game_over_counter += 1

    if game_over_counter > 180:
        run = False

    pygame.display.update()
Yoriz write May-02-2021, 01:56 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The problem is here :

if smove_counter < 130:
    sx += vel
    sy -= vel
    sx2 -= vel
    sx2 += vel
    sx3 += vel
    sx3 -= vel
else:
    sx -= vel
    sy += vel
    sx2 += vel
    sx2 -= vel
    sx3 -= vel
    sx3 += vel
You assign sx2 and sx3 twice instead of sy2 and sy3 once adding vel and then subtracting vel. Here is the working code.

import pygame
from random import *

pygame.init()

run = True
score = 0
lives = 3

game_over_counter = 0
spawn_protection = 0

spike_increase = 5
smove_counter = 0

# Colors -----------------------------------------------------------------------

white = (255, 255, 255)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)
black = (0, 0, 0)
orange = (255, 100, 10)
yellow = (255, 255, 0)
blue_green = (0, 255, 170)
maroon = (115, 0, 0)
lime = (180, 255, 100)
pink = (255, 100, 180)
purple = (240, 0, 255)
gray = (127, 127, 127)
magenta = (255, 0, 230)
brown = (100, 40, 0)
forest_green = (0, 50, 0)
navy_blue = (0, 0, 100)
rust = (210, 150, 75)
dandylion_yellow = (255, 200, 0)
highlighter = (255, 255, 100)
sky_blue = (0, 255, 255)
light_gray = (200, 200, 200)
dark_gray = (50, 50, 50)

# Display ----------------------------------------------------------------------

SCREEN_WIDTH = 950
SCREEN_HEIGHT = 950

WIN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Eat Game')

clock = pygame.time.Clock()
FPS = 60

# Writing ----------------------------------------------------------------------

font = pygame.font.SysFont('comicsans', 40)
game_over_font = pygame.font.SysFont('comicsans', 100)

# Character --------------------------------------------------------------------

x = 100
y = 100
width = 90
height = 90
vel = 3

# Food

fx = randint(41, SCREEN_WIDTH - width)
fy = randint(41, SCREEN_HEIGHT - height)
fwidth = 30
fheight = 30

# Spikes

sx = randint(41, SCREEN_WIDTH - width)
sy = randint(41, SCREEN_HEIGHT - height)
swidth = 50
sheight = 50
sx2 = randint(41, SCREEN_WIDTH - width)
sy2 = randint(41, SCREEN_HEIGHT - height)
s2width = 50
s2height = 50
sx3 = randint(41, SCREEN_WIDTH - width)
sy3 = randint(41, SCREEN_HEIGHT - height)
s3width = 50
s3height = 50

# Main Loop --------------------------------------------------------------------

while run:
	clock.tick(FPS)

	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			run = False

	WIN.fill(white)

	spike = pygame.draw.rect(WIN, yellow, (sx, sy, swidth, sheight))
	spike2 = pygame.draw.rect(WIN, yellow, (sx2, sy2, s2width, s2height))
	spike3 = pygame.draw.rect(WIN, yellow, (sx3, sy3, s3width, s3height))
	food = pygame.draw.rect(WIN, green, (fx, fy, fwidth, fheight))
	character = pygame.draw.rect(WIN, red, (x, y, width, height))

# Texts --------------------------------------------------------------------------

	score_text = font.render(f'Score: {score}', True, lime)
	lives_text = font.render(f'Lives: {lives}', True, red)
	game_over_text = game_over_font.render('Game Over', True, red)
	WIN.blit(score_text, (10, 10))
	WIN.blit(lives_text, (823, 10))

# Player Movement ----------------------------------------------------------------

	keys = pygame.key.get_pressed()

	if keys[pygame.K_LEFT] and x > 0:
		x -= vel
	if keys[pygame.K_RIGHT] and x < SCREEN_WIDTH - width:
		x += vel
	if keys[pygame.K_UP] and y > 0:
		y -= vel
	if keys[pygame.K_DOWN] and y < SCREEN_HEIGHT - height:
		y += vel
	if keys[pygame.K_ESCAPE]:
		run = False
	if keys[pygame.K_SPACE]:
		spawn_protection = 0

# Food Movement --------------------------------------------------------------------------------

	if keys[pygame.K_LEFT] and fx > 0:
		fx -= vel/2
	if keys[pygame.K_RIGHT] and fx < SCREEN_WIDTH - fwidth:
		fx += vel/2
	if keys[pygame.K_UP] and fy > 0:
		fy -= vel/2
	if keys[pygame.K_DOWN] and fy < SCREEN_HEIGHT - fheight:
		fy += vel/2

# Spikes Movement -----------------------------------------------------------------------------

	if smove_counter < 130:
		sx += vel
		sy -= vel
		sx2 -= vel
		sy2 += vel
		sx3 += vel
		sy3 -= vel
	else:
		sx -= vel
		sy += vel
		sx2 += vel
		sy2 -= vel
		sx3 -= vel
		sy3 += vel
	if smove_counter > 260:
		smove_counter = 0

# Checking for Collision ----------------------------------------------------------------------

	if character.colliderect(food):
		spawn_protection = 0
		score += 1
		fx = randint(41, SCREEN_WIDTH - fwidth)
		fy = randint(41, SCREEN_HEIGHT - fheight)
		sx = randint(41, SCREEN_WIDTH - fwidth)
		sy = randint(41, SCREEN_HEIGHT - fheight)
		sx2 = randint(41, SCREEN_WIDTH - fwidth)
		sy2 = randint(41, SCREEN_HEIGHT - fheight)
		sx3 = randint(41, SCREEN_WIDTH - fwidth)
		sy3 = randint(41, SCREEN_HEIGHT - fheight)
		swidth += spike_increase
		sheight += spike_increase
		s2width += spike_increase
		s2height += spike_increase
		s3width += spike_increase
		s3height += spike_increase

	spawn_protection += 1
	smove_counter += 1

	if spawn_protection > 80:
		if character.colliderect(spike):
			lives -= 1
			fx = randint(41, SCREEN_WIDTH - swidth)
			fy = randint(41, SCREEN_HEIGHT - sheight)
			sx = randint(41, SCREEN_WIDTH - swidth)
			sy = randint(41, SCREEN_HEIGHT - sheight)
			sx2 = randint(41, SCREEN_WIDTH - swidth)
			sy2 = randint(41, SCREEN_HEIGHT - sheight)
			sx3 = randint(41, SCREEN_WIDTH - swidth)
			sy3 = randint(41, SCREEN_HEIGHT - sheight)
		if character.colliderect(spike2):
			lives -= 1
			fx = randint(41, SCREEN_WIDTH - swidth)
			fy = randint(41, SCREEN_HEIGHT - sheight)
			sx = randint(41, SCREEN_WIDTH - swidth)
			sy = randint(41, SCREEN_HEIGHT - sheight)
			sx2 = randint(41, SCREEN_WIDTH - swidth)
			sy2 = randint(41, SCREEN_HEIGHT - sheight)
			sx3 = randint(41, SCREEN_WIDTH - swidth)
			sy3 = randint(41, SCREEN_HEIGHT - sheight)
		if character.colliderect(spike3):
			lives -= 1
			fx = randint(41, SCREEN_WIDTH - swidth)
			fy = randint(41, SCREEN_HEIGHT - sheight)
			sx = randint(41, SCREEN_WIDTH - swidth)
			sy = randint(41, SCREEN_HEIGHT - sheight)
			sx2 = randint(41, SCREEN_WIDTH - swidth)
			sy2 = randint(41, SCREEN_HEIGHT - sheight)
			sx3 = randint(41, SCREEN_WIDTH - swidth)
			sy3 = randint(41, SCREEN_HEIGHT - sheight)

# Checking for Game Over -----------------------------------------------------------------------

	if lives <= 0:
		WIN.blit(game_over_text, (SCREEN_WIDTH/5, SCREEN_HEIGHT/3))

	game_over_counter += .0001

	if game_over_counter > 180:
		run = False

	pygame.display.update()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cmath.rect accepts a negative modulus JMB 2 304 Jan-17-2024, 08:00 PM
Last Post: JMB
  elif not responding on print EddieG 3 822 Jul-20-2023, 09:44 PM
Last Post: Pedroski55
  IDLE stops responding upon saving tompi1 2 1,888 Oct-01-2020, 05:44 PM
Last Post: Larz60+
  Code is not responding abhishek81py 0 1,471 Jul-07-2020, 12:40 PM
Last Post: abhishek81py
  Sublime text not responding Mat 1 1,929 Mar-23-2020, 11:42 AM
Last Post: Fre3k
  IDLE not responding (Mac) Selinal 1 2,922 Aug-05-2019, 10:27 PM
Last Post: Larz60+
  Client Server Game Pygame not responding Damien 2 3,101 Aug-04-2018, 06:19 PM
Last Post: micseydel
  Discord bot not responding to command [New User] Alabaster 0 3,366 May-20-2018, 06:34 PM
Last Post: Alabaster
  [Discord.py] Bot not responding? Ouindoze 2 10,028 Oct-10-2017, 01:35 AM
Last Post: Ouindoze
  Pygame*import pygame ImportError: No module named pygame CASPERHANISCH 1 9,707 Jun-05-2017, 09:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020