#๐Ÿ”’ help with pygame

58 messages ยท Page 1 of 1 (latest)

sand jayBOT
#

@serene furnace

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

serene furnace
#

!paste

sand jayBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

serene furnace
#
import pygame
class Rectangle:
    def __init__(self,ai_game):
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings = ai_game.settings
        self.color = self.settings.rectangle_color
        self.rect = pygame.Rect(0,0,self.settings.rectangle_width,self.settings.rectangle_height)
        self.rect.right = self.screen_rect.right
        self.rect.centery = self.screen_rect.centery
        self.y = float(self.rect.y)
    def update(self):
        self.y += self.settings.rectangle_speed*self.settings.rectangle_direction
        self.rect.y = self.y
    def draw(self):
        pygame.draw.rect(self.screen,self.color,self.rect)
    def check_edges(self):
        if self.rect.top <= self.screen_rect.top or self.rect.bottom >= self.screen_rect.bottom:
            return True
        else:
            return False
    def change_direction(self):
        self.settings.rectangle_direction *= -1


#
class Settings:
    def __init__(self):
        self.screen_width = 800
        self.screen_height = 600
        self.bg_color = (230,230,230)
        self.ship_speed = 10.0
        self.rectangle_width = 10
        self.rectangle_height = 50
        self.rectangle_speed = 5.0
        self.rectangle_color = (0,0,0)
        self.rectangle_direction = 1
        self.bullet_speed = 10.0
        self.bullet_width = 15
        self.bullet_height = 3
        self.bullet_color = (100,100,100)
#
import pygame
class Ship:
    def __init__(self,ai_game):
        self.screen = ai_game.screen
        self.screen_rect = self.screen.get_rect()
        self.settings = ai_game.settings
        self.image = pygame.image.load('Player.png')
        self.rect = self.image.get_rect()
        self.image = pygame.transform.rotate(self.image,-90)
        self.rect.left = self.screen_rect.left
        self.rect.centery = self.screen_rect.centery
        self.y = float(self.rect.y)
        self.moving_up = False
        self.moving_down = False
    def update(self):
        if self.moving_up and self.rect.top >= self.screen_rect.top:
            self.y -= self.settings.ship_speed
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.y += self.settings.ship_speed
        self.rect.y = self.y
    def blitme(self):
        self.screen.blit(self.image,self.rect)
#
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
    def __init__(self,ai_game):
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color
        self.rect = pygame.Rect(0,0,self.settings.bullet_width,self.settings.bullet_height)
        self.rect.midleft = ai_game.ship.rect.midleft
        self.x = float(self.rect.x)
    def update(self):
        self.x += self.settings.bullet_speed
        self.rect.x = self.x
    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)
#

so the problem is

#

when the bullet hits the rectangle

#

the rectangle wont disappear

#

please hepl

wraith fox
serene furnace
#

...nvm it

#

im remaking it

serene furnace
serene furnace
#
def update_bullets(self):
        for bullet in self.bullets.copy():
            if bullet.rect.right >= self.settings.screen_width:
                self.bullets.remove(bullet)
        print(len(self.bullets))
        collisions = pygame.sprite.spritecollide(self.bullets,self.rectangle)
        if collisions:
            self.rectangle_present = False
#

above update rectangle

wraith fox
serene furnace
# wraith fox OK and what about `check_edges()`?
import pygame
class Rectangle:
    def __init__(self,ai_game):
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings = ai_game.settings
        self.color = self.settings.rectangle_color
        self.rect = pygame.Rect(0,0,self.settings.rectangle_width,self.settings.rectangle_height)
        self.rect.right = self.screen_rect.right
        self.rect.centery = self.screen_rect.centery
        self.y = float(self.rect.y)
    def update(self):
        self.y += self.settings.rectangle_speed*self.settings.rectangle_direction
        self.rect.y = self.y
    def draw(self):
        pygame.draw.rect(self.screen,self.color,self.rect)
    def check_edges(self):
        if self.rect.top <= self.screen_rect.top or self.rect.bottom >= self.screen_rect.bottom:
            return True
        else:
            return False
    def change_direction(self):
        self.settings.rectangle_direction *= -1
serene furnace
wraith fox
serene furnace
#

.

#

ok

wraith fox
#

!d pygame.sprite.groupcollide

sand jayBOT
#

pygame.sprite.groupcollide()```
 Find all sprites that collide between two groups. groupcollide(group1, group2, dokill1, dokill2, collided \= None) \-\> Sprite\_dict  This will find collisions between all the Sprites in two groups. Collision is determined by comparing the `Sprite.rect` attribute of each Sprite or by using the collided function if it is not None.

Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect.

If either dokill argument is True, the colliding Sprites will be removed from their respective Group.
wraith fox
# sand jay

The last 2 param for groupcollide has dokills

serene furnace
#
collisions = pygame.sprite.grouocollide(self.bullets,self.rectangle)
#

the problem is rect is not sprite

wraith fox
#

Why not?

serene furnace
#

because

#

there is only 1 rect

#

so why use sprites

wraith fox
#

So you can tell if it "collided" with a Sprite

#

Not Rect

serene furnace
#

how

wraith fox
# sand jay

Collision is determined by comparing the Sprite.rect attribute of each Sprite or by using the collided function if it is not None

serene furnace
#

but u mean like

#

check if rectangle collided with a sprite?

wraith fox
#

It cannot be a rectangle. It needs to be a Sprite

serene furnace
#

._.

#

ok

#

i am remaking it

#

agent

#

how would i rotate my ship 90 degrees -

wraith fox
#

!d pygame.transform.rotate

sand jayBOT
#

pygame.transform.rotate()```
 rotate an image rotate(surface, angle) \-\> Surface  Unfiltered counterclockwise rotation. The angle argument represents degrees and can be any floating point value. Negative angle amounts will rotate clockwise.

Unless rotating by 90 degree increments, the image will be padded larger to hold the new size. If the image has pixel alphas, the padded area will be transparent. Otherwise pygame will pick a color that matches the Surface colorkey or the topleft pixel value.
serene furnace
#

ok

#
self.image = pygame.image.load('Player.png')
        self.rect = self.image.get_rect()
        self.rect.left = self.screen_rect.left
        self.rect.centery = self.screen_rect.centery
        self.image = pygame.transform.rotate(self.image,-90)
#

should i give the rotation one a diff name

#

or maybe not

#

!close

sand jayBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.