[Tutor] Hello
Chukwunonso Oranyeli
Chukwunonso.Oranyeli at ssfs.org
Mon Jan 11 17:34:00 EST 2021
I am going to send my code. Right now I have a ball moving fast and bumping on the edges of my screen. I want the code to be set up in a way that when the ball hits the bottom of my screen, it resets back to the center. Could you help me with this?
import pygame
class Ball(pygame.sprite.Sprite):
def __init__(self, color, windowWidth, windowHeight, radius,):
# initialize sprite super class
super().__init__()
# finish setting the class variables to the parameters
self.color = color
self.windowWidth= windowWidth
self.windowHeight= windowHeight
self.radius= radius
# Create a surface, get the rect coordinates, fill the surface with a white color (or whatever color the
# background of your breakout game will be.
self.image = pygame.Surface((self.radius*2,self.radius*2))
pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
self.rect =self.image.get_rect()
self.x_speed = 5
self.y_speed = 3
# Add a circle to represent the ball to the surface just created. Just use the pygame.draw.circle method.
# The surface will be self.image
pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
# Give the ball an initial speed. You will need a speed for the x direction and one for the y direction.
self.x_speed= 5
self.y_speed= 3
def move(self):
self.rect.x += self.x_speed
self.rect.y += self.y_speed
if self.rect.top < 0 or self.rect.bottom > self.windowWidth:
self.y_speed = -self.y_speed
if self.rect.left< 0 or self.rect.right > self.windowHeight:
self.x_speed= -self.x_speed
More information about the Tutor
mailing list