May-02-2021, 10:20 AM
(This post was last modified: May-02-2021, 01:56 PM by Yoriz.
Edit Reason: Added code tags
)
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:
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()