I'm creating a simple game and I got stuck on a problem.
The game goes like this: "White square" eats "Green Squares"
Question:
How can I spawn "New Red squares" and remain on screen each time the "White square" eats a "Green square" ?
Here is the code:
import pygame, sys
from random import randint
from pygame.locals import*
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Square-it")
clock = pygame.time.Clock()
"Green Square"
green_x = randint(10, 780)
green_y = randint(10, 580)
green_width = 15
green_height = 15
green_color = pygame.Color("green")
green = pygame.Rect(green_x, green_y, green_width, green_height)
"Red Square"
red_x = randint(20, 780)
red_y = randint(20, 580)
red_width = 10
red_height = 10
red_color = pygame.Color("red")
red = pygame.Rect(red_x, red_y, red_width, red_height)
"White Square"
white_x = 400
white_y = 300
white_width = 10
white_height = 10
white_color = pygame.Color("white")
white = pygame.Rect(white_x, white_y, white_width, white_height)
while True:
clock.tick(60)
gameDisplay.fill((0, 20, 5))
gameDisplay.fill(white_color, white)
gameDisplay.fill(green_color, green)
gameDisplay.fill(red_color, red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
'''When White eats Green, Green regenerates'''
if white.colliderect(green):
green = pygame.Rect(randint(10, 780), randint(10, 580), green_width, green_height)
'''White Square Movement'''
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
white.left = white.left - 10
if keys[K_RIGHT]:
white.right = white.right + 10
if keys[K_UP]:
white.top = white.top - 10
if keys[K_DOWN]:
white.bottom = white.bottom + 10
Aucun commentaire:
Enregistrer un commentaire