My main code in the loop works fine by itself but for some reason this wont move and stays on the first image. Please help. Im new to this. The main loop executes fine by itself in another program but when i add the thing at the start to create the base it doesnt work. My only theory is that the base thing has to be in the main loop?
import pygame
pygame.init()
Window = pygame.display.set_mode((480,48))
pygame.display.set_caption("Mario Animation")
black = (0,0,0)
#Takes in the image for the base
Base1 = pygame.image.load("images/Base1.png")
Base2 = pygame.image.load("images/Base2.png")
Base3 = pygame.image.load("images/Base3.png")
#Takes the sprites for mario
MarioRunning1 = pygame.image.load("images/Mario1.png")
MarioRunning2 = pygame.image.load("images/Mario1.png")
MarioRunning3 = pygame.image.load("images/Mario1.png")
BaseX = 0
clock = pygame.time.Clock()
for i in range (10):
Window.blit(Base1, ((BaseX,32)))
BaseX=BaseX+16
Window.blit(Base2, ((BaseX,32)))
BaseX=BaseX+16
Window.blit(Base3, ((BaseX,32)))
BaseX=BaseX+16
CurrentImage = 1
MainLoop = True
while MainLoop:
for event in pygame.event.get():
if (event.type==pygame.QUIT):
MainLoop = False
if (CurrentImage == 1):
Window.blit(MarioRunning3, (0,0))
if (CurrentImage == 2):
Window.blit(MarioRunning2, (0,0))
if (CurrentImage == 3):
Window.blit(MarioRunning1, (0,0))
if (CurrentImage == 3):
CurrentImage = 1
else:
CurrentImage+=1
pygame.display.flip()
clock.tick(5)
pygame.quit()
As #ChristianRapp said, the MarioRunning variables all refer to the said image, Mario1.png. Try changing that chunk to:
MarioRunning1 = pygame.image.load("images/Mario1.png")
MarioRunning2 = pygame.image.load("images/Mario2.png")
MarioRunning3 = pygame.image.load("images/Mario3.png")
That's assuming you have the 3 Mario sprites necessary. It also seems that you're never changing CurrentImage when you swap to an image, so it will always be 1, and will never change. Try:
if (CurrentImage == 1):
Window.blit(MarioRunning2, (0,0))
CurrentImage = 2
elif (CurrentImage == 2):
Window.blit(MarioRunning3, (0,0))
CurrentImage == 3
else:
Window.blit(MarioRunning1, (0,0))
CurrentImage = 1
This way, it always goes to the next one in the cycle. Oh, and the reason the image won't move, is you're not telling it to. For example, the (0, 0) part of the line Window.blit(MarioRunning1, (0,0)) tells pygame where to put the image, in pixels. Changing these numbers will change where the image is put.
Related
I'm making a game with pygame and want to show an introduction. My issue is I want the player to be able to click through each slide until the end, where it would transition to the game itself. Currently I have a loop where it goes through all of the slides immediately.
Is there a way I can format this so a player could click slide by slide? Thank you!
I've tried various types of looping and iteration. A work around I found was to make a ton of different while statements, e.g. "while intro1" go into "while intro2", but it is very cumbersome. I'm hoping there's a better way.
#introduction screen
while intro:
img1 = pygame.image.load("./sprites/img1.png")
img2 = pygame.image.load("./sprites/img2.png")
img3 = pygame.image.load("./sprites/img3.png")
img4 = pygame.image.load("./sprites/img4.png")
imgs = [img1, img2, img3, img4]
display_surface = pygame.display.set_mode((0, 0))
display_surface.blit(img1, (0, 0))
pygame.display.update()
events = pygame.event.get()
for event in events:
if event.type == pygame.MOUSEBUTTONUP:
for i in range(len(imgs)):
display_surface.blit(imgs[i], (0, 0))
i += 1
pygame.display.update()
if i >= 3:
intro = False
run = True
A bit of reorganizing is in order. First off, you will keep track of which image you are on using current_image. It is set to 0 before the loop, meaning we'll start at the first image.
The images themselves don't change on each pass through the loop. They only need to be loaded once BEFORE the loop.
You have too much in your for event in events: loop. In here you should do the bare minimum changes that a certain key press will change. So on click, we change the current_image to increase by 1 and that's it.
After everything, you blit the image to the screen and update. Hopefully this helps.
#introduction screen
current_image = 0
img_names = ["./sprites/img1.png", "./sprites/img2.png", "./sprites/img3.png", "./sprites/img4.png"]
imgs = [pygame.image.load(img) for img in img_names]
display_surface = pygame.display.set_mode((0, 0))
while intro:
events = pygame.event.get()
for event in events:
if event.type == pygame.MOUSEBUTTONUP:
current_image +=1
display_surface.blit(imgs[current_image], (0, 0))
pygame.display.update()
if current_image >= 3:
intro = False
run = True
I am developing a small game for learning purposes. I have created a simple animation for the title screen. Since there is also a function for full screen in the code, I wanted to create a title screen that:
Displayed the animation
Turned into full screen when the key was activated
Continued the animation at the point it was before activating full screen
In order to do this, I resorted to threading. However, this is the first time I tried to do any multi-threading, and I donĀ“t know what did I do wrong. The result is an undetermined error.
The code for the title screen is this:
try:
GameAnimation = threading.Thread(target=GameTitleAnimation, (Window, WindowDimensions, FontDictionary, CurrentVersion))
GameAnimation.start()
except:
print "There was an error while loading the screen. Press one key to exit the program."
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Quit()
if event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
Quit()
elif event.key == K_f:
Fullscreen(Window, WindowDimensions)
else:
return
The code for the animation is:
TitleWhite = [255, 255, 255, 0]
Black = BASE_BLACK
TitleLetters = ("R", "O", "G", "U", "E", " ", "H", "U", "N", "T", "E", "R")
Title = FontDictionary["TitleFont"][1].render("ROGUE HUNTER", False, TitleWhite)
TextWidth = Title.get_width()
TextHeight = Title.get_height()
TitleXPosition = (WindowDimensions[0] - TextWidth) / 2
TitleYPosition = (WindowDimensions[1] / 2) - (TextHeight / 2)
for letter in TitleLetters:
if letter == " ":
TitleXPosition += CurrentLetterWidth
else:
while TitleWhite[3] < 100:
TitleWhite[3] += 1
CurrentLetter = FontDictionary["TitleFont"][1].render(letter, False, TitleWhite)
CurrentLetter.set_alpha(TitleWhite[3])
Window.blit(CurrentLetter, (TitleXPosition, TitleYPosition))
time.sleep(0.008)
try:
pygame.display.update()
except Exception:
traceback.print_exception
TitleWhite[3] = 0
CurrentLetterWidth = CurrentLetter.get_width()
TitleXPosition += CurrentLetterWidth
FadeInSurface = pygame.Surface((WindowDimensions[0], WindowDimensions[1]))
FadeInSurface.fill(TitleWhite)
OpacityRounds = 1
while TitleWhite[3] < 100.0:
TitleWhite[3] = 1.1 ** OpacityRounds
FadeInSurface.set_alpha(TitleWhite[3])
Window.blit(FadeInSurface, (0, 0))
OpacityRounds += 1
pygame.display.update()
time.sleep (0.015)
time.sleep(0.7)
TitleXPosition = (WindowDimensions[0] - TextWidth) / 2
Version = FontDictionary["BodyFont"][1].render(CURRENT_VERSION, False, TitleWhite)
VersionHeight = Version.get_height()
VersionWidth = Version.get_width()
VersionXPosition = (WindowDimensions[0] - VersionWidth) / 2
VersionYPosition = TitleYPosition + TextHeight
while True:
pygame.draw.rect(Window, Black, (0, 0, WindowDimensions[0], WindowDimensions[1]), 0)
Window.blit(Title, (TitleXPosition, TitleYPosition))
Window.blit(Version, (VersionXPosition, VersionYPosition))
pygame.display.update()
I'd be very grateful if anyone could help me with this. I am going crazy.
There's no reason to use threading in your code. It will only make your code harder to read, harder to debug and error prone.
Usually you want to have some kind of state in your game that you use to determinate what should happen in a frame. You can find a class based example here.
Another way to handle this, which is a bit similar to your code, is to use coroutines.
Look at your animation code and instead of calling pygame.display.update(), give the control back to the main loop. The main loop will handle events, frame limiting and drawing, then give control back to the coroutine (which keeps track of it's own state).
Here's a simple hacky example:
import pygame
import pygame.freetype
pygame.init()
size = (640, 480)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
def game_state(surf):
rect = pygame.Rect(200, 200, 32, 32)
while True:
events = yield
pressed = pygame.key.get_pressed()
x = 1 if pressed[pygame.K_RIGHT] else -1 if pressed[pygame.K_LEFT] else 0
rect.move_ip(x*5, 0)
pygame.draw.rect(surf, pygame.Color('dodgerblue'), rect)
yield
def title_state(surf):
text = 'Awesome Game'
colors = [[255, 255, 255, 20] for letter in text]
font = pygame.freetype.SysFont(None, 22)
font.origin = True
while True:
for color in colors:
color[3] += 33
if color[3] > 255: color[3] = 0
x = 200
for (letter, c) in zip(text, colors):
bounds = font.get_rect(letter)
font.render_to(surf, (x, 100), letter, c)
x += bounds.width + 1
font.render_to(surf, (180, 150), 'press [space] to start', pygame.Color('grey'))
events = yield
yield
def main():
title = title_state(screen)
game = game_state(screen)
state = title
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE:
return
if e.key == pygame.K_SPACE:
state = game if state == title else title
if e.key == pygame.K_f:
if screen.get_flags() & pygame.FULLSCREEN:
pygame.display.set_mode(size)
else:
pygame.display.set_mode(size, pygame.FULLSCREEN)
screen.fill(pygame.Color('grey12'))
next(state)
state.send(events)
pygame.display.update()
clock.tick(60)
if __name__ == '__main__':
main()
See how the main loop is clean and simple, and all of the game state is handled in the coroutines. The title screen part of the code does not care about fullscreen or not or how to switch to fullscreen, and the main loop does not care of what the title screen coroutine does. And we don't need threading.
In practice it's not that different from the class based example I linked above, but using coroutines makes it easy to implement the title screen animation.
Basically you have an endless loop, you mutate some state (like the color of a letter), and then say "now draw this!" by just calling yield.
This is a large chunk of code to debug.
I'm not familiar with pygame or python threading, but it seems to me that you need to include some debug lines to determine exactly where the error occurs during your game animation thread (if it's even occuring there at all).
Something like this pattern should help determine the source of the problem:
import logging
logging.info("start animation initialization")
...
logging.info("begin animation loop")
...
logging.info("end animation loop")
https://docs.python.org/2/howto/logging.html
How do I convert an image to another image in pygame without using sprite class? Also how can I remove the previous image after I convert it to another one?
I wrote a small program today that shows how I switch an objects image(it may help/answer your question). It has notes for most of the code's use so it is easier to understand how and why it works(for all I know, anyone could have started programming yesterday).
Anyway, here is the code:
import pygame, sys
#initializes pygame
pygame.init()
#sets pygame display width and height
screen = pygame.display.set_mode((600, 600))
#loads images
background = pygame.image.load("background.png").convert_alpha()
firstImage = pygame.image.load("firstImage.png").convert_alpha()
secondImage = pygame.image.load("secondImage.png").convert_alpha()
#object
class Player:
def __init__(self):
#add images to the object
self.image1 = firstImage
self.image2 = secondImage
#instance of Player
p = Player()
#variable for the image switch
image = 1
#x and y coords for the images
x = 150
y = 150
#main program loop
while True:
#places background
screen.blit(background, (0, 0))
#places the image selected
if image == 1:
screen.blit(p.image1, (x, y))
elif image == 2:
screen.blit(p.image2, (x, y))
#checks if you do something
for event in pygame.event.get():
#checks if that something you do is press a button
if event.type == pygame.KEYDOWN:
#quits program when escape key pressed
if event.key == pygame.K_ESCAPE:
sys.exit()
#checks if down arrow pressed
if event.key == pygame.K_DOWN:
#checks which image is active
if image == 1:
#switches to image not active
image = 2
elif image == 2:
image = 1
#updates the screen
pygame.display.update()
I am not sure how your code is set up or if this is what you need (I don't entirely understand classes either so it might be a sprite class), but I hope this helps!
Converting one image to another is as simple as reassigning the variable
firstImage = pygame.image.load("firstImage.png")
secondImage = pygame.image.load("secondImage.png")
firstImage = secondImage
del secondImage
I'm not sure what exactly you mean by removing the image. You could use "del secondImage" to delete the reference in your code and send it to garbage collection. Once you clear the screen and blit the updated image there should no longer be any sign of the outdated image.
I'm making a basic game where I have a surface and everytime I click on the surface it moves 5 pixels to the right. The program is working just fine without the checkCollide(event) function, but when I put the that condition it doesn't move. What is wrong?
My code until now is this
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((300,300))
def checkCollide(event):
k = 0
a,b = event.pos
x = P1[0].get_rect()
if x.collidepoint(a,b):
return True
return False
CP1 = [(150, 150)
,(155, 150)
,(160, 150)
,(165, 150)
,(170, 150)
,(175, 150)
,(180, 150)
,(185, 150)
,(190, 150)]
statp1_1 = 0
WHITE = (255,255,255)
DISPLAYSURF.fill(WHITE)
while True: # the main game loop
P1 = [pygame.image.load('PAzul.png'),CP1[statp1_1],statp1_1]
DISPLAYSURF.blit(P1[0], P1[1])
e = pygame.event.get()
for event in e:
if event.type == MOUSEBUTTONUP:
a = checkCollide(event)
if a:
DISPLAYSURF.fill(WHITE)
statp1_1 +=1
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Thank you
Check your logic in these lines of your function:
x = P1[0][0].get_rect()
if x.collidepoint(a,b):
return True
return False
Your code hinges on this bit:
a = checkCollide(event)
if a:
DISPLAYSURF.fill(WHITE)
So you're never evaluating this piece to be true.
I just realized what was wrong. When I do x = P1[0].get_rect() it creates a surface with topleft at (0,0).
What I needed to do was change the position of the rectangle using x.topleft = P1[1]
I've got some tips for you. First store the rect in the P1 list (it contains only the image and the rect in the following example, but maybe you could also add the statp1_1 index to it). Now we can just move this rect, if the user clicks on it (in the example I set the topleft attribute to the next point). Read the comments for some more tips. One thing you need to fix is to prevent the game from crashing when the statp1_1 index gets too big.
import sys
import pygame
pygame.init()
DISPLAYSURF = pygame.display.set_mode((300, 300))
WHITE = (255, 255, 255)
# Don't load images in your while loop, otherwise they have to
# be loaded again and again from your hard drive.
# Also, convert loaded images to improve the performance.
P1_IMAGE = pygame.image.load('PAzul.png').convert() # or .convert_alpha()
# Look up `list comprehension` if you don't know what this is.
CP1 = [(150+x, 150) for x in range(0, 41, 5)]
statp1_1 = 0
# Now P1 just contains the image and the rect which stores the position.
P1 = [P1_IMAGE, P1_IMAGE.get_rect(topleft=CP1[statp1_1])]
clock = pygame.time.Clock() # Use this clock to limit the frame rate.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
if P1[1].collidepoint(event.pos):
print('clicked')
statp1_1 += 1
# Set the rect.topleft attribute to CP1[statp1_1].
P1[1].topleft = CP1[statp1_1]
DISPLAYSURF.fill(WHITE)
DISPLAYSURF.blit(P1[0], P1[1]) # Blit image at rect.topleft.
pygame.display.update()
clock.tick(30) # Limit frame rate to 30 fps.
In my previous question For Loop Functions in Python,
I had trouble with putting functions that contained a command to draw a line for a hangman game. It didn't exactly draw the line, and I first suspected it was a problem with the for loop or the functions. Now I realize there is somewhat a glitch with Pygame.
I have tried solving the problem by using this code in the country, CANADA:
b2 = font.render(str(letters[1]), True, (red))
screen.blit(b2, (bPosition))
if hangman1x == -500 and hangman1y == -500:
hangman1x = (775, 250)
hangman1y = (775, 50)
pygame.draw.line(screen, black, (hangman1x), (hangman1y), (5))
pygame.display.flip()
time.sleep(0.5)
bPosition = -500, -500
b1.x, b1.y = -500, -500
if hangman1x == (775, 250) and hangman1y == (775, 50):
print 'hi'
width = 6
pygame.draw.line(screen, black, (hangman1x), (hangman1y), (5))
print 'yay'
pygame.display.flip()
Now here's the weird thing.
When you press the B blitted onto the screen, it turns red, like its meant to, draws the line perfectly fine, but disappears, when the B disappears, and I understand why. After that, I added that extra if code. (Notice that both pygame.draw.line(s) are the same), It prints hi and yay in the shell, but it does not keep the line. Anyway to solve this?
After you are calling pygame.draw.line() you are probably redrawing your screen completely white, this will draw over the line and hide it. Instead of drawing lines like you are, I would build a hangman class draw from that
class Hangman():
def __init__(self):
self.lines = 0 #Number of lines to be drawn
def draw(self,screen):
#TODO draw to screen based on self.lines
#More code setting up pygame
drawlist = []
myMan = Hangman()
drawlist.append(myMan)
#mainloop
while 1:
screen.fill('#000000')
for item in drawlist:
item.draw(screen)
This way you are redrawing you hangman every frame, and thus he is always being showed
EDIT Added a running example
#!/usr/bin/python
import pygame
pygame.init()
class Hangman():
def __init__(self):
self.lines = 0 #Number of lines to be drawn
def hang(self):
self.lines += 1
def draw(self,screen):
for x in range(self.lines):
coord1 = (x*10,20)
coord2 = (x*10,50)
pygame.draw.line(screen,(0,0,0),coord1,coord2)
size = screenWidth,screenHeight = 200,70
screen = pygame.display.set_mode(size)
pygame.display.flip()
myman = Hangman()
drawlist = []
drawlist.append(myman)
#mainloop
running = True
while running:
#EVENT HANDLING#
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == 32: #Spacebar
myman.hang()
#DRAWING#
screen.fill((255,255,255))
for item in drawlist:
item.draw(screen)
pygame.display.flip()