How to get value of variable from inside of class in Python? - python

So, im writing some simple code in python pygame, just to learn. I encountered problem, when i want to check collisions of ball whether it is the same as y possition of my pallete. The main problem is that the value of variable of my pallete is updating by the user, and it sits inside class pallete, in function move. How can i get it out and put it outside class, into function holding ball?
import pygame
from pygame.constants import DROPTEXT
pygame.init()
#window
w_width = 1000
w_height = 600
screen = pygame.display.set_mode((w_width, w_height))
clock = pygame.time.Clock()
open = True
#player
x,y = 100,250
#define player action variables
speed = 5
speed_x,speed_y = 5,4
moving_down = False
moving_up = False
#define ball action variables
x_ball,y_ball = 500,250
radius = 30
class palette(pygame.sprite.Sprite):
global x,y
def __init__(self, x, y, speed):
self.speed = speed
pygame.sprite.Sprite.__init__(self)
self.player_rect = pygame.Rect(x,y,30,100)
def draw(self):
pygame.draw.rect(screen, 'White', self.player_rect)
def move(self, moving_up, moving_down):
#reset movement variables
global dy
dy = 0
#assing movement variables if moving up or down
if moving_up:
dy = -self.speed
if moving_down:
dy = self.speed
#update pallete possition
self.player_rect.y += dy
def ball():
global speed_x,speed_y,x_ball,y_ball
#update pos of bal
x_ball += speed_x
y_ball += speed_y
#basic colision with screen
if x_ball>=w_width-(0.5*radius) or x_ball <=0+(0.5*radius):
speed_x*=-1
if y_ball>=w_height-(0.5*radius) or y_ball <=0+(0.5*radius):
speed_y*=-1
#collision with pallettes
pygame.draw.circle(screen, (255,255,255), (x_ball,y_ball), radius, 5)
#build players and enemies
player = palette(x,y, speed)
enemy = palette(x*9,y, speed)
#game
while open:
for event in pygame.event.get():
#quit game
if event.type == pygame.QUIT:
open = False
#keyboard presses
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
moving_up = True
if event.key == pygame.K_s:
moving_down = True
if event.key == pygame.K_ESCAPE:
open = False
#keyboard button released
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
moving_up = False
if event.key == pygame.K_s:
moving_down = False
screen.fill((0,0,0))
clock.tick(60)
pygame.display.flip
#keys
#init players
player.draw()
enemy.draw()
enemy.move(moving_up, moving_down)
player.move(moving_up, moving_down)
ball()
pygame.display.update()

Read about Classes and Instance Objects. The "variables" in a class are called "attributes". You can access the instance attributes through the instance object. In your case player and enemy are instances of the class palette. The palette class has a player_rect attribute which is a pygame.Rect object. Therefore:
y = player.pylyer_rect.y
However read the documentation. pygame.sprite.Group.draw() and pygame.sprite.Group.update() are methods which are provided by pygame.sprite.Group.
The latter delegates to the update method of the contained pygame.sprite.Sprites — you have to implement the method. See pygame.sprite.Group.update():
Calls the update() method on all Sprites in the Group. [...]
The former uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects — you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():
Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]
So you should rename player_rect to rect. Class Names should normally use the CapWords convention. So rename palette in Palette:
class Palette(pygame.sprite.Sprite):
def __init__(self, x, y, speed):
self.speed = speed
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(x,y,30,100)
# [...]
player = Palette(x, y, speed)
enemy = Palette(x*9, y, speed)

Related

Pick up block using pygame.KEYDOWN

I am trying to use pygame.KEYDOWN so that when I press the key represented by pygame.K_q the block that the player has collided with can be dragged (pick up) to where the player moves as it is done using pygame.MOUSEBUTTONDOWN in this tutorial.
But pressing the q key to drag the block does not work...
This need of mine arose when trying to implement this functionality in another larger code. So I decided to get this other tutorial2 to set up my MWE. I looked to see if anyone had already asked about it but I just found relative but not exact questions that used a very different or very large code structure and I still haven't figured out why I can't get my MWE code to work. This should be a simple question and someone may have already posted this question, so if so let me know where there is a question already posted that clarifies my question.
My MWE:
import pygame
import random
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Block(pygame.sprite.Sprite):
"""
This class represents the block to be picked up.
It derives from the "Sprite" class in Pygame.
"""
def __init__(self, color, width, height):
""" Constructor. Pass in the color of the block,
and its x and y position. """
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
""" The class is the player-controlled sprite. """
carry_block_list = []
def __init__(self, x, y):
"""Constructor function"""
# Call the parent's constructor
super().__init__()
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(RED)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
diff_x = self.rect.x - 4
diff_y = self.rect.y - 4
# Loop through each block that we are carrying and adjust
# it by the amount we moved.
for block in self.carry_block_list:
block.rect.x -= diff_x
block.rect.y -= diff_y
print("something")
# Call this function so the Pygame library can initialize itself
pygame.init()
# Create an 800x600 sized screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
# Set the title of the window
pygame.display.set_caption('Move Sprite With Keyboard')
# Create the player paddle object
player = Player(50, 50)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
block_list = pygame.sprite.Group()
for i in range(50):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
print("pick up")
# When the mouse button is pressed, see if we are in contact with
# other sprites:
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False)
# Set the list of blocks we are in contact with as the list of
# blocks being carried.
player.carry_block_list = blocks_hit_list
if event.key == pygame.K_l:
print("let go")
# When we let up on the mouse, set the list of blocks we are
# carrying as empty.
player.carry_block_list = []
if event.key == pygame.K_LEFT:
player.rect.x -= player.rect.width
elif event.key == pygame.K_RIGHT:
player.rect.x += player.rect.width
elif event.key == pygame.K_UP:
player.rect.y -= player.rect.height
elif event.key == pygame.K_DOWN:
player.rect.y += player.rect.height
# -- Draw everything
# Clear screen
screen.fill(WHITE)
# Draw sprites
all_sprites_list.draw(screen)
# Flip screen
pygame.display.flip()
# Pause
clock.tick(40)
pygame.quit()
You move blocks in Player.update() but you never execute it
First I tried to execute this function in every loop but finally I changed this function to run only when players is moved
def update(self, diff_x, diff_y):
# Loop through each block that we are carrying and adjust
# it by the amount we moved.
for block in self.carry_block_list:
block.rect.x += diff_x
block.rect.y += diff_y
and
if event.key == pygame.K_LEFT:
player.rect.x -= player.rect.width
player.update(-player.rect.width, 0)
elif event.key == pygame.K_RIGHT:
player.rect.x += player.rect.width
player.update(player.rect.width, 0)
elif event.key == pygame.K_UP:
player.rect.y -= player.rect.height
player.update(0, -player.rect.height)
elif event.key == pygame.K_DOWN:
player.rect.y += player.rect.height
player.update(0, player.rect.height)
Full code:
import pygame
import random
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Block(pygame.sprite.Sprite):
"""
This class represents the block to be picked up.
It derives from the "Sprite" class in Pygame.
"""
def __init__(self, color, width, height):
""" Constructor. Pass in the color of the block,
and its x and y position. """
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
""" The class is the player-controlled sprite. """
carry_block_list = []
def __init__(self, x, y):
"""Constructor function"""
# Call the parent's constructor
super().__init__()
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(RED)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self, diff_x, diff_y):
# Loop through each block that we are carrying and adjust
# it by the amount we moved.
for block in self.carry_block_list:
block.rect.x += diff_x
block.rect.y += diff_y
# Call this function so the Pygame library can initialize itself
pygame.init()
# Create an 800x600 sized screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
# Set the title of the window
pygame.display.set_caption('Move Sprite With Keyboard')
# Create the player paddle object
player = Player(50, 50)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
block_list = pygame.sprite.Group()
for i in range(50):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
# When the mouse button is pressed, see if we are in contact with
# other sprites:
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False)
# Set the list of blocks we are in contact with as the list of
# blocks being carried.
player.carry_block_list = blocks_hit_list
if event.key == pygame.K_l:
# When we let up on the mouse, set the list of blocks we are
# carrying as empty.
player.carry_block_list = []
if event.key == pygame.K_LEFT:
player.rect.x -= player.rect.width
player.update(-player.rect.width, 0)
elif event.key == pygame.K_RIGHT:
player.rect.x += player.rect.width
player.update(player.rect.width, 0)
elif event.key == pygame.K_UP:
player.rect.y -= player.rect.height
player.update(0, -player.rect.height)
elif event.key == pygame.K_DOWN:
player.rect.y += player.rect.height
player.update(0, player.rect.height)
# -- Draw everything
# Clear screen
screen.fill(WHITE)
# Draw sprites
all_sprites_list.draw(screen)
# Flip screen
pygame.display.flip()
# Pause
clock.tick(40)
pygame.quit()

Python - Pygame - Class __init___ question/Not drawing

I'm learning OOP and I have a few questions.
When the initiliazer is called, is the code automatically processed?
Cause if that's the case, I don't understand why my game isn't drawing the rectangle i ask it to draw in the init part of the player class.
I'm very new to OOP and as such I'm not sure of what I'm doing, to some extent.
Here's my code:
import pygame
white = (255, 255, 255)
black = (0, 0, 0)
class Game():
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
def __init__(self):
pass
def fill_screen(self, color):
self.color = color
self.screen.fill(self.color)
class Player(pygame.sprite.Sprite):
lead_x = 800/2
lead_y = 600/2
velocity = 0.002
block_size = 10
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.draw.rect(game.screen, black, [self.lead_x, self.lead_y, self.block_size, self.block_size])
def move_player_x_left(self):
self.lead_x += -self.velocity
def move_player_x_right(self):
self.lead_x += self.velocity
def move_player_y_up(self):
self.lead_y += -self.velocity
def move_player_y_down(self):
self.lead_y += self.velocity
game = Game()
player = Player()
exitGame = False
while not exitGame:
game.fill_screen(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.move_player_y_up()
if event.key == pygame.K_s:
player.move_player_y_down()
if event.key == pygame.K_d:
player.move_player_x_right()
if event.key == pygame.K_a:
player.move_player_x_left()
pygame.display.update()
pygame.quit()
quit()
You are constantly filling the screen with white in your mainloop. The Player class only draws on __init__. This means that the rect is drawn for a split second and then covered over by white.
Your assumption about the code in __init__ automatically being called is correct. These methods with double underscores are called by python in special cases, they are called magic methods. You can find a list of them here.
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# The rect drawing part was moved from here.
def update(self):
# You were previously assigning this to a variable, this wasn't necessary.
pygame.draw.rect(game.screen, black, [self.lead_x, self.lead_y, self.block_size, self.block_size])
You will need to call the new update method in the mainloop after you fill the screen.
while True:
game.fill_screen(white)
player.update()

Python 'group object' has no attribute 'update_good' - chapter 14 sprite moving

I am having troubles with an assignment for school and my teacher doesn't know enough about coding to help me.
The link to the assignment is here:
http://programarcadegames.com/index.php?lang=en&chapter=lab_sprite_moving
I got to step 6 on the assignment where it wants me to make the green blocks move but I cant seem to figure out how to make the update method work (I called it update_good in my code).
When I try the run the code it comea up with the error AttributeError: 'Group' object has no attribute 'update_good'
Here is my code
import pygame
import random
import block_library
import good_block_library
A_Block = block_library.Block
A_Good_Block = good_block_library.good_block
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
class Player(pygame.sprite.Sprite):
""" The class is the player-controlled sprite. """
# -- Methods
def __init__(self, x, y):
"""Constructor function"""
# Call the parent's constructor
super().__init__()
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(BLUE)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# -- Attributes
# Set speed vector
self.change_x = 0
self.change_y = 0
def changespeed(self, x, y):
""" Change the speed of the player"""
self.change_x += x
self.change_y += y
def update(self):
""" Find a new position for the player"""
self.rect.x += self.change_x
self.rect.y += self.change_y
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
bump_sound = pygame.mixer.Sound("bump.wav")
good_sound = pygame.mixer.Sound("good_block.wav")
bad_sound = pygame.mixer.Sound("bad_block.wav")
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
good_block_list = pygame.sprite.Group()
bad_block_list = pygame.sprite.Group()
# This is a list of every sprite.
# All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
for i in range(50):
# This represents a block
A_Good_Block = A_Block(GREEN, 20, 15)
# Set a random location for the block
A_Good_Block.rect.x = random.randrange(screen_width)
A_Good_Block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
good_block_list.add(A_Good_Block)
all_sprites_list.add(A_Good_Block)
for i in range(50):
# This represents a block
bad_block = A_Block(RED, 20, 15)
# Set a random location for the block
bad_block.rect.x = random.randrange(screen_width)
bad_block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
bad_block_list.add(bad_block)
all_sprites_list.add(bad_block)
# Create a player block
player = Player(10, 15)
all_sprites_list.add(player)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
score = 0
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 3)
# Reset speed when key goes up
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -3)
# Clear the screen
screen.fill(WHITE)
#!!!! this is where the problem occurs v
good_block_list.update_good()
all_sprites_list.update()
# See if the player block has collided with anything.
blocks_hit_list = pygame.sprite.spritecollide(player, good_block_list, True)
blocks_hit_list2 = pygame.sprite.spritecollide(player, bad_block_list, True)
# Check the list of collisions.
for block in blocks_hit_list:
score += 1
good_sound.play()
for block in blocks_hit_list2:
score-= 1
bad_sound.play()
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Score: " + str(score), True, BLACK)
screen.blit(text, [30, 350])
# Draw all the spites
all_sprites_list.draw(screen)
if player.rect.y == 0 or player.rect.y == 399:
bump_sound.play()
if player.rect.x == 1 or player.rect.x == 700:
bump_sound.play()
# update the screen
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
pygame.quit()
Below is the program I import that contains the good_block class and the update_good function. I think the problem might be in here
def main():
pass
if __name__ == '__main__':
main()
import pygame
import block_library
import random
GREEN = (0, 255, 0)
screen_width = 700
screen_height = 400
A_Block = block_library.Block
change_x = random.randint(-3,3)
change_y = random.randint(-3,3)
good_block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
class good_block(A_Block):
def changespeed(self, x, y):
""" Change the speed of the player"""
self.change_x += x
self.change_y += y
def update_good(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
Below is the library has the class that A_good_block is inherited from
import pygame
import random
class Block(pygame.sprite.Sprite):
"""
This class represents the ball.
It derives from the "Sprite" class in Pygame.
"""
def __init__(self, color, width, height):
""" Constructor. Pass in the color of the block,
and its x and y position. """
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
self.change_x = random.randint(-3,3)
self.change_y = random.randint(-3,3)
Please help
The problem is that you don't fully understand what you're code is doing.
The reason why the code all_sprites_list.update() works is because that method is actually defined for that object. You have a pygame Group object which has it's own methods that you can read about here: https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.
What you'll find is that the update() method is there but there isn't any update_good() method. The docs say that the update() method calls the update() method off every Sprite contained in it.
So it seems simple, just change update_good() to update() and then call the update() method of the Group and it will work.

python/pygame help for lab 13 programarcadegames

I am working through the programarcadegames labs using python and pygame and I am stuck. When I am running this code, I am doing something wrong when I am initializing the block and player classes. The player class is supposed to be its own class and not an instance of Block class.
When I run this program, I am getting this error:
'Block' object has no attribute 'changespeed'
import pygame
import random
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
class Block(pygame.sprite.Sprite):
def __init__(self, color, width, height):
""" Constructor. Pass in the color of the block,
and its x and y position. """
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
class Player (pygame.sprite.Sprite):
""" The class is the player-controlled sprite. """
def __init__(self,x,y):
"""Constructor function"""
# Call the parent's constructor
super().__init__()
# -- methods
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(BLACK)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# -- Attributes
# Set speed vector
self.change_x = 0
self.change_y = 0
def changespeed(self, x, y):
""" Change the speed of the player"""
self.change_x += x
self.change_y += y
def update(self):
""" Find a new position for the player"""
self.rect.x += self.change_x
self.rect.y += self.change_y
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
block_list = pygame.sprite.Group()
# This is a list of every sprite.
# All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
for i in range(50):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)
# Create a RED player block
player = Block(BLUE, 20, 15)
all_sprites_list.add(player)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
score = 0
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Set the speed based on the key pressed
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 3)
# Reset speed when key goes up
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -3)
# Clear the screen
screen.fill(WHITE)
# Get the current mouse position. This returns the position
# as a list of two numbers.
#pos = pygame.mouse.get_pos()
# Fetch the x and y out of the list,
# just like we'd fetch letters out of a string.
# Set the player object to the mouse location
#player.rect.x = pos[0]
#player.rect.y = pos[1]
# See if the player block has collided with anything.
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
# Check the list of collisions.
for block in blocks_hit_list:
score += 1
print(score)
# This calls update on all the sprites
all_sprites_list.update()
# Draw all the spites
all_sprites_list.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
pygame.quit()
Here's the problem: player = Block(BLUE, 20, 15). The player is a Block instance not a Player instance and blocks have got no changespeed method.

PyGame get sprite group

hey guys, was trying to build pacman using pygame, have a small problem. When the pacman is moving if i press a key then it changes direction, unfortunately if there is a wall above then the pacman stops at that place and points upwards until i change the direction. I want help with how to find out if the block 3 or 4 units of pacman.rect.y belongs to sprite group level which has all the walls and stuff ..
Here's a longer example of a working game with walls:
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples
import pygame
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
# This class represents the bar at the bottom that the player controls
class Wall(pygame.sprite.Sprite):
# Constructor function
def __init__(self,x,y,width,height):
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Make a blue wall, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill(blue)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.top = y
self.rect.left = x
# This class represents the bar at the bottom that the player controls
class Player(pygame.sprite.Sprite):
# Set speed vector
change_x=0
change_y=0
# Constructor function
def __init__(self,x,y):
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(white)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.top = y
self.rect.left = x
# Change the speed of the player
def changespeed(self,x,y):
self.change_x+=x
self.change_y+=y
# Find a new position for the player
def update(self,walls):
# Get the old position, in case we need to go back to it
old_x=self.rect.left
new_x=old_x+self.change_x
self.rect.left = new_x
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, walls, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.left=old_x
old_y=self.rect.top
new_y=old_y+self.change_y
self.rect.top = new_y
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, walls, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.top=old_y
score = 0
# 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('Test')
# Create a surface we can draw on
background = pygame.Surface(screen.get_size())
# Used for converting color maps and such
background = background.convert()
# Fill the screen with a black background
background.fill(black)
# Create the player paddle object
player = Player( 50,50 )
movingsprites = pygame.sprite.RenderPlain()
movingsprites.add(player)
# Make the walls. (x_pos, y_pos, width, height)
wall_list=pygame.sprite.RenderPlain()
wall=Wall(0,0,10,600)
wall_list.add(wall)
wall=Wall(10,0,790,10)
wall_list.add(wall)
wall=Wall(10,200,100,10)
wall_list.add(wall)
clock = pygame.time.Clock()
done = False
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3,0)
if event.key == pygame.K_RIGHT:
player.changespeed(3,0)
if event.key == pygame.K_UP:
player.changespeed(0,-3)
if event.key == pygame.K_DOWN:
player.changespeed(0,3)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3,0)
if event.key == pygame.K_RIGHT:
player.changespeed(-3,0)
if event.key == pygame.K_UP:
player.changespeed(0,3)
if event.key == pygame.K_DOWN:
player.changespeed(0,-3)
player.update(wall_list)
screen.fill(black)
movingsprites.draw(screen)
wall_list.draw(screen)
pygame.display.flip()
clock.tick(40)
pygame.quit()
You could use:
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide
pacman.rect.y = pacman.rect.y - 3
colliding = pygame.sprite.spritecollide(pacman, level)
if colliding:
can_move_upwards = False
else:
can_move_upwards = True
pacman.rect.y = pacman.rect.y + 3
And do the same for every direction you want to test.

Categories

Resources