Rectangle not moving on display PyGame - python

import pygame
width = 500
height = 500
win = pygame.display.set_mode((width,height))
pygame.display.set_caption("Client")
clientNumber = 0
class Player():
def __init__(self,x,y,width,height,color,win):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = (self.x,self.y,self.width,self.height)
self.val = 3
self.win = win
def draw(self):
pygame.draw.rect(self.win, self.color, self.rect)
def move(self):
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_LEFT]:
self.x = self.x - self.val
if keyPressed[pygame.K_RIGHT]:
self.x = self.x + self.val
if keyPressed[pygame.K_UP]:
self.y = self.y - self.val
if keyPressed[pygame.K_DOWN]:
self.y = self.y + self.val
def refreshWindow(win,player):
win.fill((255,255,255))
player.draw()
pygame.display.update()
def main():
player = Player(50,50,100,100,(0,255,0),win)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
player.move()
refreshWindow(win,player)
main()
The code is updating self.x and self.y and key is being pressed, but the display is not changing the
position of the rectangle. I am new to this and was learning from a tutorial. My code is the exact as that tutorial guide but its not working. Like only its not displaying the updated version. The rest is working fine, i printed statements to verify that the window object is the same and if key was pressed and if x and y coordinates were being changed or not.

Though you have answered your question and fixed your issue I want to mention something. It is not a great idea to have your game object keep both a self.rect and separate self.x, self.y, self.width, self.height attributes as you are doing.
All those attributes are contained within the rect and keeping to separate versions of the same data just means that you have to be updating them both and is large potential for errors either because one or the other did not get updated, or because the state was checked before it got updated and so it contained an old value.
just use a Rect and access the x, y, width, height values as attributes in it like this self.rect.x, self.rect.y, self.rect.width, self.rect.height.
You should check the docs for Rect here and see how much more useful it is than working with separate x,y,width, and height values. For example being able to manipulate it by the center, or being able to directly test the right edge without having to add the width, etc.
The exception with respect to keeping a separate self.x and self.y is if you are having issues related to angular movement where your velocity could be fractional and need floating point accuracy for the positions, since Rects keep int values. In that case you would keep adjust the self.x += vel_x and self.y += vel_y and then immediately assign the x,y to the rect self.rect.topleft = round(self.x), round(self.y) and then use the rect for the rest. The x and y are just for the position accuracywhen you need fractional position adjustments, which does not apply here in your example.

Never mind, i fixed it. I was not changing the self.rect values. Although the tutorial didnt change it. Anyways, its working now.

Related

Change color of Pygame Surface

I've made a Rectangle class:
class Rectangle(pygame.sprite.Sprite):
def __init__(self, len, x, y, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((len, len), flags=pygame.SRCALPHA)
# self.image.set_colorkey(Constants.WHITE)
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.center = (x + len / 2, y + len / 2)
self.is_blocked = False
self.x_pos = x
self.y_pos = y
self.x = math.floor((x - Constants.PADTOPBOTTOM) / Constants.CELL_SIZE)
self.y = math.floor((y - Constants.PADLEFTRIGHT) / Constants.CELL_SIZE)
def update(self):
if self.is_blocked:
print("is update working?") # it is working.
self.image.fill(Constants.GREEN)
And when update() is called I want to change the color of the rectangle to green but it doesn't work.
I'm new to pygame and I don't know what to do.
Your problem is that you keep creating new sprites every frame, so when you change the color of one Rectangle to green, it will be covered by a white one.
You do this by calling the fill function (which creates new Rectangle instances) inside the drawGrid function.

Sprite mask collision doesnt work (rect collision works)

The collision works when I use:
pygame.sprite.collide_rect
but it doesnt work when I use:
pygame.sprite.collide_mask
Here are my 2 classes:
class Ball():
def __init__(self):
self.rect = balltexture.get_rect()
self.rect.x=225
self.rect.y=400
self.radius = 100
self.velx=0
self.vely=0
self.action=False
self.switchx=0
self.switchy=0
pygame.sprite.Sprite.__init__(self)
self.mask = pygame.mask.from_surface(balltexture)
def draw(self):
win.blit(balltexture, self.rect)
class rock():
def __init__(self, y):
self.rect = rocktexture.get_rect()
self.rect.x = screensize.current_w
self.rect.y = y
self.width = 120
pygame.sprite.Sprite.__init__(self)
self.mask = pygame.mask.from_surface(rocktexture)
def draw(self, win):
win.blit(rocktexture, self.rect)
and here is the collision check
for i in rocks:
if pygame.sprite.collide_rect(i, Player):
print(1)
if pygame.sprite.collide_mask(i, Player):
print(2)
So when they collide it prints 1 all the time.
BTW I'm moving rect coordinates (e.g. Player.rect.x) to move the Player/rocks and I also tried the mask overlap method but it didn't work either
offset = (Player.rect.x-i.rect.x, Player.rect.y-i.rect.y)
if i.mask.overlap(Player.mask, offset):
print(2)
Not quite sure what's going on, but here's some things to try:
You can set the mask to all 1s with self.mask.fill(). At this point, the mask collision should be identical to the rect-based collision.
You can call print(self.mask.count()), which will show the number of set pixels. Make sure it's not 0 or something.
You can call i.mask.overlap(Player.mask (0, 0)), and confirm that those overlap when they have the same upper-left corner.
This is either going to be an issue with the mask creation, or the offsets are wrong in some way; this should narrow down the issue.
One of my images had no transparency, that was the problem

trying to use clamp_ip to keep sprites onscreen - pygame

I'm working on a game for class and this is what we've learned to do so far. I'm trying to get the sprites to stay onscreen with clamp_ip, but it keeps giving me an error message that "Butterfly instance has no attribute clamp_ip." Is there a different way I should be keeping the butterflies onscreen?
This first bit is just setting up pygame and the butterfly class (where I included a line to define a rectangle for the butterflies), I've highlighted where I'm guessing the potential errors are below.
This should be ok.
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
playground = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Butterflies')
screenRect = playground.get_rect()
steps = 1
class Butterfly:
def __init__(self):
self.x = random.randint(0, SCREEN_WIDTH)
self.y = random.randint(0, SCREEN_HEIGHT)
self.image_Butterfly = pygame.image.load('butterflya.png')
self.image_ButterflyB = pygame.image.load('butterflyb.png')
self.height = self.image_Butterfly.get_height()
self.width = self.image_Butterfly.get_width()
self.rect = (self.width, self.height)
clock = pygame.time.Clock()
fps = 10
net = []
for i in range (15):
butterflyInstance = Butterfly()
net.append(butterflyInstance)
playground.fill(cyan)
Game_Over = False
while not Game_Over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Game_Over = True
This is where I'm guessing I messed up. I tried to use the clamp_ip function at the end of this loop
for butterflyInstance in net:
butterflyInstance.x += random.randint(-10, 10)
butterflyInstance.y += random.randint(-10,10)
if steps % 2 == 0:
playground.blit(butterflyInstance.image_Butterfly, (butterflyInstance.x, butterflyInstance.y))
steps += 1
else:
playground.blit(butterflyInstance.image_ButterflyB, (butterflyInstance.x, butterflyInstance.y))
steps +=1
butterflyInstance.clamp_ip(screenRect)
Thank you so much!
See PyGame doc pygame.Rect()
clamp_ip is pygame.Rect method so you have to use butterflyInstance.rect.clamp_ip()
But you have to use self.rect.x, self.rect.y, self.rect.width, self.rect.height instead of self.x, self.y, self.width, self.height to keep position and size of object.
And use butterflyInstance.rect.x, butterflyInstance.rect.y, butterflyInstance.rect.width, butterflyInstance.rect.height
instead of butterflyInstance.x, butterflyInstance.y, butterflyInstance.width, butterflyInstance.height
PyGame use pygame.Rect in many places - it is usefull. For example: you can use rect.right instead of rect.x + rect.width, or rect.center = screenRect.center to center object on screen.

Spritesheet trouble [duplicate]

This question already has answers here:
How do I create animated sprites using Sprite Sheets in Pygame?
(1 answer)
Invalid destination position for blit error, not seeing how
(1 answer)
Closed 2 years ago.
Alright, so I got some extremely simple code going on here, any comments/suggestions are recommended. Keep in mind, SIMPLE, in other words short and concise. thnx.
My problem is loading an image from a png file. So for example i got a couple of images in the file, and i want only one row to be loaded when the user presses for example, the right arrow key. Basically i have 4 rows, 2 or 3 columns 4 rows for each arrow key respectively
#
import pygame, time, random
pygame.init()
HEIGHT = 700
WIDTH = 1350
GRIDSIZE = HEIGHT/28
BLACK = ( 0, 0, 0)
WHITE = (255,255,255)
RED = (255, 0, 0)
screen=pygame.display.set_mode((WIDTH,HEIGHT))
font = pygame.font.SysFont("arial", 36)
shift = 10
#---------------------------------------#
# classes #
#---------------------------------------#
class Sprite(pygame.sprite.Sprite):
""" (fileName)
Visible game object.
Inherits Sprite to use its Rect property.
"""
def __init__(self, picture=None):
pygame.sprite.Sprite.__init__(self)
self.x = 0
self.y = 0
self.visible = False
self.image = self.blend_image(picture)
self.rect = self.image.get_rect()
self.width, self.height = self.rect.width, self.rect.height
self.update()
def spawn(self, x, y):
""" Assign coordinates to the object and make it visible.
"""
self.x, self.y = x,y
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
self.visible = True
def draw(self, surface):
surface.blit(self.image, self.rect)
def update(self):
# after moving a sprite, the rect attribute must be updated
self.rect = self.image.get_rect()
self.rect = pygame.Rect(self.x,self.y,self.rect.width,self.rect.height)
def moveLeft(self):
self.x -= shift
self.update()
def moveRight(self):
self.x += shift
self.update()
def moveUp(self):
self.y -= shift
self.update()
def moveDown(self):
self.y += shift
self.update()
def blend_image(self, file_name):
""" Remove background colour of an image.
Need to use it when a picture is stored in BMP or JPG format, with opaque background.
"""
image_surface = pygame.image.load(file_name)
image_surface = image_surface.convert()
colorkey = image_surface.get_at((0,0))
image_surface.set_colorkey(colorkey)
return image_surface
#---------------------------------------#
# functions #
#---------------------------------------#
def redraw_screen():
screen.fill(BLACK)
world.draw(screen)
if player.visible:
player.draw(screen)
pygame.display.update()
#---------------------------------------#
# main program #
#---------------------------------------#
world = Sprite("town.png")
world.spawn(0,0)
player = Sprite("player.png")
player.spawn(100,400)
LEFT_BORDER = 0
RIGHT_BORDER = WIDTH-1100
TOP_BORDER = 0
BOTTOM_BORDER = HEIGHT-480
#---------------------------------------#
clock = pygame.time.Clock()
FPS = 10
inPlay = True
while inPlay:
clock.tick(FPS)
# keyboard handler
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
inPlay = False
# world moves opposite to the arrow
if keys[pygame.K_LEFT] and world.x < LEFT_BORDER:
world.moveRight()
if keys[pygame.K_RIGHT] and world.x > RIGHT_BORDER:
world.moveLeft()
if keys[pygame.K_UP] and world.y < TOP_BORDER:
world.moveDown()
if keys[pygame.K_DOWN] and world.y > -BOTTOM_BORDER:
world.moveUp()
redraw_screen()
#---------------------------------------#
pygame.quit()
Right, so this is using python 2.7 btw. Any help is appreciated thnx again.
And also, there are some things that are added that have no use, but thats for later code, and vice-versa. Some tweaks i could do, like naming, that will come later.
If the player presses right, i have an image of a guy facing right.
Left, up, and down same concept.
if he holds the button, the guy "runs"
Now i hav normal position, and running position.
4 rows, 2 columns in a png file, how to load 1 row for respective key?

Cleanest way for a sprite to inherit an attribute from its group?

Specifically, I would like each sprite in sg_fireball to have 'bounces', without giving 'bounces' to every sprite from Spell(). Is there a clean way to do this without making 'bounces' an argument of Spell(), or looping through sg_fireball?
The relevant code snippets:
self.sg_fireball = pygame.sprite.Group()
self.sg_fireball.speed = 6.0
self.sg_fireball.image = pygame.image.load("fireball.png")
self.sg_fireball.bounces = 1
if event.type == pygame.MOUSEBUTTONDOWN:
self.character.cast(self.sg_fireball)
def cast(self, sg):
sg.add(Spell(self.rect.center, sg.speed, self.dir, sg.image))
class Spell(pygame.sprite.Sprite):
def __init__(self,pos, speed, direction, img, bounces):
pygame.sprite.Sprite.__init__(self)
self.bounces = bounces
self.image = img
self.rect = pygame.Rect(pos, (8,8))
self.posx = self.rect.x
self.posy = self.rect.y
self.speed = speed
self.dir = direction
self.velx = self.speed*math.cos(self.dir)
self.vely = self.speed*math.sin(self.dir)
If I understood correctly, you wish some of the sprites to have a certain attribute, while others won't. This is a perfect example of polimorphism and inheritance.
This is one of the options that you can do:
You subclass a normal spell as a bouncy spell. You can then have another update function where you will take care of bouncing. You can normally add a BouncySpell in the same sprite group as the NormalSpell.

Categories

Resources