So I am currently working on Ping with Pygame and I just can't figure out how I should do the collision with the paddle. I have one Pong Game without Classes where i did it like this
if paddle.colliderect(ecl):
bewegungx = abs(bewegungx)
ecl is the ball in this case and here is it with my classes. I currently have only one paddle since I wanna first figure the collision out and then do the rest.
Thank you in advance :)
import pygame
white = (255, 255, 255)
black = (0, 0, 0)
WIDTH = 400
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
FPS = 60
clock = pygame.time.Clock()
class Ball():
def __init__(self):
self.ball_w = 30
self.ball_h = 30
self.ball_x = 100
self.ball_y = 150
self.ball_speedx = 3
self.ball_speedy = 3
self.ball_draw = pygame.Rect(self.ball_x, self.ball_y, self.ball_w, self.ball_h)
def draw(self):
pygame.draw.rect(screen, white, self.ball_draw)
def move(self):
self.ball_x += self.ball_speedx
self.ball_y += self.ball_speedy
self.ball_draw.topleft = (self.ball_x, self.ball_y)
def wall_collision(self):
if self.ball_draw.top <=0:
self.ball_speedy = self.ball_speedy *-1
if self.ball_draw.right >= WIDTH:
self.ball_speedx = self.ball_speedx *-1
if self.ball_draw.left <=0:
self.ball_speedx = self.ball_speedx *-1
if self.ball_draw.bottom >= HEIGHT:
self.ball_speedy = self.ball_speedy *-1
class Paddle():
def __init__(self):
self.paddle_w = 30
self.paddle_h = 130
self.paddle_x = 10
self.paddle_y = 150
self.paddle_speed = 0
self.paddle_left = pygame.Rect(self.paddle_x, self.paddle_y, self.paddle_w, self.paddle_h)
def draw(self):
pygame.draw.rect(screen, white, self.paddle_left)
def move(self):
key = pygame.key.get_pressed()
if key[pygame.K_w] and self.paddle_left.top > 0:
self.paddle_left.y -=7
if key[pygame.K_s] and self.paddle_left.bottom < 600:
self.paddle_left.y += 7
ball = Ball()
paddle = Paddle()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill(black)
#Ball Functionsw
ball.draw()
ball.move()
ball.wall_collision()
#Paddle Functions
paddle.draw()
paddle.move()
pygame.display.flip()
clock.tick(FPS)
When the Ball hits the Paddle, the direction of the Ball changes. Add a method paddle_left_collision to the Ball class:
class Ball():
# [...]
def paddle_left_collision(self, paddle_rect):
if self.ball_draw.colliderect(paddle_rect):
self.ball_speedx = abs(self.ball_speedx)
Call it in the applicaiton loop
while True:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# move objects and detect collisisons
ball.move()
paddle.move()
ball.wall_collision()
ball.paddle_left_collision(paddle.paddle_left)
# draw scene
screen.fill(black)
ball.draw()
paddle.draw()
pygame.display.flip()
clock.tick(FPS)
Note that if you have two paddles, change the method as follows:
def paddles_collision(self, paddle_left_rect, paddle_right_rect):
if self.ball_draw.colliderect(paddle_left_rect):
self.ball_speedx = abs(self.ball_speedx)
if self.ball_draw.colliderect(paddle_right_rect):
self.ball_speedx = -abs(self.ball_speedx)
See also How do I detect collision in pygame? and Sometimes the ball doesn't bounce off the paddle in pong game
Related
I'm new to Pygame and trying to learn about sprites and collision detection. I found some code and tried to add some functionality to it.
The basic idea is that I have a polygon which can be rotated in place. When the user presses SPACEBAR, the polygon fires a projectile which can bounce around the frame. If the projectile hits the polygon again, I would like to quite the program
First of all, here is the code
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((32, 32))
self.image.fill((0, 0, 0))
self.image.set_colorkey((0, 0, 0))
self.fire_from = (36, 20)
pygame.draw.polygon(self.image, pygame.Color('dodgerblue'), ((0, 0), (32, 16), (0, 32)))
self.org_image = self.image.copy()
self.angle = 0
self.direction = pygame.Vector2(1, 0)
self.rect = self.image.get_rect(center=(200, 200))
self.pos = pygame.Vector2(self.rect.center)
def update(self, events, dt):
for e in events:
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_SPACE:
bullet = Projectile(self.fire_from, self.direction.normalize())
self.groups()[0].add(bullet)
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
self.angle += 3
if pressed[pygame.K_RIGHT]:
self.angle -= 3
self.direction = pygame.Vector2(1, 0).rotate(-self.angle)
self.image = pygame.transform.rotate(self.org_image, self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
class Projectile(pygame.sprite.Sprite):
def __init__(self, pos, direction):
super().__init__()
self.image = pygame.Surface((8, 8))
self.image.fill((0, 0, 0))
self.image.set_colorkey((0, 0, 0))
pygame.draw.circle(self.image, pygame.Color('orange'), (4, 4), 4)
self.rect = self.image.get_rect(center=pos)
self.direction = direction
self.pos = pygame.Vector2(self.rect.center)
self.max_bounce = 10
def update(self, events, dt):
#print(self.pos)
# Bounding box of the screen
screen_r = pygame.display.get_surface().get_rect()
# where we would move next
next_pos = self.pos + self.direction * dt
# we hit a wall
if not screen_r.contains(self.rect):
# after 10 hits, destroy self
self.max_bounce -= 1
if self.max_bounce == 0:
return self.kill()
# horizontal reflection
if next_pos.x > screen_r.right or next_pos.x < screen_r.left:
self.direction.x *= -1
# vertical reflection
if next_pos.y > screen_r.bottom or next_pos.y < screen_r.top:
self.direction.y *= -1
# move after applying reflection
next_pos = self.pos + self.direction * dt
# set the new position
self.pos = next_pos
self.rect.center = self.pos
def main():
pygame.init()
screen = pygame.display.set_mode((500, 500))
tank = Player()
sprites = pygame.sprite.Group()
sprites.add(tank)
#print(tank.groups()[0])
clock = pygame.time.Clock()
dt = 0
running = True
while running:
#print(tank.groups()[0])
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
sprites.update(events, dt)
screen.fill((30, 30, 30))
sprites.draw(screen)
pygame.display.update()
## ALWAYS DETECTS A COLLISION, WHY?
if len(tank.groups()[0])>1:
if pygame.sprite.spritecollideany(tank, tank.groups()[0]):
running = False
dt = clock.tick(60)
if __name__ == '__main__':
main()
I gather here that the Player class (i.e. my tank) itself has a sprite groups, to which it adds each projectile as its fired. Furthermore, the projectile originates from within the rectangle for the polygon (hence it always initially collides)
I would like to only add the projectile to the sprite group if it has made at least 1 bounce, is that possible? Or... is there a better way to do this?
Any help or pointers please?
Thanks
Null
The tank collides with itself, because it is a member of tank.groups()[0]:
if pygame.sprite.spritecollideany(tank, tank.groups()[0]):
Add a Group that contains just the bullets:
class Player(pygame.sprite.Sprite):
def __init__(self):
# [...]
self.bullets = pygame.sprite.Group()
def update(self, events, dt):
for e in events:
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_SPACE:
bullet = Projectile(self.fire_from, self.direction.normalize())
self.groups()[0].add(bullet)
self.bullets.add(bullet)
# [...]
Use this Group for the collision test:
def main():
# [...]
while running:
# [...]
pygame.display.update()
if pygame.sprite.spritecollideany(tank, tank.bullets):
running = False
dt = clock.tick(60)
I'm making a simple game in pygame, in which you're supposed to dodge or shoot the targets that descend through the screen. So far, I've created the ship and managed to set the movement of both the bullet and the ship, as for their key bindings. However, the bullet's x coordinate doesn't seem to change at all, even though I've defined in the init method in the bullet class that the bullet x coordinate is equal to the ship x coordinate, therefore, the bullet would always be leaving the ship's position. Instead, the bullet always leaves the middle of the screen. I can't find the issue. Any help would be very much appreciated
import pygame, sys
pygame.init()
clock = pygame.time.Clock()
screen_width = 600
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Space Race Game")
class ROCKET:
def __init__(self):
self.rocketImg = pygame.image.load("spaceship.png")
self.rocket_x = screen_width/2 - 32
self.rocket_y = screen_height/2 + 100
def draw_rocket(self):
screen.blit(self.rocketImg, (self.rocket_x, self.rocket_y))
def move_rocket(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and self.rocket_x + 15 > 0:
self.rocket_x -= 5
if key[pygame.K_RIGHT] and self.rocket_x < screen_width - 40:
self.rocket_x += 5
class BULLET(ROCKET):
def __init__(self):
super().__init__()
self.bullet_width = 10
self.bullet_height = 20
self.bullet_x = self.rocket_x + 25
self.bullet_y = self.rocket_y
self.move = [0, 0]
self.bullet_speed = 5
self.bullet_rect = pygame.Rect(self.bullet_x, self.bullet_y, self.bullet_width, self.bullet_height)
def draw_bullet(self):
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
self.bullet_x = self.rocket_x
self.move[1] = -1
self.bullet_y += self.move[1] * self.bullet_speed
self.bullet_rect.topleft = (self.bullet_x, self.bullet_y)
if self.bullet_y < self.rocket_y - 10:
pygame.draw.rect(screen, (0, 0, 0), self.bullet_rect)
if self.bullet_y < - 20:
self.bullet_y = self.rocket_y
self.move[1] = 0
rocket = ROCKET()
bullet = BULLET()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
rocket.draw_rocket()
rocket.move_rocket()
bullet.draw_bullet()
pygame.display.flip()
clock.tick(60)
You have to set the x-coordinate of the bullet by the current x-coordinate of the rocket.
Add an argument rocket to the method draw_bullet of the class BULLET. Make sure that you can only fire the bullet if the bullet has not yet been fired (self.move[1] == 0). Compute the center of the rocket and set the position of the bullet (rocket.rocket_x + self.rocketImg.get_width() // 2):
class BULLET(ROCKET):
# [...]
def draw_bullet(self, rocket):
key = pygame.key.get_pressed()
if key[pygame.K_SPACE] and self.move[1] == 0:
rocket_center_x = rocket.rocket_x + self.rocketImg.get_width() // 2
self.bullet_x = rocket_center_x - self.bullet_width // 2
self.move[1] = -1
# [...]
Passe the instance of ROCKET to the method draw_bullet:
while True:
# [...]
bullet.draw_bullet(rocket)
# [...]
If you want to fire multiple bullets, see the answers to the questions:
How can i shoot a bullet with space bar?
How do I stop more than 1 bullet firing at once?
I'm new to Pygame, and I just started working on a Pong Game. The following code hasn't been finished yet, but for some reason, the ball won't move. It only works if I set the "ball_draw = pygame..." inside the "draw_ball" method, instead of the initializing method. However, if I do that, I can't use the "wallCollision" method in the class because the "ball_draw" would be a local variable and not an attribute of the whole class and therefore, not accessible in other methods. How can I fix this? I appreciate your help.
import pygame, sys
pygame.init()
clock = pygame.time.Clock()
screen_width = 1280
screen_height = 960
bgColor = pygame.Color("grey12")
lightGrey = (200, 200, 200)
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pong Game")
class BALL:
def __init__(self):
self.ball_width = 30
self.ball_height = 30
self.ball_x = screen_width/2 - self.ball_width
self.ball_y = screen_height/2 - self.ball_height
self.ball_speed_x = 7
self.ball_speed_y = 7
self.ball_draw = pygame.Rect(self.ball_x, self.ball_y, self.ball_width, self.ball_height)
def draw_ball(self):
pygame.draw.ellipse(screen, lightGrey, self.ball_draw)
def move_ball(self):
self.ball_x += self.ball_speed_x
self.ball_y += self.ball_speed_y
def wallCollision(self):
if self.ball_draw.bottom >= screen_height:
self.ball_speed_y *= -1
if self.ball_draw.top <= 0:
self.ball_speed_y *= -1
if self.ball_draw.left <= 0:
self.ball_speed_x *= -1
if self.ball_draw.right >= screen_width:
self.ball_speed_x *= -1
class PLAYER1:
def __init__(self):
self.player1_width = 10
self.player1_height = 140
self.player1_x = screen_width - 20
self.player1_y = screen_height/2 - 70
def draw_player1(self):
player1_draw = pygame.Rect(self.player1_x, self.player1_y, self.player1_width, self.player1_height)
pygame.draw.rect(screen, lightGrey, player1_draw)
def move_player1(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and self.player1_y >= 0:
self.player1_y -= 5
if keys[pygame.K_DOWN] and self.player1_y <= screen_height:
self.player1_y += 5
ball = BALL()
player1 = PLAYER1()
while True:
# Checking for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Drawing on screen
screen.fill(bgColor)
ball.draw_ball()
player1.draw_player1()
# Movement and Collisions
ball.move_ball()
player1.move_player1()
ball.wallCollision()
# Updating screen
pygame.display.flip()
# Defining 60 frames per second (FPS)
clock.tick(60)
You use the rectangle attribute self.ball_draw to draw the ball. Hence, if you move the ball and change the self.ball_x and self.ball_y attributes, you need to update the rectangle:
class BALL:
# [...]
def draw_ball(self):
pygame.draw.ellipse(screen, lightGrey, self.ball_draw)
def move_ball(self):
self.ball_x += self.ball_speed_x
self.ball_y += self.ball_speed_y
self.ball_draw.topleft = (self.ball_x, self.ball_y)
I'm trying to make a circle move from the middle of the screen to the top, and then back to the middle, and so on, using Pygame.
import pygame
import sys
pygame.init()
gameOver = False
speed = 5
clock = pygame.time.Clock()
fps = 20
class Screen:
largeur = 600
hauteur = 600
demiLargeur = int(largeur/2)
demiHauteur = int(hauteur/2)
screen = pygame.display.set_mode((Screen.largeur, Screen.hauteur))
class Couleurs:
bleu = (000,000,255)
noir = (000,000,000)
class Cercle:
diametre = 50
epaisseur = 5
posTop = [Screen.demiLargeur, Screen.demiHauteur-2*diametre]
class CirclePos:
top = [Cercle.posTop[0],Cercle.posTop[1]]
circleListTop = [CirclePos.top]
class DrawCircles:
def top (circleListTop):
for CirclePos.top in circleListTop:
pygame.draw.circle(screen, Couleurs.bleu, (CirclePos.top[0],CirclePos.top[1]),Cercle.diametre,Cercle.epaisseur)
class UpdateCirclesPositions:
def top(circleListTop):
for idx,CirclePos.top in enumerate(circleListTop):
if CirclePos.top[1] > Cercle.diametre :
CirclePos.top[1] -= speed
else:
circleListTop.pop(idx)
while not gameOver:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(Couleurs.noir)
clock.tick(fps)
DrawCircles.top(circleListTop)
UpdateCirclesPositions.top(circleListTop)
pygame.display.update()
I have this code so far, the idea being to make it move up, make it desappear, and then create another list to move circles from top to middle. I feel it's a bad idea.
Any idea ?
Thanks
As for me you have too much classes. In class Circle you should have its properties and methods update and draw.
When circle is at the top then it should change speed - speed = -speed. The same when it is in the middle. This way it may move all time. But it may need variable direction to check if it already changed direction because it can be in place where it may change speed all time (in every move).
import pygame
# --- constans ---
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
FPS = 20
SPEED = 5
# --- classes ---
class Colors:
blue = (000, 000, 255)
black = (000, 000, 000)
red = (255, 000, 000)
class Circle:
def __init__(self, x, y, size=50, thick=5, color=Colors.blue, speed=SPEED):
self.size = size
self.thick = thick
self.color = color
self.rect = pygame.Rect(0, 0, 2*size, 2*size)
self.rect.centerx = x
self.rect.centery = y
if speed >= 0:
self.direction = 'up'
else:
self.direction = 'down'
self.speed = speed
def draw(self, screen):
pygame.draw.circle(screen, self.color, self.rect.center, self.size, self.thick)
def update(self):
self.rect.y -= self.speed
if self.rect.top <= 0 and self.direction == 'up':
self.direction = 'down'
self.speed = -self.speed
if self.rect.bottom >= screen_rect.centery and self.direction == 'down':
self.direction = 'up'
self.speed = -self.speed
# --- main ---
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()
circles = [
Circle(screen_rect.centerx, screen_rect.centery),
Circle(screen_rect.centerx, 0, speed=-SPEED, color=Colors.red)
]
clock = pygame.time.Clock()
game_over = False
while not game_over:
# --- events ----
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# --- updates --- (without draws)
for item in circles:
item.update()
# --- draws --- (without updates)
screen.fill(Colors.black)
for item in circles:
item.draw(screen)
pygame.display.update()
clock.tick(FPS)
pygame.quit() # close window
I am fairly new to Pygame.
I am attempting to re-create Pong in Python using Pygame, but I have hit a roadblock.
Here is my code:
import pygame
pygame.init()
UP = "up"
DOWN = "down"
white = (255,255,255)
black = (0,0,0)
pygame.mouse.set_visible(True)
resolution = (800,600)
window = pygame.display.set_mode(resolution)
pygame.display.set_caption("Pong!")
clock = pygame.time.Clock()
sprites_list = pygame.sprite.Group()
running = True
class Paddle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((25,100))
self.image.fill(white)
pygame.draw.rect(self.image,white, (0,0,25,100))
self.rect = self.image.get_rect()
def move(self,direction,pixels):
if direction == DOWN:
self.rect.y -= pixels
if direction == UP:
self.rect.y += pixels
player1 = Paddle()
player1.rect.x = 0
player1.rect.y = 200
sprites_list.add(player1)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
player1.move(DOWN, 5)
if keys[pygame.K_UP]:
player1.move(UP, 5)
sprites_list.update()
sprites_list.draw(window)
pygame.display.flip()
clock.tick(60)
pygame.quit()
I am trying to make player1, a Paddle object, move up or down depending on which key is pressed. When I run this code, player1 is stretched out after the up or down arrows are pressed; 5 pixels are added onto player1.rect.
What am I doing wrong and why will sprites_list.update() not put player1 in its new position?
You have to clear screen before you draw elements in new loop.
You can fill window with black color window.fill((0,0,0)) or draw background image in every loop.
This is your code after reorganization and adding window.fill(BLACK)
import pygame
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
UP = "up"
DOWN = "down"
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# --- classes ---- (CamelCaseNames)
class Paddle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((25,100))
self.image.fill(WHITE)
# you don't have to draw white rect if all surface already is white
#pygame.draw.rect(self.image,white, (0,0,25,100))
self.rect = self.image.get_rect()
def move(self, direction, pixels):
if direction == DOWN:
self.rect.y += pixels
if direction == UP:
self.rect.y -= pixels
# --- functions --- (lower_case_names)
# empty
# --- main --- (lower_case_names)
# - init -
pygame.init()
pygame.mouse.set_visible(True)
window = pygame.display.set_mode(resolution)
pygame.display.set_caption("Pong!")
# - objects -
sprites_list = pygame.sprite.Group()
player1 = Paddle()
player1.rect.x = 0
player1.rect.y = 200
sprites_list.add(player1)
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
player1.move(DOWN, 5)
if keys[pygame.K_UP]:
player1.move(UP, 5)
# - updates (without draws) -
sprites_list.update()
# - draws (without updates) -
window.fill(BLACK) # <--- clear screen
sprites_list.draw(window)
pygame.display.flip()
clock.tick(60)
# - end -
pygame.quit()