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?