I am trying to make a moving platform so when the platforms reaches a curtain point it is supposed to reverse direction and go back but from what I can see it looks like it is vibrating back and fort
program link: https://drive.google.com/file/d/0BzvvQCByWwmAQThfdkEtSlRKa1k/view?usp=sharing
here is my code:
class lbuild(pygame.sprite.Sprite):
#This class represents alevel builder. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height,x,y):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
# Draw the car (a rectangle!)
pygame.draw.rect(self.image, color, [0, 0, width, height])
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
self.rect.x=x
self.rect.y=y
all_sprites_list = pygame.sprite.Group()
movblock=pygame.sprite.Group()#sprite group
def level1():
global all_sprites_list
global movblock
xpos=0
for x in range(50):
all_sprites_list.add(lbuild(GREY,20,20,xpos,680))
xpos =xpos+20
ypos=660
xpos2 =40
for x in range(2):
all_sprites_list.add(lbuild(black,60,20,xpos2,ypos))
ypos=ypos-20
mblk=lbuild(RED,100,20,120,600)#draws the block
movblock.add(mblk)#adds it to the sprite group
clock=pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
#Game Logic
all_sprites_list.update()
#Drawing on Screen
screen.fill(WHITE)
#Draw The Road
spd=5
if mblk.rect.x>200:#supposed to cheak if the block x postion a has reached 200 and the reverse its direction but instead it looks like it is vibrating
spd= -spd
if mblk.rect.x<100:
spd= -spd
mblk.rect.x+=spd
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
all_sprites_list.draw(screen)
movblock.draw(screen)
#Refresh Screen
pygame.display.flip()
#Number of frames per secong e.g. 60
clock.tick(60)
Your problem is spd = 5 inside while True.
You change direction using
spd = -spd
but after that you overwrite it using
spd = 5
You have to use spd = 5 before while True
Full version with other modifications.
import pygame
import sys
# --- constants --- (UPPER_CASE names)
WHITE = (255, 255, 255)
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
BLACK = (0,0,0)
SCREEN_WIDTH=1000
SCREEN_HEIGHT=700
# --- classes --- (CamelCase names)
class LBuild(pygame.sprite.Sprite):
def __init__(self, color, width, height, x, y):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
# Draw the car (a rectangle!)
pygame.draw.rect(self.image, color, [0, 0, width, height])
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# --- functions --- (lower_case names)
def level_1(screen, all_sprites_list, movblock):
x = 0
for _ in range(50):
all_sprites_list.add(LBuild(GREY, 20, 20, x, 680))
x += 20
y = 660
x2 = 40
for _ in range(2):
all_sprites_list.add(LBuild(BLACK, 60, 20, x2, y))
y -= 20
mblk = LBuild(RED, 100, 20, 120, 600)
movblock.add(mblk)
spd = 5
# - mainloop -
clock = pygame.time.Clock()
#current_time = pygame.time.get_ticks()
# change something after 2s
#change_time = current_time + 2000 # 2000ms = 2s
while True:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
# False = exit game
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
# True = go to next level
return True
# - updates (without draws) -
#current_time = pygame.time.get_ticks()
#if current_time >= change_time:
# TODO: change something
# # change something again after 2s
# change_time = current_time + 2000
all_sprites_list.update()
if mblk.rect.x > 200:
spd = -spd
if mblk.rect.x < 100:
spd = -spd
mblk.rect.x += spd
# - draws (without updates) -
screen.fill(WHITE)
all_sprites_list.draw(screen)
movblock.draw(screen)
pygame.display.flip()
# - FPS -
clock.tick(60)
# --- main ---
# - init -
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Car Racing")
# - game -
all_sprites_list = pygame.sprite.Group()
movblock = pygame.sprite.Group()
goto_next_level = level_1(screen, all_sprites_list, movblock)
#if goto_next_level:
# goto_next_level = level_2(screen, all_sprites_list, movblock)
#if goto_next_level:
# goto_next_level = level_3(screen, all_sprites_list, movblock)
# - exit -
pygame.quit()
sys.exit()
Related
I did a lot of research on how to delete a drawing on the screen after clicking it, and I couldn't do that
Try1 how to remove draw objects from pygame window?
Try2 How to remove a drawn circle in pygame? (without "going over" other things)
When clicked on the circle it will remove itself, and thus create another circle to be able to click.
import pygame, sys
from pygame.locals import *
from pygame import mixer
pygame.init()
musica = 'circles.mp3'
mixer.music.load(musica)
mixer.music.play()
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("OSU DA DEEP WEB")
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
screen.fill(WHITE)
#posição
width = 500 -30
height = 500 - 30
widthposition = random.randrange(width)
heightposition = random.randrange(width)
#sistema de pontos
points = 0
circle = pygame.draw.circle(screen, (0, 0, 0), (400, 300), 25)
def draw():
print('CLicked')
circle = pygame.draw.circle(screen, (0, 0, 0), (400, 300), 25)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
You have to check event to catch when left button was click and then you can draw white background and draw circle
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.BUTTON_LEFT:
screen.fill(WHITE)
draw(screen)
But this still doesn't check if you clicked on circle.
Minimal working code.
import sys
import random
import pygame
# --- constants ---
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
WIDTH = 500
HEIGHT = 500
# --- functions ---
def draw(screen):
print('Clicked')
x = random.randrange(30, WIDTH-30)
y = random.randrange(30, HEIGHT-30)
pygame.draw.circle(screen, (0, 0, 0), (x, y), 25)
# --- main ---
musica = 'circles.mp3'
pygame.init()
pygame.mixer.music.load(musica)
pygame.mixer.music.play()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen.fill(WHITE)
draw(screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.BUTTON_LEFT:
screen.fill(WHITE)
draw(screen)
pygame.display.flip()
draw.circle gives object pygame.Rect() with rectangle area used by circle and you could use it to "check collision" with mouse position
circle_rect = pygame.draw.circle(...)
#... code ...
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.BUTTON_LEFT:
if circle_rect.collidepoint(even.pos):
screen.fill(WHITE)
draw(screen)
and it check position in rectangle area so it works better but not ideal
import sys
import random
import pygame
# --- constants ---
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
WIDTH = 500
HEIGHT = 500
# --- functions ---
def draw(screen):
print('Clicked')
x = random.randrange(30, WIDTH-30)
y = random.randrange(30, HEIGHT-30)
circle_rect = pygame.draw.circle(screen, (0, 0, 0), (x, y), 25)
return circle_rect
# --- main ---
musica = 'circles.mp3'
pygame.init()
pygame.mixer.music.load(musica)
pygame.mixer.music.play()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen.fill(WHITE)
circle_rect = draw(screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.BUTTON_LEFT:
if circle_rect.collidepoint(event.pos):
screen.fill(WHITE)
circle_rect = draw(screen)
pygame.display.flip()
There is function to check collicion in cicle area but it works for two pygame.sprite.Sprite, not for single pygame.Rect and mouse position (single point). You would have to convert mouse position and pygame.Rect to pygame.sprite.Sprite but it is to complex for this problem.
Doc: pygame.sprite.collide_circle()
OR you can use pygame.math.Vector2.distance_to() to calculate distance between mouse position and center of circle - it has to be equal or smaller then 25
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.BUTTON_LEFT:
pos = pygame.math.Vector2(event.pos)
if pos.distance_to(circle_rect.center) <= 25:
screen.fill(WHITE)
circle_rect = draw(screen)
import sys
import random
import pygame
# --- constants ---
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
WIDTH = 500
HEIGHT = 500
# --- functions ---
def draw(screen):
print('Clicked')
x = random.randrange(30, WIDTH-30)
y = random.randrange(30, HEIGHT-30)
circle_rect = pygame.draw.circle(screen, (0, 0, 0), (x, y), 25)
return circle_rect
# --- main ---
musica = 'circles.mp3'
pygame.init()
pygame.mixer.music.load(musica)
pygame.mixer.music.play()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen.fill(WHITE)
circle_rect = draw(screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.BUTTON_LEFT:
pos = pygame.math.Vector2(event.pos)
if pos.distance_to(circle_rect.center) <= 25:
screen.fill(WHITE)
circle_rect = draw(screen)
pygame.display.flip()
EDIT:
If you will want to add other object which will move then you will have to organize it in different way. In while True in every loop you will have to clear screen and draw again all object. And this needs more changes
import sys
import random
import pygame
# --- constants ---
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
WIDTH = 500
HEIGHT = 500
FPS = 25
# --- classes ---
class Circle():
def __init__(self, x, y, r, color, random=False):
self.x = x
self.y = y
self.r = r
self.color = color
# vector to check collision
self.center = pygame.math.Vector2((x, y))
if random:
self.set_random_position()
def draw(self, screen):
#pygame.draw.circle(screen, self.color, (self.x, self.y), self.r)
pygame.draw.circle(screen, self.color, self.center, self.r)
def check_collision(self, position):
return self.center.distance_to(position) <= self.r
def set_random_position(self):
self.x = random.randint(30, WIDTH-30) # `randint` better then `randrange`
self.y = random.randint(30, HEIGHT-30) # `randint` better then `randrange`
# vector to check collision
self.center = pygame.math.Vector2(self.x, self.y)
def move_random(self):
dx = random.randint(-5, 5) # `randint` better then `randrange`
dy = random.randint(-5, 5) # `randint` better then `randrange`
self.x += dx
self.y += dy
if self.x < self.r:
self.x = self.r
if self.x > WIDTH-self.r:
self.x = WIDTH-self.r
if self.y < self.r:
self.y = self.r
if self.y > HEIGHT-self.r:
self.y = HEIGHT-self.r
self.center = pygame.math.Vector2(self.x, self.y)
# --- functions ---
# ... empty ...
# --- main ---
musica = 'circles.mp3'
pygame.init()
#pygame.mixer.music.load(musica)
#pygame.mixer.music.play()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# create circle
circle = Circle(0, 0, 25, RED, random=True)
# create other objects and keep on list
others = [Circle(0, 0, 10, GREEN, random=True) for _ in range(100)]
clock = pygame.time.Clock()
while True:
# - events - (without draws)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == pygame.BUTTON_LEFT:
if circle.check_collision(event.pos):
# only change `x,y` without drawing
circle.set_random_position()
# - updates - (without draws)
# move other objects from list
for item in others:
item.move_random()
# - draws - (without events and updates)
# clear screen
screen.fill(WHITE)
# draw circle
circle.draw(screen)
# draw other objects from list
for item in others:
item.draw(screen)
# send on monitor
pygame.display.flip()
# - FPS -
# to keep the same speed on different computers (with different CPU speed)
clock.tick(FPS)
This code is mostly just the generic start up of a pygame window but I'm trying to make it so the object moves (the planet object I've made) in the orbit around the sun object I've made for the coordinates I've given it. I know the x and y values are updating but I don't understand why the object doesn't move.
#import the library
import pygame
import math
#classes
class button:
def _init_ (self,screen, colour, x, y, width,height, letter):
self.screen = screen
self.colour = colour
self.x = x
self.y = y
self.width = width
self.height = height
self.letter = letter
self.radius = radius
def draw(self):
pygame.draw.rect(self.screen, self.colour,(self.x,self.y, self.width, self.height))
if self.letter!= '+' and self.letter!= '-':
font = pygame.font.SysFont('agencyfb',15,True,False)
else:
font = pygame.font.SysFont('agencyfb',25,True,False)
text = font.render(self.letter, True, black)
text_rect = text.get_rect(center=(self.x+self.width/2,self.y+self.height/2))
screen.blit(text, text_rect)
class orbit:
def __init__(self,screen,colour,x,y,radius,width):
self.screen = screen
self.colour = colour
self.x = x
self.y = y
self.width = width
self.radius = radius
def draw_circle(self):
pygame.draw.circle(self.screen,self.colour,(self.x,self.y),self.radius,self.width)
#define colours
##Sun = pygame.draw.circle(screen,Sun,[1000,450],100,0)
Black = (0,0,0)
White = (255,255,255)
Green = (0,255,0)
Red = (255,0,0)
Blue = (0,0,255)
Sun = (255,69,0)
Sun = []
Planet = []
#initialise the engine
pygame.init()
#Opening a window
size = (1920,1080)
screen = pygame.display.set_mode(size)
#set window title
pygame.display.set_caption("Orbit Simulator")
#loop unti the user clicks the close button
done = False
#
x=1000
y=450
Sun.append(orbit(screen,Red,1000,450,100,0))
Planet.append(orbit(screen,White,x,y,50,0))
#
#used to manage how fast the screen updates
clock = pygame.time.Clock()
#------ Main program Loop ------
while not done:
#--- Main event loop
for event in pygame.event.get(): #user did something
if event.type == pygame.QUIT: #if user clicked close
done = True #flag that we are done and exit the loop
#------ Game logic should go here ------
#------ Drawing code should go here -------
#first, clear the screen to white. Don't put other drawing commands above this or they will be erased with this command.
screen.fill(Black)
for i in Sun:
i.draw_circle()
for i in Planet:
r=150
angle=0
count = 0
while angle <= 360:
angle_radians = math.radians(angle)
x = int(math.cos(angle_radians)*r)
y = int(math.sin(angle_radians)*r)
angle +=1
count +=1
print(count)
x+=1000
y+=450
pygame.draw.circle(screen,White,[x,y],10,0)
print("Position [",x,",",y,"]")
#update the screen
pygame.display.flip()
#------ Limit to 60 frames per second ------
clock.tick(60)
#------ When the loop ends, quit ------
pygame.quit()
You can make an object rotate around another by using trigonometry or vectors. With vectors you just have to rotate a vector which defines the offset from the rotation center each frame and add it to the position vector (self.pos which is the rotation center) to get the desired self.rect.center coordinates of the orbiting object.
import pygame as pg
from pygame.math import Vector2
class Planet(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
self.image = pg.Surface((40, 40), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('dodgerblue'), (20, 20), 20)
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos)
self.offset = Vector2(200, 0)
self.angle = 0
def update(self):
self.angle -= 2
# Add the rotated offset vector to the pos vector to get the rect.center.
self.rect.center = self.pos + self.offset.rotate(self.angle)
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
planet = Planet(screen_rect.center, all_sprites)
yellow = pg.Color('yellow')
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
all_sprites.update()
screen.fill((30, 30, 30))
pg.draw.circle(screen, yellow, screen_rect.center, 60)
all_sprites.draw(screen)
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()
im currently learning to program in python (first language) and im trying to create a clicker game, however, while doing the click function i got this error:
line 73, in <module>
if player.collidepoint(event.pos):
AttributeError: 'Player' object has no attribute 'collidepoint'
it seems that my "player" object (the clickable object) doesnt have a rect ? but after looking it up for hours i could not find an answer
this is my game code
import pygame
from os import path
img_dir = path.join(path.dirname(__file__), "img")
width = 500
height = 600
fps = 30
# Cores
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0 ,0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
# Iniciar o game
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Trojan Clicker")
clock = pygame.time.Clock()
font_name = pygame.font.match_font("arial")
def draw_text(surf, text, size, x , y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, white)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((75, 75))
self.image.fill(red)
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = width / 2
self.rect.bottom = height / 2
self.speedx = 0
def update(self):
self.speedx = 0
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
clicks = 0
# Loop
running = True
while running:
# Fps
clock.tick(fps)
# Eventos
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# 1 is the left mouse button, 2 is middle, 3 is right.
if event.button == 1:
# `event.pos` is the mouse position.
if player.collidepoint(event.pos):
# Increment the number.
number += 1
# Updates
all_sprites.update()
# Draw / render X
screen.fill(black)
all_sprites.draw(screen)
draw_text(screen, str(clicks), 18, width / 2, 10)
# Depois de desenhar tudo, "flip" o display
pygame.display.flip()
pygame.quit()
Some comments are in portuguese btw, sorry about that
Thanks in advance for everyone who helps
Change the line
if player.collidepoint(event.pos):
to
if player.rect.collidepoint(event.pos):
I have an array with all the sprites inside of them, and in the loop, if the sprite is clicked then the sprite is killed.
If I click the sprites from left to right, everything is fine. They are cleared one by one independent of each other. But, if I click the sprite most right of the screen, it kills everything to the left of it.
Full code because I've no idea where the important region might be.
import pygame
from pygame.locals import *
import random
pygame.init()
level = 1
cooldown = 1000
class test(pygame.sprite.Sprite):
def __init__(self, w):
super().__init__()
self.image = pygame.Surface([600,600])
self.rect = self.image.get_rect()
self.image = pygame.image.load("index.png")
self.image = pygame.transform.scale(self.image, (w,w))
self.last = pygame.time.get_ticks()
screen = pygame.display.set_mode([600,600])
clock = pygame.time.Clock()
background = pygame.Surface(screen.get_size())
background.fill([0,0,0])
rX = random.randint(0, 500)
rX2 = random.randint(0, 500)
screen.blit(background, (0,0))
sp = []
all_sprites_list = pygame.sprite.Group()
a_1 = test(random.randint(40,60))
a_1.rect.x = rX
a_1.rect.y = -400
sp.append(a_1)
all_sprites_list.add(a_1)
#The steps for a_1 is repeated to make variables a_2, a_3, a_4...
Running = True
while Running:
now = pygame.time.get_ticks()
for x in sp:
x.rect.y+=level
m_pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == QUIT:
Running = False
if event.type == MOUSEBUTTONDOWN:
for s in sp:
if s.rect.collidepoint(m_pos):
s.kill()
pygame.display.flip()
all_sprites_list.clear(screen, background)
all_sprites_list.draw(screen)
all_sprites_list.update()
clock.tick(60)
Problem is because in class you create Surface with size (600, 600) and you assign this size to self.rect. After that you load image but you forgot to assign its size to self.rect so program always check mouse click with size (600, 600) and when you click most right item then all rect (600, 600) are in area of click.
But you don't have to create Surface if you load image.
My version with many changes.
I removed list sp because I can use all_sprites_list
When it sends MOUSEBUTTONDOWN event then mouse position is in event.pos so I don't need pygame.mouse.get_pos()
import pygame
import random
# --- classes --- (UpperCaseNames)
class Test(pygame.sprite.Sprite):
def __init__(self, w):
super().__init__()
self.image = pygame.image.load("index.png")
self.image = pygame.transform.scale(self.image, (w, w))
self.rect = self.image.get_rect()
self.last = pygame.time.get_ticks()
# --- main ----
level = 1
cooldown = 1000
# - init -
pygame.init()
screen = pygame.display.set_mode((600, 600))
# - items -
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
screen.blit(background, (0,0))
all_sprites_list = pygame.sprite.Group()
for x in range(10):
item = Test(random.randint(40, 60))
item.rect.x = 40 * random.randint(0, 10)
item.rect.y = 0
all_sprites_list.add(item)
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
now = pygame.time.get_ticks()
for item in all_sprites_list:
item.rect.y += level
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
for item in all_sprites_list:
if item.rect.collidepoint(event.pos):
item.kill()
pygame.display.flip()
all_sprites_list.clear(screen, background)
all_sprites_list.draw(screen)
all_sprites_list.update()
clock.tick(30)
pygame.quit()
this program makes boxes move, but i need to make them move randomly and indepensent of each other,
by calling the method "rect.move()" for each of the boxes but i don't know how to do that can u help me.
ex. of how it should not look like: https://youtu.be/D7rkcA0-BR0
import pygame
import random
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
class Rect():
def __init__(self):
self.rectXPos = 0
self.rectYPos = 0
self.height = 0
self.width = 0
self.changeX = 0
self.changeY = 0
self.x = 0
self.y = 0
def move(self):
self.x += self.changeX
self.y += self.changeY
def draw(self,screen):
pygame.draw.rect(screen,RED,[self.x + self.rectXPos, self.y + self.rectYPos, self.height,self.width])
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
myList =[]
for i in range(10):
rect = Rect()
rect.rectXPos = random.randrange(0,700)
rect.rectYPos = random.randrange(0,500)
rect.height = random.randrange(20,70)
rect.width = random.randrange(20,70)
rect.changeX = random.randrange(-3,3)
rect.changeY = random.randrange(-3,3)
myList.append([rect.rectXPos , rect.rectYPos, rect.height, rect.width, rect.changeX, rect.changeY])
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Game logic should go here
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(WHITE)
for i in range(10):
rect.rectXPos = myList[i][0]
rect.rectYPos = myList[i][1]
rect.height = myList[i][2]
rect.width = myList[i][3]
rect.changeX = myList[i][4]
rect.changeY= myList[i][5]
rect.draw(screen)
rect.move()
# --- Drawing code should go here
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()
this is premade code from http://programarcadegames.com/
What you need is for the Rect attributes to be randomly generated with the random module. I have modified the code you gave an made some changes.
Firstly I changed the draw method so that it just draws at the x and y values.
The biggest change was that instead of the big complicated myList list in your code, I just stored 10 Rect objects in a list called myRects which I think is much simpler.
You can fiddle around some more with the number generation from around Line 45-52. You can read a bit more on the random.randrange() function here: https://docs.python.org/3/library/random.html#functions-for-integers
I hope this answer helped you! If you have any further questions please post a comment below!
import pygame
import random
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
class Rect():
def __init__(self):
self.x = 0
self.y = 0
self.height = 0
self.width = 0
self.changeX = 0
self.changeY = 0
def move(self):
self.x += self.changeX
self.y += self.changeY
def draw(self, screen):
pygame.draw.rect(screen, RED, [self.x, self.y, self.width, self.height], 0)
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done = False
clock = pygame.time.Clock()
myRects = []
for i in range(10):
rect = Rect()
rect.x = random.randrange(0, 700)
rect.y = random.randrange(0, 700)
rect.width = random.randrange(20, 70)
rect.height = random.randrange(20, 70)
rect.changeX = random.randrange(-3, 3)
rect.changeY = random.randrange(-3, 3)
myRects.append(rect)
print(myRects)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(WHITE)
for rect in myRects:
rect.draw(screen)
rect.move()
pygame.display.update()
clock.tick(10)
pygame.quit()
quit()