jeudi 25 mai 2017

How to Interact with an item of a List in Pygame?

i Started creating a game and i stumbled into a little problem:

*When pressing "SPACE Bar" Red Squares keep Spawning randomly on Display

Question How can i make the Red Squares into obstacles?

im a total begginer and im sorry for asking such a simple question.. :/

The Code might give you an idea:

import pygame, sys
from random import randint
from pygame.locals import*

"List the Stores the Squares"
red_square_list = []

gameDisplay_width = 800
gameDisplay_height = 600

pygame.init()
gameDisplay = pygame.display.set_mode((gameDisplay_width, gameDisplay_height))
pygame.display.set_caption("Square-it")
clock = pygame.time.Clock()
red_color = pygame.Color("red")

"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)

"Red Squares"
def create_red_square(x, y):
    red_width = 10
    red_height = 10
    red = pygame.Rect(x, y, red_width, red_height)
    return red

while True:

    clock.tick(60)
    gameDisplay.fill((0, 20, 5))
    gameDisplay.fill(white_color, white)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           sys.exit()

    for each in red_square_list:
        gameDisplay.fill(red_color, each)

    pygame.display.update()

    '''White Square Movement'''
    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        white.left = white.left - 4

    if keys[K_RIGHT]:
        white.right = white.right + 4

    if keys[K_UP]:
        white.top = white.top - 4

    if keys[K_DOWN]:
        white.bottom = white.bottom + 4

    "when Space key Pressed, Spawns red Squares"
    if keys[K_SPACE]:
        x = randint(0, gameDisplay_width)
        y = randint(0, gameDisplay_height)
        red_square_list.append(create_red_square(x, y))




Aucun commentaire:

Enregistrer un commentaire