PyGame simulation will not render - python

I'm working on a natural selection simulator. I've asked a question pertaining to it before (How to test if areas overlap). Now I have a version of the code that I'd like to run but it does not. Instead of getting an error message, however, the window opens, but the simulation (that otherwise seems to me as though it should run) does not run.
It would seem to me that the problem has something to do with the initial spawn information not being passed into the main class for my organisms, but I can't be sure.
If anyone could identify why the code isn't working and tell me what I'd need to correct I'd really appreciate it.
UPDATE
The command line returns information that the code is running, so the problem must be that the display isn't working. It definitely has to do with my sprites because removing
self.image.fill(colors['hotpink2'])
self.image.set_colorkey(colors['hotpink2'])
self.mask = pygame.mask.from_surface(self.image)
from the init causes the rectangles to display briefly.
The updated code is as follows:
# Import libraries
import pygame, random, numpy
# Initialize PyGame
pygame.init()
# Define colors
colors = pygame.color.THECOLORS
# Set window dimensions
mapWidth = 800
mapHeight = 800
size = [mapWidth, mapHeight]
screen = pygame.display.set_mode(size)
# Display program title in window
pygame.display.set_caption("Natural Selection Game")
# Loop until user closes window
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Generate IDs for organisms
def id_generator():
i = 0
while True:
i += 1
yield i
ids = id_generator()
# Prevent organisms from colliding with self
def collide(a, b):
if a.id == b.id:
return False
return pygame.sprite.collide_mask(a, b)
# ----- Organisms -----
class Organism(pygame.sprite.Sprite):
def __init__(self, width, height, x, y, changeX, changeY, lifespan, species):
# Make PyGame Sprite
pygame.sprite.Sprite.__init__(self, organisms)
self.id = next(ids)
# Set dimensions
self.width = width
self.height = height
# Set starting point
self.x = x
self.y = y
# Set motion type
self.changeX = changeX
self.changeY = changeY
# Set lifespan
self.lifespan = lifespan
# Set species
self.species = species
if species == 'paper':
self.color = colors['red']
elif species == 'rock':
self.color = colors['green']
elif species == 'scissors':
self.color = colors['blue']
# Set age at birth
self.age = 0
# Recognize collisions as to produce only one offspring
self.colliding = set()
# Sprite body
self.rect = pygame.rect.Rect(x, y, width, height)
self.image = pygame.Surface((width, height))
self.image.fill(colors['hotpink2'])
self.image.set_colorkey(colors['hotpink2'])
# Draw
pygame.draw.ellipse(self.image, self.color, [self.x, self.y, self.width, self.height])
self.mask = pygame.mask.from_surface(self.image)
# Randomly generate traits for first generation
#classmethod
def initialSpawn(cls):
# Set dimensions for first generation
width = random.randrange(5,50)
height = random.randrange(5,50)
# Set starting point for first generation
x = random.randrange(0 + width, 800 - width)
y = random.randrange(0 + height, 800 - height)
# Set motion type for first generation
changeX = random.randrange(0,6)
changeY = random.randrange(0,6)
# Set lifespan for first generation
lifespan = random.randrange(300,700)
# Set species for first generation
species = random.choice(['paper', 'rock', 'scissors'])
return cls(width, height, x, y, changeX, changeY, lifespan, species)
# Inherit and/or mutate traits for offspring
def reproduce(self, collidee):
# Set dimensions for offspring
width = random.choice(self.width, collidee.height)
height = random.choice(self.width, collidee.height)
# Set starting points for offspring
x = random.choice(self.x, collidee.x)
y = random.choice(self.y, collidee.y)
# Set motion type for offspring
changeX = random.choice(self.changeX, collidee.changeX)
changeY = random.choice(self.changeY, collidee.changeY)
# Set lifespan for offspring
lifespan = numpy.mean(self.lifespan, collidee.lifespan)
# Set species for offspring
species = self.species
return width, height, x, y, changeX, changeY, species
def update(self):
# Update age
self.age += 1
# Update movement
self.rect.move_ip(self.changeX, self.changeY)
# Manage bouncing
if self.rect.left < 0 or self.rect.right > mapWidth:
self.changeX *= -1
if self.rect.top < 0 or self.rect.bottom > mapHeight:
self.changeY *= -1
# Death from aging
if self.age > self.lifespan:
print (self.id, ' died of age')
self.kill()
return
# Check if collided with another organism of same species for mating or predation
collidee = pygame.sprite.spritecollideany(self, organisms, collide)
# Check the prerequisites for mating:
# - Not already colliding
# - Same species
# - No overpopulation
if collidee and not collidee.id in self.colliding and self.species == collidee.species and len(self.organisms) < 100:
# Keep track of the current collision, so this code is not triggerd throughout duration of collision
self.colliding.add(collidee.id)
collidee.colliding.add(self.id)
print (self.id, ' mated with ', collidee.id)
# The fun part! ;)
self.reproduce(collidee)
# Check the prerequisites for predation:
# - Not already colliding
# - Different species
elif collidee and not collidee.id in self.colliding and self.species != collidee.species:
if self.species == 'paper' and collidee.species == 'rock':
collidee.kill()
self.lifespan += 100
elif self.species == 'rock' and collidee.species == 'scissors':
collidee.kill()
self.lifespan += 100
elif self.species == 'scissors' and collidee.species == 'paper':
collidee.kill()
self.lifespan += 100
else:
# Organism is single and ready to mingle
self.colliding = set()
# Organism group
organisms = pygame.sprite.Group()
# Initial spawner
for i in range(15):
organisms.add(Organism.initialSpawn())
# ----- Simulation -----
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Logic
organisms.update()
# Draw screen
screen.fill(colors['white'])
# Draw organisms
organisms.draw(screen)
# FPS
clock.tick(60)
# Update drawings
pygame.display.flip()
pygame.quit()

You're calling a class method Organism.initialSpawn() which returns width, height, x, y, changeX, changeY, lifespan, species but you don't do anything with it. It just goes directly to garbage collection.
You're probably trying to create a new organism from the variables you're given, and add those in the pygame.sprite.Group. This can be done like this:
# Organism group
organisms = pygame.sprite.Group()
# Initial spawner
for i in range(15):
temp = Organims(*Organism.initialSpawn())
organisms.add(temp)
Although, your initialSpawn() method has the wrong syntax. All methods in a class needs self as first parameter unless something else is specified. I would make it a class method and instead of returning a bunch of variables that you use to create a new Organism, you just return a new Organism directly.
class Organism(pygame.sprite.Sprite):
# Randomly generate traits for first generation
#classmethod
def initialSpawn(cls):
# Set dimensions for first generation
width = random.randrange(5,50)
height = random.randrange(5,50)
# Set starting point for first generation
x = random.randrange(0 + width, 800 - width)
y = random.randrange(0 + height, 800 - height)
# Set motion type for first generation
changeX = random.randrange(0,6)
changeY = random.randrange(0,6)
# Set lifespan for first generation
lifespan = random.randrange(300,700)
# Set species for first generation
species = random.choice(['paper', 'rock', 'scissors'])
if species == 'paper':
color = colors['red']
elif species == 'rock':
color = colors['green']
elif species == 'scissors':
color = colors['blue']
return cls(width, height, x, y, changeX, changeY, lifespan, species)
# Organism group
organisms = pygame.sprite.Group()
# Initial spawner
for i in range(15):
organisms.add(Organism.initialSpawn())
EDIT: Okay, I looked closer on your code and you have lots of errors.
Need to put self in your __init__ method as first parameter.
In __init__, you have an unidentified function add(organisms). Remove that.
In __init__, you're creating a color variable based on a condition but you don't use it. Instead you're trying to use an attribute self.color which you don't have. Change the variables color to attributes self.color.
In __init__, you're trying to access the class Surface by typing pygame.surface.Surface. There's no module surface and the class is in the pygame module. Just type pygame.Surface instead.
Add a decorator #classmethod and cls for InitialSpawn as I did above.
In InitialSpawn, remove the if-statements where you set color, because you don't need it. It's just unnecessary code.
In update, change self.change_x and self.change_y to self.changeX and self.changeY in reproduce metod. Be consistent.
In update, change self.organisms to just organisms. It's a global variable and not an attribute.
In update, mate is an undefined variable. Don't know what you're trying to do so I don't know how to fix that.
Those were the errors I found just using Pycharm. Change to an IDE that can detect errors (like Pycharm) when coding and it'll help you tremendously.

Related

Issue while saving rects, it crash my game

I have this code that saves the position and the rect from an object (usally small, like a drawing) and then spawns them (blits) in my screen. I encountered that if I put too many objects or too big, I´m guessing the rects collide and then the game crashes, but also, sometimes, even if I have not many objects, it can crash because this occurance.
How could I solve this problem? I´m guessing adding an if sentence so it checks that is not as near as to crash the game or something like that, where I save the rects of the images is in the for i in self.game_images: :
class GameScene(Scene):
def __init__(self, game, images, main_image, next_scene):
super().__init__(next_scene)
self.game = game
self.main_image = main_image
self.game_images = images
# Fade effect set-up
self.fade = False
self.fade_time = 0
self.current_alpha = 255
self.part = 1
self.record_text = font.render('Atiende',True, PURPLE)
self.correct_image_rect = None
# Trying to use colliderect so it doesnt overlap
# this is the same code as before but adapted to use the gameimage class and the rects stored there
self.rects = []
for i in self.game_images:
position_set = False
while not position_set:
x = random.randint(100,950)
y = random.randint(100,600)
i.rect.x = x
i.rect.y = y
margin = 5
rl = [rect.inflate(margin*2, margin*2) for rect in self.rects]
if len(self.rects) == 0 or i.rect.collidelist(rl) < 0:
self.rects.append(i.rect)
position_set = True
# this makes a number and object pair, and allows us to set the correct rects for the correct gameimage classes
for i, rect in enumerate(self.rects):
self.game_images[i].rect = rect
# this is the fade stuff from before that was in draw. It really belongs here tbh
def update(self, dt):
if self.part == 1 and self.fade:
self.fade_time += dt
if self.fade_time > fade_timer:
self.fade_time = 0
self.main_image.set_alpha(self.current_alpha)
self.record_text.set_alpha(self.current_alpha)
# Speed whichin the image dissapears
self.current_alpha -= 5
if self.current_alpha <= 0:
self.fade = False
self.part = 2
else:
# we reset the main image alpha otherwise it will be invisible on the next screen (yeah, this one caught me out lol!)
self.main_image.set_alpha(255)
# draw is similar to before, but a bit more streamlined as the fade stuff is not in update
def draw(self, screen):
super().draw(screen)
if self.part == 1:
screen.blit(self.record_text, (550, 20))
screen.blit(self.main_image.image, (580, 280))
else:
# Second half
text2 = font.render('¿Qué has visto?',True, PURPLE)
screen.blit(text2, (400,5))
# Show all similar images
for game_image in self.game_images:
game_image.draw(screen)
# We associate the correct rect to the correct image, to pass it later to the CORRECT Screen
self.correct_image_rect = self.game_images[self.game_images.index(self.main_image)].rect
# again we pass the event to the game object the same as with the other classes
def get_event(self, event):
if self.part == 2:
if self.game.level == 13:
self.game.game_over = True
if self.correct_image_rect.collidepoint(event.pos):
return 'CORRECT'
for rect in self.rects:
if not self.correct_image_rect.collidepoint(event.pos) and rect.collidepoint(event.pos):
return 'INCORRECT'
You end up in an infinite loop, because you try to add all the objects at once. If the algorithm cannot find a random position for an object that does not collide with another object, the loop does not terminate.
Create the objects one by one in the update method. The update method is continuously called in the application loop. Create on object per frame. There may be times when not all objects can be generated, but you can avoid the infinite loop:
class GameScene(Scene):
def __init__(self, game, images, main_image, next_scene):
super().__init__(next_scene)
self.game = game
self.main_image = main_image
self.game_images = images
# Fade effect set-up
self.fade = False
self.fade_time = 0
self.current_alpha = 255
self.part = 1
self.record_text = font.render('Atiende',True, PURPLE)
self.correct_image_rect = None
# Trying to use colliderect so it doesnt overlap
# this is the same code as before but adapted to use the gameimage class and the rects stored there
self.rects = []
# this is the fade stuff from before that was in draw. It really belongs here tbh
def update(self, dt):
if len(self.rects) < len(self.game_images):
i = len(self.rects)
x = random.randint(100,950)
y = random.randint(100,600)
self.game_images[i].rect.x = x
self.game_images[i].rect.y = y
margin = 5
rl = [rect.inflate(margin*2, margin*2) for rect in self.rects]
if len(self.rects) == 0 or self.game_images[i].rect.collidelist(rl) < 0:
self.rects.append(self.game_images[i].rect)
if self.part == 1 and self.fade:
self.fade_time += dt
if self.fade_time > fade_timer:
self.fade_time = 0
self.main_image.set_alpha(self.current_alpha)
self.record_text.set_alpha(self.current_alpha)
# Speed whichin the image dissapears
self.current_alpha -= 5
if self.current_alpha <= 0:
self.fade = False
self.part = 2
else:
# we reset the main image alpha otherwise it will be invisible on the next screen (yeah, this one caught me out lol!)
self.main_image.set_alpha(255)
# [...]

How do I make a variation of a pre-existing class?

I am currently creating a game where each enemy has a specific AI. Each enemy is given an AI and statistic changes(health, damage, etc.). However, I also need to assign these enemies to an NPC slot before the game's main loop is run. The problem comes in because I need to identify the x-value and y-value of the enemy so the game knows where to place them. If I keep the x- and y-values as a parameter in the AI class, I have to repeatedly redefine them when I put them in an NPC slot(my slots are labelled 'NPC1', 'NPC2', and so on).
class PeonGrounded(object):
def __init__(self, x, y, width, height, vel, aggrorange, health=20.0, kb=10, jumplength=10.0, jumpheight=2.0,
jumpmomentum=0.25):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = vel
self.direction = 'R'
self.standing = True
self.health = health
self.canMove = True
self.jumplength = jumplength
self.isJump = False
self.canJump = True
self.kb = kb
self.jumpheight = jumpheight
self.jumpmomentum = jumpmomentum
self.neg = 1
self.aggrorange = aggrorange
self.aggroed = False
I now want to be able to define some parameters(everything but x, y, width, height) but leave others blank for defining later.

Pygame pixel perfect collision not working as expected

I am creating tetris using pygame. i want to use collision detection so that when the shape in play comes into contact with any other previously played shapes, i can stop the shape, as per the logic of tetris. i came across pixel perfect collision using masks. i have followed some tutorials online, however the pixel detection returns true every time a new shape comes into play, not when any shapes collide. sorry in advance for the long code, its the bare minimum for the code to actually and still containing the game element of it. i think there is something wrong with my approach which is causing this error. I basically have a function that everytime the shape in play comes into contact with the 'floor' that shape is held in that position and a new shape is created. i think ive overcomplicated it, in turn creating this error. thanks in advance
import pygame
import sys
import shapelogic
pygame.init()
screensize = width, height = 800, 595
screen = pygame.display.set_mode(screensize)
background_image =pygame.image.load("/Users/marceason/PycharmProjects/Tetris/Wooden_background.jpg").convert_alpha()
myshape = 0
stop_movement = 0
blit_count = 0
stored_shapes = pygame.sprite.Group()
stored_shapes_with_coords = []
extra_blit_required = False
index = 0
count = 0
listofshapes = []
class shapemanager():
def __init__(self):
self.listofshapes = []
def create_another_instance(self):
global count
count += 1
string = "Shape_{0},".format(count)
another_shape = Shape(string)
self.listofshapes.append(another_shape)
global index
object = self.listofshapes[index]
index += 1
return object
def load_shape(self):
shape = self.create_another_instance()
shape.load_shapes()
class Shape(pygame.sprite.Sprite):
def __init__(self, name):
pygame.sprite.Sprite.__init__(self)
self.name = name
self.x = 50
self.y = 100
self.move_event = pygame.USEREVENT + 1
self.reached_bottom_event = pygame.USEREVENT + 2
self.one_sec_timer = 1000
self.half_sec_timer = 500
self.reachbottomflag = False
self.movement_possible = True
self.image = pygame.image.load(
"/Users/marceason/PycharmProjects/Tetris/Tetris_Shapes/Green_Shape_1_Position_1.png")
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
def move_shape(self):
if self.movement_possible:
key_input = pygame.key.get_pressed()
if key_input[pygame.K_LEFT]:
self.x -= 16
if key_input[pygame.K_RIGHT]:
self.x += 16
if not self.reachbottomflag:
if key_input[pygame.K_DOWN]:
self.y += 16
def reachbottom(self):
if self.y >= 560:
self.reachbottomflag = True
def no_movement_possible(self):
self.movement_possible = False
def assign_shape():
global myshape
global stop_movement
myshape = sl.create_another_instance()
pygame.time.set_timer(myshape.move_event, myshape.one_sec_timer)
stop_movement = pygame.time.set_timer(myshape.reached_bottom_event, myshape.half_sec_timer)
def blit_used_shapes():
global screen
global blit_count
blit_count = len(stored_shapes_with_coords)
local_count = 0
while local_count < blit_count:
screen.blit(stored_shapes_with_coords[local_count][0], (stored_shapes_with_coords[local_count][1], stored_shapes_with_coords[local_count][2]))
local_count += 1
sl = shapemanager()
##### HERE IS THE PIXEL DETECTION #####
result = pygame.sprite.spritecollide(myshape, stored_shapes, False, pygame.sprite.collide_mask)
## Main loop ##
assign_shape()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
screen.blit(background_image, (0, 0))
screen.blit(myshape.image, (myshape.x, myshape.y))
myshape.move_shape()
key_input = pygame.key.get_pressed()
if key_input[pygame.K_SPACE]:
myshape.rotate_shape()
myshape.reachbottom()
if myshape.reachbottomflag:
if event.type == myshape.reached_bottom_event:
myshape.no_movement_possible()
stored_shape_tuple = [myshape.image, myshape.x, myshape.y]
stored_shapes_with_coords.append(stored_shape_tuple)
stored_shapes.add(myshape)
extra_blit_required = True
assign_shape()
####### PIXEL DETECTION IS HERE IN FOR LOOP ####
if result:
print("this should only execute when two shapes touch!!")
if extra_blit_required:
blit_used_shapes()
pygame.display.update()
The issue is that you are not updating the sprites rect attribute. The sprites rects all have position (0, 0) (since you do not set it in the call to self.image.get_rect()) and as a result the masks will all overlap and collide.
If you read the docs for pygame.sprite.collide_mask you will note that it says that your sprites need to have mask and rect attributes. You have a rect in your sprite and you set it in the __init__(), but you do not keep it updated when you move the sprite. You just change the x and y attributes without adjusting the rect position. The reason that the collide_mask wants a rect is that it uses that to determine the offset parameter for the pygame.mask.Mask.overlap() call that it uses. The important thing to realize is that masks themselves do not have a position, they need the rects to determine the relative positions of the masks.
This is similar to images/surfaces not having a position and needing a rect to track that for them.
On a separate issue, the way you are blit'ing the sprites to the screen makes no sense. You are not using the abilities of the sprite groups to draw and worse you are keeping the image, x and y of the sprite in a separate list and not containing it in the sprite itself. You should go look at some examples of pygame sprite based code. There are lots of examples out there.

I can't blit the same object multiple times

I am currently making a Guitar Hero like game in python and I am trying to blit a note multiple times, but I can't seem to get it to work (the note is called Red)!
#Sprite Class
class Sprite(pygame.sprite.Sprite):
# What has to be passed when you create a sprite.
# image_file: The filename of the image.
# lacation: The initial location of where to draw the image.
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) # Call Sprite initializer
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.xSpeed = 15 # The default xSpeed
self.ySpeed = 15 # The default ySpeed
self.name = "Not Assigned"
self.speed = 10
self.direction = 0
def setSpeed(self, speed):
self.speed = speed
self.calcXYSpeeds()
def setDirection(self, direction):
self.direction = direction
self.calcXYSpeeds()
def calcXYSpeeds(self):
angleRadians = math.radians(self.direction) # Convert the direction to radians.
self.xSpeed= self.speed*math.cos(angleRadians)
self.ySpeed = self.speed*math.sin(angleRadians)
def move(self):
self.rect = self.rect.move(self.xSpeed,self.ySpeed)
def setDirectionTowards(self, (x,y)):
self.direction = math.degrees(math.atan2((self.rect.x - x), (self.rect.y - y)))
self.direction = 270 - self.direction
self.calcXYSpeeds()
#Object Variables
Red = Sprite("Red.png", (100,25))
note1 = Sprite("note1.jpg", (50,650))
#add sprite to group
notes = pygame.sprite.Group()
notes.add(Red)
# Create an clock to keep track time
clock = pygame.time.Clock()
#Scoring Variables
font=pygame.font.Font(None,50)
score=0
score_text=font.render(str(score),1,(225,225,225))
#other Variables
Red1=0
ySpeed = 10
running = 1
while (running==1):
# Sets the frame rate to 30 frames/second.
clock.tick(30)
# Gets any events like keyboard or mouse.
event = pygame.event.poll()
key=pygame.key.get_pressed()
#object random spawn
time = pygame.time.get_ticks()
if (time==30*(random.randint(0,1000))):
screen.blit(Red.image, Red.rect)
pygame.display.update()
#Object Movement
Red.rect.y = Red.rect.y + ySpeed
#Keys to complete notes
if key[pygame.K_a]and Red1==0 and pygame.sprite.collide_rect(Red, note1) == True:
font=pygame.font.Font(None,50)
score = score+1
score_text=font.render(str(score),1,(225,225,225))
Red1=1
#where i currently working to fix the problem
notes.Group.clear(screen, background)
notes.Group.update()
notes.draw(screen)
# Sets the exit flag if the X was clicked on the run window.
if event.type == pygame.QUIT:
running = 0
# Color the whole screen with a solid color.
screen.fill((0, 255, 255))
#prints objects
screen.blit(note1.image, note1.rect)
screen.blit(Red.image, Red.rect)
screen.blit(score_text,(500,50))
# Update the window.
pygame.display.update()
You are cleaning and updating the screen multiple times in the same cycle.
Clean the screen in every cycle is a correct approach, but you need to clean only once otherwise you are erasing everything you blitted before.
Also, for notes to persist between cycles you need to keep them in a collection, like notes, that way you blit every note in every cycle.
Keep in mind this order in your main loop:
Control FPS (clock.tick())
Check events
Update state (ie: add/remove notes collection, etc)
Clear screen
Draw current state (notes, static elements, etc)
Update display
Another important thing, don't use the same Sprite() nor the same Rectangle in different locations. If you want to draw another copy of the same image in a new location make a copy of the rectangle, then create a new Sprite() object.

Understand object orientated python - initialization of objects in pygame

I'm learning Object Orientated Python and understand the main principals of classes and creating objects from classes however I need something explained Re: the pygame code below. I'm struggling to get my head around what's happening when sprite lists are being created and the two lines of code under the code which creates the ball object (allsprites.add etc). In other words what are sprites and why are lists of them created? Why isn't the ball object just created from the class on its own? Why does it need to be added to a sprite list?? What's going on? Any explanation would be greatly appreciated.
"""
Sample Breakout Game
Sample Python/Pygame Programs
Simpson College Computer Science
http://programarcadegames.com/
http://simpson.edu/computer-science/
"""
# --- Import libraries used for this program
import math
import pygame
# Define some colors
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
# Size of break-out blocks
block_width = 23
block_height = 15
class Block(pygame.sprite.Sprite):
"""This class represents each block that will get knocked out by the ball
It derives from the "Sprite" class in Pygame """
def __init__(self, color, x, y):
""" Constructor. Pass in the color of the block,
and its x and y position. """
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create the image of the block of appropriate size
# The width and height are sent as a list for the first parameter.
self.image = pygame.Surface([block_width, block_height])
# Fill the image with the appropriate color
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
self.rect = self.image.get_rect()
# Move the top left of the rectangle to x,y.
# This is where our block will appear..
self.rect.x = x
self.rect.y = y
class Ball(pygame.sprite.Sprite):
""" This class represents the ball
It derives from the "Sprite" class in Pygame """
# Speed in pixels per cycle
speed = 10.0
# Floating point representation of where the ball is
x = 0.0
y = 180.0
# Direction of ball (in degrees)
direction = 200
width = 10
height = 10
# Constructor. Pass in the color of the block, and its x and y position
def __init__(self):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create the image of the ball
self.image = pygame.Surface([self.width, self.height])
# Color the ball
self.image.fill(white)
# Get a rectangle object that shows where our image is
self.rect = self.image.get_rect()
# Get attributes for the height/width of the screen
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
def bounce(self, diff):
""" This function will bounce the ball
off a horizontal surface (not a vertical one) """
self.direction = (180 - self.direction) % 360
self.direction -= diff
def update(self):
""" Update the position of the ball. """
# Sine and Cosine work in degrees, so we have to convert them
direction_radians = math.radians(self.direction)
# Change the position (x and y) according to the speed and direction
self.x += self.speed * math.sin(direction_radians)
self.y -= self.speed * math.cos(direction_radians)
# Move the image to where our x and y are
self.rect.x = self.x
self.rect.y = self.y
# Do we bounce off the top of the screen?
if self.y <= 0:
self.bounce(0)
self.y = 1
# Do we bounce off the left of the screen?
if self.x <= 0:
self.direction = (360 - self.direction) % 360
self.x = 1
# Do we bounce of the right side of the screen?
if self.x > self.screenwidth - self.width:
self.direction = (360 - self.direction) % 360
self.x = self.screenwidth - self.width - 1
# Did we fall off the bottom edge of the screen?
if self.y > 600:
return True
else:
return False
class Player(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player controls. """
def __init__(self):
""" Constructor for Player. """
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
self.width = 75
self.height = 15
self.image = pygame.Surface([self.width, self.height])
self.image.fill((white))
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
self.rect.x = 0
self.rect.y = self.screenheight-self.height
def update(self):
""" Update the player position. """
# Get where the mouse is
pos = pygame.mouse.get_pos()
# Set the left side of the player bar to the mouse position
self.rect.x = pos[0]
# Make sure we don't push the player paddle
# off the right side of the screen
if self.rect.x > self.screenwidth - self.width:
self.rect.x = self.screenwidth - self.width
# Call this function so the Pygame library can initialize itself
pygame.init()
# Create an 800x600 sized screen
screen = pygame.display.set_mode([800, 600])
# Set the title of the window
pygame.display.set_caption('Breakout')
# Enable this to make the mouse disappear when over our window
pygame.mouse.set_visible(0)
# This is a font we use to draw text on the screen (size 36)
font = pygame.font.Font(None, 36)
# Create a surface we can draw on
background = pygame.Surface(screen.get_size())
# Create sprite lists
blocks = pygame.sprite.Group()
balls = pygame.sprite.Group()
allsprites = pygame.sprite.Group()
# Create the player paddle object
player = Player()
allsprites.add(player)
# Create the ball
ball = Ball()
allsprites.add(ball)
balls.add(ball)
# The top of the block (y position)
top = 80
# Number of blocks to create
blockcount = 32
# --- Create blocks
# Five rows of blocks
for row in range(5):
# 32 columns of blocks
for column in range(0, blockcount):
# Create a block (color,x,y)
block = Block(blue, column * (block_width + 2) + 1, top)
blocks.add(block)
allsprites.add(block)
# Move the top of the next row down
top += block_height + 2
# Clock to limit speed
clock = pygame.time.Clock()
# Is the game over?
game_over = False
# Exit the program?
exit_program = False
# Main program loop
while exit_program != True:
# Limit to 30 fps
clock.tick(30)
# Clear the screen
screen.fill(black)
# Process the events in the game
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_program = True
# Update the ball and player position as long
# as the game is not over.
if not game_over:
# Update the player and ball positions
player.update()
game_over = ball.update()
# If we are done, print game over
if game_over:
text = font.render("Game Over", True, white)
textpos = text.get_rect(centerx=background.get_width()/2)
textpos.top = 300
screen.blit(text, textpos)
# See if the ball hits the player paddle
if pygame.sprite.spritecollide(player, balls, False):
# The 'diff' lets you try to bounce the ball left or right
# depending where on the paddle you hit it
diff = (player.rect.x + player.width/2) - (ball.rect.x+ball.width/2)
# Set the ball's y position in case
# we hit the ball on the edge of the paddle
ball.rect.y = screen.get_height() - player.rect.height - ball.rect.height - 1
ball.bounce(diff)
# Check for collisions between the ball and the blocks
deadblocks = pygame.sprite.spritecollide(ball, blocks, True)
# If we actually hit a block, bounce the ball
if len(deadblocks) > 0:
ball.bounce(0)
# Game ends if all the blocks are gone
if len(blocks) == 0:
game_over = True
# Draw Everything
allsprites.draw(screen)
# Flip the screen and show what we've drawn
pygame.display.flip()
pygame.quit()
You don't need to add the balls and blocks to sprite lists - it's just a matter of convenience. You could manually check each ball for a collision, but it's easier to just tell pygame to check them all for you
# See if the ball hits the player paddle
if pygame.sprite.spritecollide(player, balls, False):
# The 'diff' lets you try to bounce the ball left or right
# depending where on the paddle you hit it
diff = (player.rect.x + player.width/2) - (ball.rect.x+ball.width/2)
# Set the ball's y position in case
# we hit the ball on the edge of the paddle
ball.rect.y = screen.get_height() - player.rect.height - ball.rect.height - 1
ball.bounce(diff)
You could draw each thing to the screen separately on each frame, but it's easier just to tell pygame to do it for you:
# Draw Everything
allsprites.draw(screen)
Things can be in more than one list as required, for example a ball is added to the balls list so that you can easily check for collisions, but also added to the allsprites list so that you can easily draw everything on the screen
# Create the ball
ball = Ball()
allsprites.add(ball)
balls.add(ball)
Edit:
An important distinction is that allsprites is actually a sprite.Group. It has a list of sprites inside it, but it also has other methods like draw.
To address your question of "what is a Sprite", it's simply a thing that gets drawn on screen. pygame methods like sprite.Group.draw expect a list of things with certain attributes - such as update. The easiest way to make sure that you provide all of those attributes with the right names is to subclass Sprite, however this is also a (strongly recommended) convenience thing - for instance, this is from the pygame source code:
While it is possible to design sprite and group classes that don't
derive from the Sprite and AbstractGroup classes below, it is strongly
recommended that you extend those when you add a Sprite or Group
class.
So what specifically does subclassing Sprite get you? Let's take a look at the source. Here's how to find the source code for a python module:
>>> import pygame.sprite
>>> pygame.sprite.__file__
'c:\\Python27\\lib\\site-packages\\pygame\\sprite.py'
>>>
Every python module has a __file__ attribute that tells you where the source is located (well not quite every). If you open it up in your editor, and scroll down, you see the class definition for Sprite:
class Sprite(object):
"""simple base class for visible game objects
pygame.sprite.Sprite(*groups): return Sprite
The base class for visible game objects. Derived classes will want to
override the Sprite.update() and assign a Sprite.image and
Sprite.rect attributes. The initializer can accept any number of
Group instances to be added to.
When subclassing the Sprite, be sure to call the base initializer before
adding the Sprite to Groups.
"""
def __init__(self, *groups):
self.__g = {} # The groups the sprite is in
if groups: self.add(groups)
def add(self, *groups):
"""add the sprite to groups
Sprite.add(*groups): return None
Any number of Group instances can be passed as arguments. The
Sprite will be added to the Groups it is not already a member of.
"""
has = self.__g.__contains__
for group in groups:
if hasattr(group, '_spritegroup'):
if not has(group):
group.add_internal(self)
self.add_internal(group)
else: self.add(*group)
def remove(self, *groups):
"""remove the sprite from groups
Sprite.remove(*groups): return None
Any number of Group instances can be passed as arguments. The Sprite will
be removed from the Groups it is currently a member of.
"""
has = self.__g.__contains__
for group in groups:
if hasattr(group, '_spritegroup'):
if has(group):
group.remove_internal(self)
self.remove_internal(group)
else: self.remove(*group)
def add_internal(self, group):
self.__g[group] = 0
def remove_internal(self, group):
del self.__g[group]
def update(self, *args):
"""method to control sprite behavior
Sprite.update(*args):
The default implementation of this method does nothing; it's just a
convenient "hook" that you can override. This method is called by
Group.update() with whatever arguments you give it.
There is no need to use this method if not using the convenience
method by the same name in the Group class.
"""
pass
def kill(self):
"""remove the Sprite from all Groups
Sprite.kill(): return None
The Sprite is removed from all the Groups that contain it. This won't
change anything about the state of the Sprite. It is possible to continue
to use the Sprite after this method has been called, including adding it
to Groups.
"""
for c in self.__g.keys():
c.remove_internal(self)
self.__g.clear()
def groups(self):
"""list of Groups that contain this Sprite
Sprite.groups(): return group_list
Return a list of all the Groups that contain this Sprite.
"""
return self.__g.keys()
def alive(self):
"""does the sprite belong to any groups
Sprite.alive(): return bool
Returns True when the Sprite belongs to one or more Groups.
"""
return (len(self.__g) != 0)
def __repr__(self):
return "<%s sprite(in %d groups)>" % (self.__class__.__name__, len(self.__g))
So in summary, you don't have to subclass Sprite - you could just provide all of these methods on your own - but it's easier if you do ;)

Categories

Resources