Im following a tutorial to make a game working with some pygame tiled and pytmx stuff, but however, we added a new object´s layer with some empty rectangles over the tiles where the player is not being able to enter, then we maked a wall´s list to put inside this "walls" and then we maded a couple of functions to check if the player is over the collision obstacle, and if that´s the case move the player one step back, but when I run the game, it opens the map and I can actually move my player around, but when I test the obstacle surface and collisions, it´s not working, and the player can go over the "walls". could you please help me to solve this, here I leave my wall´s code (inside of my Game Class):
def update(self):
self.group.update()
#verification du collision
for sprite in self.group.sprites():
if sprite.feet.collidelist(self.walls) > - 1:
sprite.move_back()
def run(self):
clock = pygame.time.Clock()
#boucle du jeu
running = True
while running:
self.player.save_location()
self.handle_input()
self.update()
self.group.center(self.player.rect)
self.group.draw(self.screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
clock.tick(60)
pygame.quit()
But... on the tutorial in Tiled software on the Obstacle information, there´s an option to put the "type" of object, at least that says on the tutorial, but in my Tiled, says "Class" instead of " Type" I think is the same, but however, I tried to put Class instead of Type in collision code, but... you know that Class is a reserved word by python, so... I put back " Type", I´m not sure if this sort of information matters, but maybe yes,
in my tmx_map, on the obstacles layer I already set the "class" ("type object") of each object as "Collision"
anyway, here´s the Player´s code
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.sprite_sheet = pygame.image.load('player.png')
self.image = self.get_image(0,0)
self.image.set_colorkey([0,0,0])
self.rect = self.image.get_rect()
self.position = [x,y]
self.images = {
'down':self.get_image(0,0),
'left' :self.get_image(0,32),
'right':self.get_image(0,64),
'up' :self.get_image(0,96),
}
self.feet = pygame.Rect(0,0, self.rect.width * 0.5, 12)
self.old_position = self.position.copy()
self.speed = 2
def save_location(self): self.old_position = self.position.copy()
def update(self):
self.rect.topleft = self.position
self.feet.midbottom = self.rect.midbottom
def move_back(self):
self.position = self.old_position
self.rect.topleft = self.position
self.feet.midbottom = self.rect.midbottom
thanks in advance
Since Tiled 1.9, "type" was renamed to "class", for consistency between all different kinds of objects, which can now all have a custom class.
Likely pytmx has not been updated yet with this change. Fortunately, since this change affected compatibility, Tiled provides an option to use the "Tiled 1.8" format, in which case the class is still written out as "type".
You can change the "Compatibility version" option in the Project Properties (you need to create a Tiled project to set this option).
Related
So I'm making a platformer in pygame and I was quite far along in the code when I changed how the levels where gonna be set up so I now needed to have the player start at the top of the screen instead of the bottom so it was simple I just needed to change the attributes witch I call into my player class so tat the position can change as that's how it was set in the first place but the player just kept starting in the same place
player = player(100,screen_height+100)
def tryagain(self,x,y):
player1 = pygame.image.load('guy.png')
self.image = pygame.transform.scale(player1, (40,75))
self.rect = self.image.get_rect()
self.rect.center = (x,y)
2
class player(): def __init__(self, x, y): self.tryagain(x,y)
screen.blit(self.image, self.rect)
i even coded all the code from the start but now the Collisions with platforms refuse to work even thoi never chaged that code
The top left of the pygame coordinate system is (0, 0):
player = player(100,screen_height+100)
player = player(100, 100)
Essentially, I am trying to display multiple projectiles on the screen for the player to avoid.
At this point, I'm not worried about hit boxes or anything like that, just simply trying to get multiple projectiles to display. It displays one, then the projectile disappears off the screen and never shows back up.
I believe this may be a scope issue, but I'm not entirely sure. I have moved around pretty much every piece of code I can think of to no avail.
class Game:
clock = pygame.time.Clock()
def __init__(self):
self.enemy = Projectile()
self.enemies = []
def loop(self):
self.clock.tick(30)
for enemy in self.enemies:
if self.enemy.y < 925 and self.enemy.x < self.screen_width:
self.enemy.x += self.enemy.velocity
self.enemy.frame_count += 1
else:
self.enemies.pop(self.enemies.index(self.enemy))
if len(self.enemies) < 5:
self.enemies.append(self.enemy)
def render(self):
# other stuff
for enemy in self.enemies:
self.enemy.draw_projectile(self._display_surf)
And then in my projectile file I have:
class Projectile():
# projectile images
def __init__(self):
# stuff
def draw(self, gameDisplay):
if self.frame_count > 118:
self.frame_count = 0
display_window.blit(self.projectile_sprites[self.frame_count],(self.x, self.y))
I'm trying to get multiple projectile-type enemies on the screen. I looked over a few tutorials on how to do this type of thing and I can't get the expected results.
Few things need to be fixed. Not sure these fixs will be enough, but at least they will help.
Class Projectile seems ok sintactically. I cannot speak for the omitted parts, though.
However, you never use the gameDisplay argument in the draw function, but you have a display_window. Are they supposed to be the same thing? If so, use the same name.
I guess display_window / gameDisplay is the display, the surface initialized with pygame.display.set_mode(). If so, is fine.
For the Game class, see the comments I added.
class Game:
clock = pygame.time.Clock()
def __init__(self):
#self.enemy = Projectile() remove this line, is useless.
self.enemies = []
def loop(self):
self.clock.tick(30)
for enemy in self.enemies:
if enemy.y < 925 and enemy.x < self.screen_width:
#no need of self before enemy in this section. Otherwise you
#do not refer to the "enemy" on which you are looping but to
#the attribute "enemy" (which I removed). They are different things.
enemy.x += enemy.velocity
enemy.frame_count += 1
else:
self.enemies.pop(self.enemies.index(enemy))
#I leave this here for now, but remember that editing
#a list on which you are looping is a bad idea in general.
if len(self.enemies) < 5:
self.enemies.append(Projectile())
#each iteration you need to create a new instance of the class.
def render(self):
# other stuff
for enemy in self.enemies:
enemy.draw(self._display_surf)
#again, no self before enemy, same reason of before.
I do not know what is draw_projectile(). I suppose you meant to use the draw() method of Projectile class, it makes sense thos way.
Again self._display_surf is not defined in your code, I supposed is the display which you pass to the draw() function.
I am creating a tile-based 2d overworld for a game - heavily influenced by Pokemon - using pygame/python, Tiled for .tmx files, and the tmx library by Richard Jones. The code I'm using is mostly based on this wonderful demo of Pallet Town in python.
The game runs just fine, however, I am having problems with making tiles on the map (e.g. houses, trees) overlap the player sprite when it would make sense for the player sprite to disappear behind them. For example: in the image here, principles of depth perception would tell us that the house in the foreground should occlude the player in the background, but because the map is 2D there is no depth and therefore no occlusion. I would love to add depth, but seeing as I'm very new to pygame (and python in general), I'm at a loss at how to draw the relevant foreground objects over the sprite.
Luckily I'm not alone in this problem and plenty of documentation on possible solutions exist. For example:
this StackExchange question
this LibGDX tutorial
this Unity tutorial
However, this code isn't typically written for python and I'm not sure how to implement it in my situation. Sorting/drawing by z position (or by a 'depth' property) seems like the most sensible thing to do, but looking at the tmx library I can only find x and y values mentioned. Adding the player sprite to an empty object layer in Tiled is also a solution, but once again I'm unsure of how to do this and all my attempts have led to error messages. (Attempts not detailed here because I honestly don't know what I did and it didn't work anyway.)
My current code is as follows:
class Player(pygame.sprite.Sprite):
def __init__(self, location, collStart, orientation, *groups):
super(Player, self).__init__(*groups)
self.image = pygame.image.load('sprites/player.png')
self.imageDefault = self.image.copy()
self.rect = pygame.Rect(location, (26,26))
self.collider = pygame.Rect(collStart, (13,13))
self.orient = orientation
self.holdTime = 0
self.walking = False
self.dx = 0
self.step = 'rightFoot'
# Set default orientation
self.setSprite()
self.speed = pygame.time.get_ticks() + 50 # slows down walking speed
by .5 sec (current time + 50 ms)
def setSprite(self):
# this function contains information about where to find which sprite
in the sprite sheet, probably not relevant here.
def update(self, dt, game):
key = pygame.key.get_pressed()
if pygame.time.get_ticks() >= self.speed:
self.speed = pygame.time.get_ticks() + 50
# Setting orientation and sprite based on key input, removed the
#code here because it wasn't relevant
#[....]
# Walking mode enabled if a button is held for 0.1 seconds
if self.holdTime >= 100:
self.walking = True
lastRect = self.rect.copy()
lastColl = self.collider.copy() # collider covers the bottom section of the sprite
# Code for walking in the direction the player is facing, not relevant here
#[....]
# Collision detection:
# Reset to the previous rectangle if player collides
# with anything in the foreground layer
if len(game.tilemap.layers['triggers'].collide(self.collider,
'solid')) > 0:
self.rect = lastRect
self.collider = lastColl
# Area entry detection, loads dialog screen from the dialog file:
elif len(game.tilemap.layers['triggers'].collide(self.collider,
'entry')) > 0:
entryCell = game.tilemap.layers['triggers'].find('entry')[0]
game.fadeOut()
run()
pygame.quit()
quit()
return
if self.dx == 16:
# Makes the player appear to take steps w/ different feet, not relevant here
#[....]
# After traversing 32 pixels, the walking animation is done
if self.dx == 32:
self.walking = False
self.setSprite()
self.dx = 0
game.tilemap.set_focus(self.rect.x, self.rect.y)
class Game(object):
def __init__(self, screen):
self.screen = screen
def initArea(self, mapFile):
"""Load maps and initialize sprite layers for each new area"""
self.tilemap = tmx.load(mapFile, screen.get_size())
self.players = tmx.SpriteLayer()
self.objects = tmx.SpriteLayer()
# In case there is no sprite layer for the current map
except KeyError:
pass
else:
self.tilemap.layers.append(self.objects)
# Initializing player sprite
startCell = self.tilemap.layers['triggers'].find('playerStart')[0]
self.player = Player((startCell.px, startCell.py), (startCell.px,
startCell.bottom-4),
startCell['playerStart'], self.players)
self.tilemap.layers.append(self.players)
self.tilemap.set_focus(self.player.rect.x, self.player.rect.y)
def main(self):
clock = pygame.time.Clock()
self.initArea('test tilemap.tmx')
while 1:
dt = clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
self.tilemap.update(dt, self)
screen.fill((0,0,0))
self.tilemap.draw(self.screen)
pygame.display.flip()
Once again, I'm using the tmx library found here. Maybe something needs to be changed there? Hopefully, someone can help me figure this out. It's definitely possible, as shown by this pokemon clone in python (no source code available, sadly).
Also, first-time StackOverflow user so let me know if I'm committing any faux-passes :)
Figured it out! As suggested by Kingsley in the comments, the solution was to change the draw order of the layers. Layers were being drawn in order of a list in the Layers class, with the player sprite having the highest index and thus being drawn on top of everything else. Moving the player between the background and foreground layer in the list made it appear behind the foreground objects.
To do this I added the following code to the initArea function in the Game class:
def initArea(self, mapFile):
"""Load maps and initialize sprite layers for each new area"""
self.tilemap = tmx.load(mapFile, screen.get_size())
self.players = tmx.SpriteLayer()
self.objects = tmx.SpriteLayer()
# Initializing player sprite
startCell = self.tilemap.layers['triggers'].find('playerStart')[0]
self.player = Player((startCell.px, startCell.py), (startCell.px, startCell.bottom-4),
startCell['playerStart'], self.players)
foregroundItem = self.tilemap.layers.__getitem__("foreground") # finds the layer called foreground
foregroundIndex = self.tilemap.layers.index(foregroundItem) # finds the position of the foreground layer in the Layers list (Layers class specified in .tmx file)
self.tilemap.layers.insert(foregroundIndex-1, self.players) # move the Player layer one layer below the foreground layer
self.tilemap.set_focus(self.player.rect.x, self.player.rect.y)
I'll experiment a bit more tonight, but for now this solution seems to work. Thanks for the help!
So i have been creating a game with sprites and groups. Everything is going really well aside from that one thing that keeps bugging me. A picture says more than a thousand words:
http://i.imgur.com/S3fsone.png
As you can see, the background image is shown in the crosshair, just like it should. The ship, however, is getting cut off by rectangular white edges. Both the crosshair and ship are sprites, and the images used in them are transparent PNG images.
I have no idea how to remove this. Currently i am experimenting on the crosshair, so with that in mind, i'll post the relevant code:
#Ship class
class Ship(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.imageoriginal = pygame.image.load("ship.png")
self.image = self.imageoriginal
self.rect = self.image.get_rect()
self.rect.center = position
#Crosshair class
class Pointer(pygame.sprite.Sprite):
def __init__(self, image):
super(Pointer, self).__init__()
self.image = image.convert_alpha()
self.rect = self.image.get_rect()
def update(self):
self.rect.center = pygame.mouse.get_pos()
def main():
pygame.init()
#Window and background
window = pygame.display.set_mode((1000, 600), SRCALPHA)
background = pygame.image.load("background.png")
window.blit(background, (0, 0))
#Ship and ship group
ship = Ship((490, 500))
theship = pygame.sprite.Group(ship)
#Crosshair image and group
crosshairimage = pygame.image.load("images\\crosshair.png")
pointer = pygame.sprite.Group()
#Game loop
running = True
while running:
clock.tick(60)
#Cut out from my event loop. Creates the crosshair that follows the mouse
for e in pygame.event.get():
elif e.key == K_4:
pointer.add(Pointer(crosshairimage))
#Update and redraw methods for both
theship.clear(window, background)
theship.update(window)
theship.draw(window)
pointer.clear(window, background)
pointer.update()
pointer.draw(window)
pygame.display.flip()
And that should be it. The crosshair follows the mouse perfectly, and the non-red part of it is transparent on the background. It is not transparent over other sprites however, and that is the ghist of the problem. Other sprites moving over other sprites also cause this problem.
I can guess that it is the rect in the sprites that causes this, but i wouldn't know how to fix it. Tried using a mask but i kind of need the rects to be able to move my sprites don't i? Found no equal to the "self.rect.center = position" method in the mask class.
Much appreciated if anyone could tell me what i can do about this, it's been a headache for a long time now.
I think the problem is in your drawing section. Instead of having each group clear and draw separately, clear both groups, then update them, then draw them, so instead of:
#Update and redraw methods for both
theship.clear(window, background)
theship.update(window)
theship.draw(window)
pointer.clear(window, background)
pointer.update()
pointer.draw(window)
pygame.display.flip()
have:
#Update and redraw methods for both
theship.clear(window, background)
pointer.clear(window, background)
theship.update(window)
pointer.update()
theship.draw(window)
pointer.draw(window)
pygame.display.flip()
I hope this helps.
Unimportant Preamble:
Hello, I'm using Python and Pygame to create a game. This is for the purpose of improving my programming skills rather than a serious attempt at game creation. I've taken a break from Python lately for Objective C, but I'm now returning to it. This is a problem that I was having before I took a brief break, and I've returned to a question that was originally puzzling me. I've spent quite a while researching it, and I have a suspicion I've come across the solution and merely failed to understand it. I apologize for some of the bad naming conventions/lack of comments. I'm working on improving that.
Substance of Question:
Anyway, I've attached the four images I'm using. The program uses a simple function to position various Tiles on the screen. The mouse cursor is a sword. It is the entire image, but I'll be changing that later. I've made the program type "blue" in the shell whenever the cursor collides with a Tile. My goal is to have this happen when it collides with "ANY" tile of that color.
Long-term, I want to be able to modify the properties of these tile sprites. Various game-pieces would interact, and I would need to save the state of each sprite. I'd also be setting interactions for the other sprites.
Right now the sprites are all generating images, but my collision rectangle for the Tile is simply moving after each image is generated. I suppose that makes sense given the code, but I need a way to multiple sprites, each with a rectangle for collision.
Thanks
EDIT: I was unable to add images due to a new-user restriction. They are available enter link description here I think I read somewhere that people can (and do) edit posts here. So if anyone who the ability to move the images into this thread is welcome to do so.
import random,math,sys,os
import pygame
from pygame.locals import *
pygame.init() #Initializing Pygame
#Colors
black=(0,0,0)
#Screen
screen=pygame.display.set_mode((1200,800),0,0)
pygame.display.set_caption("Nero's Sandbox")
pygame.mouse.set_visible(False)
clock=pygame.time.Clock()
fps=40
#Game Functions:
def terminate():
pygame.quit()
sys.exit()
def numgen(x,y):
return random.randint(x,y)
#Loop Variables
tri=2
#Groups:
allsprites = pygame.sprite.Group()
alltiles = pygame.sprite.Group()
allmice = pygame.sprite.Group()
#Mouse Classes
class Pointy(pygame.sprite.DirtySprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('redsword.png').convert() #31x32 image
self.image.set_colorkey(black)
self.rect=self.image.get_rect()
self.set=pygame.sprite.Group()
self.add(allmice, allsprites, self.set)
pygame.sprite.RenderPlain((self.set,allmice,allsprites))
def update(self):
screen.fill(black)
alltiles.draw(screen)
if event.type == pygame.MOUSEMOTION:
pos = pygame.mouse.get_pos()
self.rect.topright = pos
self.set.draw(screen)
#Tile Sprites - only one rect is being recognized.
class Tile(pygame.sprite.Sprite):
def __init__(self, graphic):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(graphic).convert()
self.image = pygame.transform.scale((self.image),(50,50))
self.rect=self.image.get_rect()
self.add(alltiles, allsprites)
self.set=pygame.sprite.RenderPlain((self))
def update(self, x, y):
pos = (x,y)
self.rect.topleft = pos
#Micers
pointy1=Pointy()
#Game Loops
while True: #Ensures all loops within program are constantly called when conditions are met.
screen.fill(black)
while tri==2:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
pygame.display.flip()
x = 0
y = 50
w = 0
while x!=600:
x=x+50
w = w+1
if w%2==0:
purpletile1=Tile('purplesquare.png')
purpletile1.set.update(x,y)
purpletile1.set.draw(screen)
else:
c=numgen(1,2)
if c==1:
bluetile1=Tile('lightbluesquare.png')
bluetile1.set.update(x,y)
bluetile1.set.draw(screen)
if c==2:
redtile1=Tile('redsquare.png')
redtile1.set.update(x,y)
redtile1.set.draw(screen)
if x>=600 and y!=450:
if y<450:
x = 0
y = y+50
w=w-1
if y>=450:
tri=3
while tri==3:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
alltiles.draw(screen)
pointy1.set.update()
pointy1.set.draw(screen)
pygame.display.flip()
clock.tick(fps)
if pygame.sprite.collide_rect(pointy1,bluetile1):
print('blue')
I had this same problem myself! I did some debugging, and it appeared that all instances of my class shared the same instance of pygame.Rect()
You may want to change the line:
pygame.sprite.Sprite.__init__(self)
to
super.__init__(self)
This way, pygame.sprite.Sprite's init will set attributes to your tile. I could be wrong, I'm not entirely familiar with python's inheritance syntax, but that is the way I do it.
I could also be the get_rect that is causing the same rectangle to be used for all classes, but that doesn't seem to be likely.
I hope that I was some help, and just remember that pygame.Rect is an object, so somehow you are instantiating only once.