1

I am trying to make a game where there are a bunch of falling circles and the player(the square) needs to avoid them. I have managed to get the controls of the player and all of the falling circles but when I press play all I see are the falling circles and no square. The only time the square shows is when I press the arrow keys. How can I make the square and circles show up at the same time?

 import pygame
from pygame.locals import *
import os
import random
import math
import sys
import time

white = (255,255,255)
blue = (0,0,255)
gravity = 10
size =10
height = 500
width =600
varHeigth = height
ballNum = 5
eBall = []
apGame = pygame.display.set_mode((width, height))
pygame.display.set_caption("AP Project")

clock = pygame.time.Clock()

class Player(object):

  def __init__(self):
    red = (255, 0, 0)
    move_x = 300
    move_y = 400
    self.rect = pygame.draw.rect(apGame,red, (move_x, move_y, 10, 10))
    self.dist = 10

  def handle_keys(self):
    for e in pygame.event.get():
      if e.type == pygame.QUIT:
        pygame.quit();
        exit()
      elif e.type == pygame.KEYDOWN:
        key = e.key
        if key == pygame.K_LEFT:
          self.draw_rect(-1, 0)
        elif key == pygame.K_RIGHT:
          self.draw_rect(1, 0)
        elif key == pygame.K_ESCAPE:
          pygame.quit();
          exit()

  def draw_rect(self, x, y):
    red = (255, 0, 0)
    black = (0, 0, 0)
    '''apGame.fill(black)'''
    self.rect = self.rect.move(x * self.dist, y * self.dist);
    pygame.draw.rect(apGame, red , self.rect)
    pygame.display.update()


  def draw(self,surface):
    red = (255, 0, 0)
    move_x = 300
    move_y = 400
    pygame.draw.rect(apGame, red, (move_x, move_y, 10, 10))


move_x = 300
move_y = 400
red = (255, 0, 0)
black = (0, 0, 0)
player = Player()
clock = pygame.time.Clock()
'''apGame.fill(black)'''
player.draw(apGame)
pygame.display.update()

for q in range(ballNum):
  x = random.randrange(0, width)
  y = random.randrange(0, varHeigth)
  eBall.append([x, y])

while True:
#I think this is where my problem is
  apGame.fill(black)
  player.handle_keys()

  for i in range(len(eBall)):

    pygame.draw.circle(apGame, blue, eBall[i], size)

    eBall[i][1] += 5

    if eBall[i][1] > height:

        y = random.randrange(-50, -10)
        eBall[i][1] = y

        x = random.randrange(0, width)
        eBall[i][0] = x


  pygame.display.flip()
  clock.tick(30)
| improve this question | | | | |
New contributor
Bogs is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2

If you insert a line :

player.draw_rect (0, 0)

正规365体育投注just above

pygame.display.flip ()

正规365体育投注then your square will show up. I would recommend using :

pygame.key.get_pressed ()

instead of :

pygame.KEYDOWN

so that your player will keep moving as long as the key is being held down instead of having to press and release the left or right keys over and over.

Edit :正规365体育投注 In response to OPs question in comments, here is working code :

import pygame
from pygame.locals import *
import random

white = (255,255,255)
blue = (0,0,255)
gravity = 10
size =10
height = 500
width =600
varHeigth = height
ballNum = 5
eBall = []
apGame = pygame.display.set_mode((width, height))
pygame.display.set_caption("AP Project")

clock = pygame.time.Clock()

class Player(object):

  def __init__(self):
    red = (255, 0, 0)
    move_x = 300
    move_y = 400
    self.rect = pygame.draw.rect(apGame,red, (move_x, move_y, 10, 10))
    self.dist = 10

  def handle_keys(self):
    for e in pygame.event.get():
      if e.type == pygame.QUIT:
        pygame.quit();
        exit()
    key = pygame.key.get_pressed ()
    if key [pygame.K_LEFT]:
      self.draw_rect(-1, 0)
    elif key [pygame.K_RIGHT]:
      self.draw_rect(1, 0)
    elif key [pygame.K_ESCAPE]:
      pygame.quit();
      exit()
    else :
      self.draw_rect (0, 0)

  def draw_rect(self, x, y):
    red = (255, 0, 0)
    black = (0, 0, 0)
    self.rect = self.rect.move(x * self.dist, y * self.dist);
    pygame.draw.rect(apGame, red , self.rect)

  def draw(self,surface):
    red = (255, 0, 0)
    move_x = 300
    move_y = 400
    pygame.draw.rect(apGame, red, (move_x, move_y, 10, 10))


move_x = 300
move_y = 400
red = (255, 0, 0)
black = (0, 0, 0)
player = Player()
clock = pygame.time.Clock()
'''apGame.fill(black)'''
player.draw(apGame)
pygame.display.update()

for q in range(ballNum):
  x = random.randrange(0, width)
  y = random.randrange(0, varHeigth)
  eBall.append([x, y])

while True:
  apGame.fill(black)
  for i in range(len(eBall)):
    pygame.draw.circle(apGame, blue, eBall[i], size)
    eBall[i][1] += 5
    if eBall[i][1] > height:
        y = random.randrange(-50, -10)
        eBall[i][1] = y
        x = random.randrange(0, width)
        eBall[i][0] = x

  player.handle_keys ()
  pygame.display.flip()
  clock.tick(30)
| improve this answer | | | | |
  • The player is showing but I can't get the player to move. I try using your pygame.key.get_pressed() but I can't make it to work. Do you have any examples or sources to look at? – Bogs yesterday
  • yep, this did it thanks a lot man. – Bogs 16 hours ago

Your Answer

Bogs is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.