Python-Pygame sprite bounces when gravity is used - python-3.x

I am making a 8bit style platformer. The player falls and gains speed because of the pseudo gravity but he will fall a few pixels into the ground level. Without gravity he will land on the ground and not fall though but it is a constant fall speed.When in the ground you can go up but he will fall when you let up. He will not got down so that is not an issue for now. Any help would be appreciated.
The player class/file.
import pygame,sys
from pygame.locals import *
class Player:
x=0
y=0
offset = 5
L=False
R=False
U=False
D=False
image = None
gravity = .25
velocity = offset
objectDict = None #this si the list of the current objects so that collision can be check with every
#object.. get updated every loop to keep a accurate check of locations
rect = None
grav = True #TODO use this to check if we are paying attention to the gravity
def __init__(self,x,y):
self.x = x
self.y = y
self.image = pygame.image.load('Resources/Pics/player.png')
def draw(self,DISPLAY):
#print('draw will go here')
imgRect = self.image.get_rect()
imgRect.midleft = (self.x,self.y)
self.rect = imgRect
DISPLAY.blit(self.image, imgRect)
#and now im here
def checkCollide(self,otherRect):
return self.rect.colliderect(otherRect)
def checkCollideAll(self):
if(self.objectDict != None):
# print(len(self.objectDict))
# for x in range(1,len(self.objectDict)):
# newb = self.checkCollide(self.objectDict[x].getRect())
# print(self.objectDict[x].getRect())
# if(newb):
# return True
# return False
collideNum = self.rect.collidelist(self.objectDict)
if(collideNum == -1):
return False
else:
return True
def willCollideBelow(self):
if(self.objectDict):
checkRect = (self.x,(self.y),self.image.get_size())
collideNum = self.rect.collidelist(self.objectDict)
if collideNum == -1:
return False
else:
return True
def objUpdate(self,dict):
self.objectDict = dict
def getRect(self):
return self.rect
def update(self):
# while(self.checkCollideAll()):
# print('while happened')
# self.y -= self.offset
# imgRect = self.image.get_rect()
# imgRect.midleft = (self.x,self.y)
# self.rect = imgRect
# print(self.willCollideBelow())
if not self.willCollideBelow():
self.D = True
# print('will fall')
else:
self.D = False
if self.U == True:
self.y -= self.offset
if self.D == True:
self.y += self.velocity
if not self.velocity >= 9.8:
self.velocity += self.gravity
else:
self.velocity = self.offset
if self.L == True:
self.x -= self.offset
if self.R == True:
self.x += self.offset

You didn't provide a running example and your code is hard to read (pascal case, a lot of unnecessary parenthesis), but here's my guess:
In your willCollideBelow function, you check if you hit an object beneath the player:
def willCollideBelow(self):
if(self.objectDict):
checkRect = (self.x,(self.y),self.image.get_size())
collideNum = self.rect.collidelist(self.objectDict)
if collideNum == -1:
return False
else:
return True
instead of just returning True or False, return the object (or the index of the object) you actually collide with:
def will_collide_below(self):
if(self.objectDict):
# using 'collidelistall' would be better, but that's another topic
return self.rect.collidelist(self.objectDict)
Now that you know which object the player collides with, you can adjust the vertical position of the player:
ground_i = self.will_collide_below()
if ground_i:
ground = self.objectDict[ground_i]
self.velocity = 0
self.rect.bottom = ground.top # or self.y = ground.top
You'll get the idea.
Some more notes:
You use different variables to store the position of the player (I see x, y, rect and imgRect). It would make you code a lot simpler if you would just use a single Rect to store the position:
class Player:
...
def __init__(self,x,y):
self.image = pygame.image.load('Resources/Pics/player.png')
self.rect = self.image.get_rect(midleft=(x,y))
def draw(self, display):
display.blit(self.image, self.rect)
def update(self):
...
if self.L: # no need to check == True
self.rect.move_ip(-self.offset)
if self.R: # simply use move_ip to alter the position
self.rect.move_ip(self.offset)
You also use a bunch of class variables where you really should use instance variables, like rect, L, R, U and D.

Related

Memory game using pygame

I am making a memory game for an assignment. I can get all the tiles to be covered and make them reveal themselves upon being clicked on but I cannot cover them after they have been clicked on or match them. I dont know how to match the tiles also.
class Tile:
surface = None
border_size = 3
border_color = pygame.Color('black')
# An object in this class represents a Dot that moves
#classmethod
def set_surface(cls,game_surface):
cls.surface = game_surface
# instance method
def __init__(self,x , y, image, cover):
self.image = image
self.cover = cover
self.covered = True
self.time_cover = None
# self.timer = pygame.time.get_ticks()
width = self.image.get_width()
height = self.image.get_height()
self.rect = pygame.Rect(x, y, width, height)
def draw(self):
pygame.draw.rect(Tile.surface,Tile.border_color,self.rect,Tile.border_size)
Tile.surface.blit(self.image,self.rect)
if self.covered:
Tile.surface.blit(self.cover, self.rect)
def select(self, position):
valid_click = False
if self.rect.collidepoint(position):
if self.covered:
valid_click = True
self.expose_tile()
self.time_cover = pygame.time.get_ticks() + 2000
self.update()
else:
valid_click = False
return valid_click
def update(self):
if not self.covered and self.time_cover >= 2000:
self.covered = True
return self.covered
def expose_tile(self):
# if a tile is clicked this method will show the pic ture underneath that tile
self.covered = False
def __eq__ (self, other_tile):
pass
When you call update() in the main application loop, then revealed tiles will be covered after 2 seconds.
But you can add a cover_tile method, too:
class Tile:
# [...]
def cover_tile(self):
self.covered = True
If matching tiles share the same image (self.image), then matching tiles can be identified by comparing the images. e.g.:
(In the following tileA and tileB are instances of Tile)
if tielA.image != tileB.image:
tileA.cover_tile()
tileB.cover_tile()
else
print("matching")

How does one make collisions for sprites in pygame?

I'm trying to make collisions for my basic 2D game using pygame. I've run into an issue where the game won't start and i'm getting an error.
playercolidecheck = MyPlayer.collide(v)
File "C:\Users\marti\Desktop\Python\PyMine\PyMineFile.py", line 53,
in collide
return pygame.sprite.spritecollideany(self, enemies)
File "C:\Users\marti\AppData\Local\Programs\Python\Python37\lib\site-
packages\pygame\sprite.py", line 1586, in spritecollideany
spritecollide = sprite.rect.colliderect
AttributeError: 'Player' object has no attribute 'rect'
I have tried to to make my own custom collision but to no avail. Here is the code for the project:
import pygame
pygame.mixer.init()
pygame.init()
win = pygame.display.set_mode((800,600))
pygame.display.set_caption("PyMine")
clock = pygame.time.Clock()
#Images
dirtimage = pygame.image.load("dirt.png")
grassimage = pygame.image.load("grass.png")
woodimage = pygame.image.load("wood.png")
playerimage = pygame.image.load("player.png")
pygame.display.set_icon(grassimage)
drawlist = []
class Block:
def __init__(self,ID,x,y,add,w,h):
self.ID = ID
self.x = x
self.y = y
self.w = w
self.h = h
if add == True:
drawlist.append(self)
def main(self):
if self.ID == 0:
win.blit(dirtimage, (self.x,self.y))
if self.ID == 1:
win.blit(grassimage, (self.x,self.y))
if self.ID == 2:
win.blit(woodimage, (self.x,self.y))
class Player:
def __init__(self,add,x,y,):
self.x = x
self.y = y
if add == True:
drawlist.append(self)
def main(self):
win.blit(playerimage, (self.x,self.y))
def DONTUSE(self,TargetX,TargetY):
if TargetX > self.x or TargetX < self.x or TargetY > self.y or TargetY < self.y: return False
return True
def collide(self, enemies):
return pygame.sprite.spritecollideany(self, enemies)
MyBlock1 = Block(0,160,160,True,32,32)
MyBlock2 = Block(1,32,0,True,32,32)
MyBlock3 = Block(2,64,0,True,32,32)
MyPlayer = Player(False,96,0)
run = True
while run:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
MyPlayer.x -= 32
if event.key == pygame.K_RIGHT:
MyPlayer.x += 32
win.fill((255,255,255))
MyPlayer.main()
for v in drawlist:
v.main()
playercolidecheck = MyPlayer.collide(v)
if playercolidecheck == True:
MyPlayer.y -= 0.1
pygame.draw.rect(win,(255,25,25),[MyPlayer.x,MyPlayer.y,16,16])
MyPlayer.y += 0.1
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
It gives me an error and it doesn't display anything on the screen. Altough it opens a window and names it and gives it its icon.
Your Player class does not have a pygame rect object, which is required for the spritecollideany function. So passing self into this line:
return pygame.sprite.spritecollideany(self, enemies)
You are passing your own custom class Player to the function, you need to add a rect object to your class so pygame knows where it is.
So instead of using self.x self.y for player you should have a self.rect initialized at the start and use self.rect.x, self.rect.y for those variables. Making sure to update this rect as your player moves. This puts things in a format that pygame understands.

Positioning an Entity in a Class (Pygame)

I'm trying to blit an image of grass onto my game in a specific area, but I'm having trouble doing so because it's in a class. I want to do something like this..
grass_platform.draw(screen, (200, 200))
NOTE: I know this actually doesn't work
Here's my code...
import pygame
pygame.init()
#Screen Size
screen_size = [1024,576]
#Display Window
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('The Adventures of Fresco the Explorer')
#Clock
clock = pygame.time.Clock()
#Colors
black = (0,0,0)
white = (255,255,255)
#Game Start
gameStart = False
#Backgrounds
forest = pygame.image.load('forest.png')
#Gravity
gravity =-10
fall = True
#Player
fresco = pygame.image.load('fresco v2.png').convert()
fresco = pygame.transform.scale(fresco,(32,136))
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, filename):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.image = fresco
self.rect = self.image.get_rect()
def update (self):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
self.rect.x -= 6
self.image = pygame.transform.flip(fresco, True, False)
elif event.key == pygame.K_d:
self.rect.x += 6
self.image = pygame.transform.flip(fresco, False, False)
elif event.key == pygame.K_w:
self.rect.y -= 20
def draw(self, screen):
screen.blit(self.image, self.rect)
player = Player(0, 0, 'fresco v2.png')
#Grass Platform
grass = pygame.image.load('grass.png')
grass = pygame.transform.scale(grass, (90, 90))
class Grass (pygame.sprite.Sprite):
def __init__(self, filename):
self.image = grass
self.rect = self.image.get_rect()
def draw(self, screen):
screen.blit(self.image,self.rect)
grass_platform = Grass('grass.png')
#Game Loop
while not gameStart:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameStart = True
#Background Blitted
screen.blit(forest,(0,0))
#Falling Event
if fall == True:
if gravity:
player.rect.y -= gravity
#Class Blitted
player.update()
player.draw(screen)
grass_platform.draw(screen)
#Updates Screen
pygame.display.update()
#FPS
clock.tick(30)
pygame.quit()
I dont know if i understood you correctly but if you mean just passing a coordinate parameter and blitting to that position that should be quite easy.
class Grass (pygame.sprite.Sprite):
def __init__(self, filename):
self.image = grass
self.rect = self.image.get_rect()
def draw(self, screen, pos):
screen.blit(self.image, pos)
and then when you draw() you just do this:
grass_platform.draw(screen, (300, 200))
or on the other hand if you want to keep the position you could add
this code to the init method:
def __init__(self, filename, x, y):
self.x = x
self.y = y
and then acces it later
def draw(self, screen):
window.blit(img, (self.x, self.y))
Some notes:
Player and Grass are subclasses from Sprite, so they have already a draw function which does exactly what you are doing. Just remove them.
the following part
#Falling Event
if fall == True:
if gravity:
player.rect.y -= gravity
is specific to the Player class and should be moved to the Player class' update function
When using the Sprite class, the rect attribute is used to store the position of the sprite, hence the x and y attribute in your Player class don't make much sense. Remove them, and use the x and y parameters to change the x and y attribute of the rect instead:
self.rect = self.image.get_rect(x=x, y=y)
Hence, if you want to draw grass_platform at a specific position, just alter it's rect, e.g.:
grass_platform.rect.topleft = (200, 200)
instead of calling draw and update on each of your sprites manually, just but them into a Group, and call draw and update on that Group.

Increase level when score reaches hundreds place

I have a game I am working on and am trying to increase the level every time I get to a hundred place, i.e., 100, 200, 300...etc.
Here is my function and it is probably something very simple I am forgetting to do
Thanks to #redunderthebed I discovered that the function was in the __init__() and that it was not getting into the if statement. So I moved my level changing into the if loop. So I moved it to my main() method and it still is not going into the if loop when the score goes to a 100 position. What am I doing wrong here?
# Pizza Panic
# Player must catch falling pizzas before they hit the ground
from livewires import games, color
from random import randrange
games.init(screen_width=640, screen_height=480, fps=50)
class Pan(games.Sprite):
""" A pan controlled by player to catch falling pizzas """
image = games.load_image("pan.bmp")
def __init__(self):
""" Initialize the pan object and create Text object for score """
super(Pan, self).__init__(image=Pan.image,
x=games.mouse.x,
bottom=games.screen.height)
self.score = games.Text(value=0, size=25, color=color.black,
top=5, right=games.screen.width - 10)
games.screen.add(self.score)
self.level = 0
def update(self):
""" move to mouse x position """
self.x = games.mouse.x
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
self.check_catch()
def check_catch(self):
""" Check if catch pizzas """
for pizza in self.overlapping_sprites:
self.score.value += 10
self.score.right = games.screen.width - 10
pizza.handle_caught()
class Pizza(games.Sprite):
""" a pizza which falls to the ground """
image = games.load_image("pizza.bmp")
speed = 1
def __init__(self, x, y=90):
""" Initialize a pizza object """
super(Pizza, self).__init__(image=Pizza.image,
x=x, y=y,
dy=Pizza.speed)
def update(self):
""" Check if bottom edge has reached screen bottom """
if self.bottom > games.screen.height:
self.end_game()
self.destroy()
def handle_caught(self):
""" Destroy self if caught """
self.destroy()
def end_game(self):
""" End the game """
end_message = games.Message(value="Game Over",
size=90,
color=color.red,
x=games.screen.width / 2,
y=games.screen.height / 2,
lifetime=5 * games.screen.fps,
after_death=games.screen.quit)
games.screen.add(end_message)
class Chef(games.Sprite):
""" A chef which moves left and right, dropping pizzas """
image = games.load_image("chef.bmp")
def __init__(self, y=55, speed=2, odds_change=200):
""" Initialize the chef object """
super(Chef, self).__init__(image=Chef.image,
x=games.screen.width / 2,
y=y,
dx=speed)
self.odds_change = odds_change
self.time_til_drop = 0
def update(self):
""" Determine if direction needs to be reversed """
if self.left < 0 or self.right > games.screen.width:
self.dx = -self.dx
elif randrange(self.odds_change) == 0:
self.dx = -self.dx
self.check_drop()
def check_drop(self):
""" Decrease countdown or drop pizza and reset countdown """
if self.time_til_drop > 0:
self.time_til_drop -= 1
else:
new_pizza = Pizza(x=self.x)
games.screen.add(new_pizza)
# set buffer to approx 30% of pizza height, regardless of pizza
# speed
self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1
def main():
""" Play the game """
wall_image = games.load_image("wall.jpg", transparent=False)
games.screen.background = wall_image
the_chef = Chef()
games.screen.add(the_chef)
the_pan = Pan()
games.screen.add(the_pan)
score = the_pan.score.value
if score % 100 == 0:
the_pan.level += 1
the_pan.level_msg = games.Message(value="LEVEL {}"
.format(the_pan.level),
size=90,
color=color.red,
x=games.screen.width / 2,
y=games.screen.height / 2,
lifetime=5 * games.screen.fps,
after_death=None)
games.screen.add(the_pan.level_msg)
pizza = Pizza(x=0)
pizza.speed += 1
else:
print("not getting into the if statement")
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
# start it up
main()
I couldn't find any real documentation for Livewires, but I'm going to say based on your code (and assuming it's doing anything) you need to put your level check in here:
def update(self):
""" move to mouse x position """
self.x = games.mouse.x
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
self.check_catch()
self.check_level()
Perhaps check_level()
def check_level(self):
if not self.score.value % 100:
self.level += 1
self.level_msg = games.Message(value="LEVEL {}"
.format(the_pan.level),
size=90,
color=color.red,
x=games.screen.width / 2,
y=games.screen.height / 2,
lifetime=5 * games.screen.fps,
after_death=None)
games.screen.add(the_pan.level_msg)

Python, please help me that why my rect cant move

I was trying to make a basic game using pygame, but the rectangle i created can not move after i press the key, please teach me how to fix it,thank you
import pygame, sys, time
from pygame.locals import *
class Tile:
def __init__(self,surface):
self.surface = surface
self.x = 250
self.y = 200
self.position = (self.x, self.y)
self.color = pygame.Color('red')
self.speed = 5
self.size = 30
self.rect = pygame.Rect(self.position[0],self.position[1],self.size,self.size)
def draw(self):
pygame.draw.rect(self.surface,self.color,self.rect)
def moveUp(self):
if self.rect.top < self.speed:
self.speed = self.rect.top
self.y = self.y - self.speed
def moveDown(self):
maxBottom = self.surface.get_height()
if maxBottom - self.rect.bottom < self.speed:
self.speed = maxBottom - self.rect.bottom
self.y = self.y + self.speed
def moveLeft(self):
if self.rect.left < self.speed:
self.speed = self.rect.left
self.x = self.x - self.speed
def moveRight(self):
maxRight = self.surface.get_width()
if maxRight - self.rect.right < self.speed:
self.speed = maxRight - self.rect.right
self.x = self.x + self.speed
def move(self,key):
if key == K_w:
self.moveUp()
if key == K_s:
self.moveDown()
if key == K_a:
self.moveLeft()
if key == K_d:
self.moveRight()
def main():
pygame.init()
pygame.key.set_repeat(20, 20)
surfaceSize = (500, 400)
windowTitle = 'Pong'
frameDelay = 0.01
surface = pygame.display.set_mode(surfaceSize, 0, 0)
pygame.display.set_caption(windowTitle)
gameOver = False
tile = Tile(surface)
tile.draw()
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and not gameOver:
tile.move(event.key)
pygame.display.update()
time.sleep(frameDelay)
main()
I was trying to make a basic game using pygame, but the rectangle i created can not move after i press the key, please teach me how to fix it,thank you
You're only updating your own x and y variables, and not the PyGame ones for the rect, you can fix it quickest in the draw function, but it's really an updating operation so should go in those functions:
def draw(self):
self.rect.top = self.y # You need to update the position of the rect
self.rect.left = self.x # and not just your own x, y variables.
pygame.draw.rect(self.surface,self.color,self.rect)
Then your indentation for updating the display is off, but you need an extra line anyway:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and not gameOver:
tile.move(event.key)
surface.fill((0, 0, 0)) # Fill the screen with black to hide old rect
tile.draw() # Re-draw the tile in new position
pygame.display.update() # Update the display
Last thing to fix, don't use time.sleep for the frame delay, use PyGame's inbuilt clock function. Here's a tutorial: https://www.youtube.com/watch?v=pNjSyBlbl_Q&nohtml5=False
Your if statements looks suspect to me:
if self.rect.left < self.speed
How is self.speed related to self.rect.left?
You should instead be shifting the entire rect by self.speed in the direction you want. Try that.
I also find adding print statements throughout the code and looking at the console can help a lot. An even better solution would be to use a debugger.

Resources