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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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 1,271 Jan-17-2024, 08:00 PM
Last Post: JMB
  elif not responding on print EddieG 3 1,817 Jul-20-2023, 09:44 PM
Last Post: Pedroski55
  IDLE stops responding upon saving tompi1 2 2,834 Oct-01-2020, 05:44 PM
Last Post: Larz60+
  Code is not responding abhishek81py 0 1,970 Jul-07-2020, 12:40 PM
Last Post: abhishek81py
  Sublime text not responding Mat 1 2,496 Mar-23-2020, 11:42 AM
Last Post: Fre3k
  IDLE not responding (Mac) Selinal 1 3,640 Aug-05-2019, 10:27 PM
Last Post: Larz60+
  Client Server Game Pygame not responding Damien 2 4,121 Aug-04-2018, 06:19 PM
Last Post: micseydel
  Discord bot not responding to command [New User] Alabaster 0 3,963 May-20-2018, 06:34 PM
Last Post: Alabaster
  [Discord.py] Bot not responding? Ouindoze 2 11,910 Oct-10-2017, 01:35 AM
Last Post: Ouindoze
  Pygame*import pygame ImportError: No module named pygame CASPERHANISCH 1 10,679 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