Rotating an image using pygame [duplicate] - python

This question already has answers here:
How do I rotate an image around its center using Pygame?
(6 answers)
How can you rotate an image around an off center pivot in Pygame
(1 answer)
How to rotate an image(player) to the mouse direction?
(2 answers)
Closed 2 years ago.
I'm trying to rotate an image using pygame. Here is my code thus far:
def main(self):
self.size = width, height = 500, 500
self.black = 0,0,0
self.screen = pygame.display.set_mode(self.size)
self.ball = pygame.image.load("deathstar.png")
self.ballrect = self.ball.get_rect()
self.ballcenter = self.ballrect.center
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEMOTION:
print ("mouse at ", event.pos)
self.rotate_image(some angle)
self.screen.fill(self.black)
self.screen.blit(self.ball, self.ballrect)
pygame.display.flip()
def rotate_image(self, angle):
How would I define rotate_image? I need it to rotate based on where the mouse is placed on the screen. Thanks!

Related

How to fix this : "invalid destination position for blit'? [duplicate]

This question already has answers here:
Invalid destination position for blit error, not seeing how
(1 answer)
How to draw images and sprites in pygame?
(4 answers)
Closed last month.
import pygame
class popUpWindow:
#Initialization of Window
def __init__(self,capNa,width,height):
self.screen = pygame.init()
self.capNa = capNa
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width,self.height))
pygame.display.set_caption(self.capNa)
#Make Player
self.px = float
self.py = float
self.psrc = 'Images/player.png'
self.player_load = pygame.image.load(self.psrc)
def LoadPlayer(self):
self.screen.blit(self.player_load, (self.px,self.py))
pygame.display.update()
def runWindow(self):
self.__init__(self.capNa,self.width,self.height)
self.Is_running = True
self.player_load()
while self.Is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.update()
def closeWindow(self):
pygame.quit()
I tried to make the code change the player position but I failed!
How can I change the player positions at any time, without returning back to the code.

Move sprite in pygame without flickering [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Why is the PyGame animation is flickering
(1 answer)
Closed 2 years ago.
So I've been playing around in pygame, and I can draw and use the update method to move my sprite, but when I clear the screen the sprite flickers. How do I move the sprite without the flickering
Here's the code:
class Sprite(pygame.sprite.Sprite):
def __init__(self, pos):
super(Sprite, self).__init__()
self.image = demon
self.rect = self.image.get_rect(center = pos)
def update(self, moveX, moveY):
self.rect.x += moveX
self.rect.y += moveY
screen.fill(BLACK)
The typical Pygame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
Remove screen.fill(BLACK) form update:
class Sprite(pygame.sprite.Sprite):
# [...]
def update(self, moveX, moveY):
self.rect.x += moveX
self.rect.y += moveY
# screen.fill(BLACK) <--- DELETE
Implement the following application loop (based on your previous question):
while carryOn == True:
# handle events
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
# update
# [...]
# clear display
screen.fill(BLACK)
# draw sprites
all_sprites_list.draw(screen)
# update display
pygame.display.flip()
clock.tick(60)

Why does the background scroll without me moving? [duplicate]

This question already has answers here:
How to move the player across a one background image?
(1 answer)
How to scroll the background surface in PyGame?
(1 answer)
Closed 1 year ago.
What I want to do is make the background move when my player moves forward, as the background contains a story-line of sorts. Instead the background is just moving on it's own and cannot be stopped.
Here is all the relevant code:
paccpos=(0,0)
bgX=0
pX, pY = paccpos
while 1:
events = pygame.event.get()
for e in events:
if e.type == QUIT:
exit()
if e.type == KEYDOWN and e.key == K_ESCAPE:
exit()
if pX+1:
bgX -= 1.4
screen.blit(bg_img, (bgX, 0))
entities.update(dt, events)
entities.draw(screen)
pygame.display.update()
dt = timer.tick(60)
class Player(Entity):
def __init__(self, platforms, pos, *groups):
super().__init__((color),(pygame.image.load("lemon3.png")), pos)
self.vel = pygame.Vector2((0, 0))
self.onGround = False
self.platforms = platforms
self.speed = 8
self.jump_strength = 6
self.pos=pos
def globpos(self):
global paccpos
paccpos=self.pos
def update(self, dt, events):
pressed = pygame.key.get_pressed()
up = pressed[K_UP]
left = pressed[K_LEFT]
right = pressed[K_RIGHT]
running = pressed[K_SPACE]

Background in pygame not working [duplicate]

This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Why is my PyGame application not running at all?
(2 answers)
Closed 2 years ago.
import pygame
import sys
import os
basic stuff
'''
Objects
'''
# put Python classes and functions here
'''
Setup #...
'''
# put run-once code here
fps = 60
ani = 4
clock = pygame.time.Clock()
pygame.init()
I think here might be a problem:
On the internet I saw something with .convert() but it keeps giving me :
AttributeError: 'str' object has no attribute 'convert'
when I try to put .conver()
it's been 2 hours of searching and I can't find this noob stuff...
background = pygame.image.load(os.path.join('background.png'))
size = (width, height) = background.get_size()
world = pygame.display.set_mode(size)
...
'''
Main Loop
'''
or here:
world.blit(background, (0,0))
...
while main == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == ord('q'):
pygame.quit()
sys.exit()
main = False
pygame.display.flip()
clock.tick(fps)

How do I make something happen when the mouse cursor hovers over an image [duplicate]

This question already has answers here:
How do I detect if the mouse is hovering over a button? PyGame button class is not displaying the text or changing colour on hover
(1 answer)
How to detect when a rectangular object, image or sprite is clicked
(1 answer)
Closed 2 years ago.
Ok so I am working on a home screen for my game and I have an image that acts as a button.
So when you click some where on the image the image changes then imports the next part of the game. But what I want to do as well is when the mouse hovers over the image it will play a sound. But how do I get it to detect when the mouse is being hovered over the button image?
Here is a copy of my code for the home screen. ps. I figured out my problem now you can seem my code here.(This is all the code so far for my home screen. Any was thanks to any one who can help me make it detect when the mouse in hovering over the image.)
import pygame, sys, random
import time
B_images = ["startbutton.png", "startbuttonpush.png"]
class BClass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("startbutton.png")
self.rect = self.image.get_rect()
self.rect.center = [310, 500]
def animate():
screen.fill([0,0,0])
screen.blit(B.image,B.rect)
pygame.display.flip()
pygame.init()
x = y = 0
pos = pygame.mouse.get_pos()
pygame.display.set_caption("Skier")
screen = pygame.display.set_mode([640,640])
B = BClass()
font = pygame.font.Font(None, 50)
ButtonSound = pygame.mixer.Sound('ButtonSound.ogg')
while True:
animate()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
import End.py
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if ( x in range(229,391)) and (y in range(470,530)):
B.image = pygame.image.load("startbuttonpush.png")
animate()
time.sleep(0.1)
import skier.py
To detect a mouse hover, do the same thing as detecting the mouse click, except do it on the pygame.MOUSEMOTION event. This is called each time a mouse movement is detected.
if event.type == pygame.MOUSEMOTION:
x, y = event.pos
if ( x in range(229,391)) and (y in range(470,530)):
print "Hovering over image!"
Also note that x in range(229, 391) is super-inefficient, and you should do 229 <= x <= 391 instead. And you should not hard-code those coordinates, eventually.
I used to do this on my buttons in pygame as well.
You should consider this code:
def is_hovering():
mouse = pygame.mouse.get_pos()
if button_object.rect.collidepoint(mouse):
return True
else:
return False

Categories