0

I'm making a game with a basketball moving and I want to make the image of the ball rotate for it to look realistic. I have seen many things on the internet but nothing works. On this program I used pygame.transform.rotate() but it does scarcely anything. What to do?

Here is my current program:


` Simple pygame program 

import pygame
import time 
import random
import winsound
import sys
pygame.init()
keys = pygame.key.get_pressed()
x = (250)
y = (375)
red = (255, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
green = (40, 180, 76)
yellow = (255, 255, 0)
orange = (255, 165, 0)
loop = 2
obx = 600
oby = 350
screen = pygame.display.set_mode([1500, 775])
pygame.display.set_caption("Game")
speed = (3)
running = True
while running:
# Did the user click the window close button?
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

# Fill the background with blue
screen.fill((200, 200, 255))

# Draw the ball

ball = pygame.image.load(r'C:\Users\xenia\Desktop\Python Games\game\basketball.png')
screen.blit(ball, (x, y))
ball = pygame.transform.scale(ball, (200, 20))
ballrect = ball.get_rect()

# Draw the terrain

grass = pygame.draw.rect(screen,green,(0, 425, 1000000000, 4000))
image1 = pygame.image.load(r'C:\Users\xenia\Desktop\Python Games\game\terrain..jpg')
screen.blit(image1, (0, 425))
ob = pygame.image.load(r'C:\Users\xenia\Desktop\Python Games\game\obstacle.jpg')
obe = ob.get_rect()
screen.blit(ob, (obx, oby))
# Key gets pressed
keys = pygame.key.get_pressed()

# If left arrow key is pressed then move left

if keys[pygame.K_LEFT] and x>0:

    #dicrease x coordinate to move left
    
    x -= speed
    ball = pygame.transform.rotate(ball, -10) #Rotating the ball to look realistic
    if x <= obx and x <= 750:
        obx += 0.1
        obx += 0.1
        obx += 0.1
        obx += 0.1
    if x >= obx and x >= 750:
        obx += 0.1
        obx += 0.1
        obx += 0.1
        obx += 0.1
    if keys[pygame.K_RIGHT] and x<1425:

    #increase x coordinate to move right
    
        x += speed
        ball = pygame.transform.rotate(ball, 10) # Rotating the ball to look realistic
        if x >= obx and x >= 750:
        obx -= 0.1
        obx -= 0.1
        obx -= 0.1
        obx -= 0.1
    if x <= obx and x <= 750:
        obx -= 0.1
        obx -= 0.1
        obx -= 0.1
        obx -= 0.1

if keys[pygame.K_UP] and y>10:
    print('2')  #(haven't added content here yet)`

As you can see when the right/left arrow is pressed the ball should be rotating. I have seen many questions on stack overflow but none of them works for me. What to do?

nick1234
  • 1
  • 1

1 Answers1

0

The main problem is:

ball = pygame.transform.rotate(ball, -10)

You have to keep the original image and rotate the original image e.g.:

angle -= 10
ball = pygame.transform.rotate(original_ball, angle)

This is all well explained in the answer to the question How do I rotate an image around its center using PyGame?.

Minimal example of rolling balls:

repl.it/@Rabbid76/PyGame-FollowBall

import math
import pygame

class MarbelSprite(pygame.sprite.Sprite):
    def __init__(self, x, ground, diameter, velocity, filename):
        pygame.sprite.Sprite.__init__(self)
        try:
            self.image = pygame.transform.smoothscale(pygame.image.load(filename).convert_alpha(), (diameter, diameter))
        except:     
            self.image = pygame.Surface((diameter, diameter), pygame.SRCALPHA)
            pygame.draw.circle(self.image, (255, 128, 0), (diameter // 2, diameter // 2), diameter // 2)
        self.original_image = self.image
        self.rect = self.image.get_rect(midbottom = (x, ground))
        self.diameter = diameter
        self.x = x
        self.velocity = velocity
        self.move_x = 0
        self.follow = None
        self.angle = 0
        
    def update(self, time, restriction):
        move_x = 0
        prev_x = self.x
        if self.move_x != 0:
            move_x = self.move_x * self.velocity * time
        elif self.follow:
            dx = self.follow.rect.centerx - self.x
            move_x = (-1 if dx < 0 else 1) * min(self.velocity * time, abs(dx))
        self.x += move_x
        self.x = max(restriction.left + self.diameter // 2, min(restriction.right - self.diameter // 2, self.x))
        self.rect.centerx = round(self.x)
        self.angle -= (self.x - prev_x) / self.diameter * 180 / math.pi
        self.image = pygame.transform.rotate(self.original_image, self.angle)
        self.rect = self.image.get_rect(center = self.rect.center)

pygame.init()
window = pygame.display.set_mode((500, 300))
clock = pygame.time.Clock()

ground_level = 220
object = MarbelSprite(window.get_rect().centerx, ground_level, 100, 0.4, 'BaskteBall64.png')
follower = MarbelSprite(window.get_width() // 4, ground_level, 50, 0.2, 'TennisBall64.png')
all_sprites = pygame.sprite.Group([object, follower])

run = True
while run:
    time = clock.tick(60)
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    object.move_x = keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]
    follower.follow = object
    all_sprites.update(time, window.get_rect())

    window.fill((32, 64, 224))
    pygame.draw.rect(window, (80, 64, 64), (0, ground_level, window.get_width(), window.get_height()-ground_level))
    all_sprites.draw(window)
    pygame.display.flip()

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174