I am not sure how I add gravity to my game? - python-3.x

I am confused on how I add gravity to my platformer game. I am using pygame for this project. I am relatively new to coding, but anyways here's my code.
import pygame
pygame.init()
win = pygame.display.set_mode((800, 500))
pygame.display.set_caption("Platformer")
x = 200
y = 200
width = 20
height = 20
vel = 5
run = True
FPS = 30
fpsClock = pygame.time.Clock()
while run:
pygame.time.delay(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and x > 0:
x -= vel
if keys[pygame.K_d] and x < 800 - width:
x += vel
if keys[pygame.K_w] and y > 0:
y -= vel
if keys[pygame.K_s] and y < 500 - height:
y += vel
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.display.update()
fpsClock.tick(FPS)
I hope someone can help me get some sort of gravity function working, anything helps. (also what is the best way to make pixelated art, not very relevant to the question)

Gravity just means that the object is moved down a little in each frame:
gravity = 3
while run:
# [...]
y += gravity
if y > win.get_height() - 20:
y = win.get_height() - 20
I suggest to use a pygame.Rect object to represent the player:
import pygame
pygame.init()
win = pygame.display.set_mode((800, 500))
pygame.display.set_caption("Platformer")
player = pygame.Rect(200, 200, 20, 20)
vel = 5
gravity = 3
run = True
FPS = 30
fpsClock = pygame.time.Clock()
while run:
fpsClock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
player.x += (keys[pygame.K_d] - keys[pygame.K_a]) * vel
if player.left < 0:
player.left = 0
if player.right > win.get_width():
player.right = win.get_width()
player.y += gravity
if player.bottom > win.get_height():
player.bottom = win.get_height()
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), player)
pygame.display.update()
pygame.quit()

Related

Trying to get ball to return to middle after ball has left screen

I'm trying to recreate a game of pong just for fun.
Here is my full code as of right now.
import pygame, random, math
pygame.init()
#colours:-------------------------------------------------------------
R = random.randrange(1,255)
B = random.randrange(1,255)
G = random.randrange(1,255)
WHITE = (255, 255, 255)
GREEN = (39, 133, 20)
YELLOW = (252, 252, 25)
BLACK = (0, 0, 0)
BLUE = (30, 100, 225)
RED = (255,0,0)
RANDOM_COLOR = (R, B, G)
#Surface:-------------------------------------------------------------
width = 700
height = 600
size = (width, height)
screen = pygame.display.set_mode(size)
screen_rect = screen.get_rect()
pygame.display.set_caption("Pong Remake")
background = pygame.image.load("background.png").convert()
background = pygame.transform.scale(background, (width, height))
logo = pygame.image.load("logo.png").convert()
logo.set_colorkey((BLACK))
credits = pygame.image.load("credits.png")
credits.set_colorkey((BLACK))
#variables:-----------------------------------------------------------
clock = pygame.time.Clock()
done = False
text = pygame.font.Font(None,25)
display_instructions = True
instruction_page = 1
start_font = pygame.font.Font("C:\Windows\Fonts\BAUHS93.TTF", 35)
instruction_font = pygame.font.Font(None, 17)
win_lose_font = pygame.font.Font("C:\Windows\Fonts\BAUHS93.TTF",50)
score = pygame.font.Font(None, 100)
bounce = pygame.mixer.Sound("bounce.wav")
playerOne_score = 0
playerTwo_score = 0
playerOne = ""
playerTwo = ""
x = 350
y = 300
ball_rect = pygame.Rect(x,y,10,10)
paddleOne_rect = pygame.Rect(10, 250, 20, 60)
paddleTwo_rect = pygame.Rect(670, 250, 20, 60)
x_speed = random.randrange(5, 10)
y_speed = random.randrange(5,10)
def draw_background(screen, pic, x,y):
screen.blit(pic, (x,y))
#main loop
#INPUT v ---------------------------------------------------------
#Start Page
while not done and display_instructions:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
instruction_page += 1
if instruction_page == 2:
display_instructions = False
#Shows the start of the page
if instruction_page == 1:
draw_background(screen, logo, 100,-150)
draw_background(screen, credits, 100,50)
instruction_text = instruction_font.render("How to Play. The objective to this game is to score the ball on the other side before the opponent can.", False, WHITE)
instruction_text_three = instruction_font.render("First Player to get 10 points wins, Have Fun and Good Luck!", False, WHITE)
instruction_text_two = instruction_font.render("For Player One, use the a and the z keys to move up and down, For Player Two, use the k and m keys.", False, WHITE)
continue_text= start_font.render("Click to Play...",True, WHITE)
screen.blit(continue_text, [200, 400])
screen.blit(instruction_text, [0,500])
screen.blit(instruction_text_three, [0,532])
screen.blit(instruction_text_two,[0,516])
if instruction_page == 2:
display_instructions = False
clock.tick(60)
pygame.display.flip()
while not done:
click = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
click = True
#INPUT ^ =========================================================
#PROCESS v -------------------------------------------------------
str(playerOne_score)
str(playerTwo_score)
scoreOne = text.render("Player One:" + str(playerOne_score), False, WHITE)
scoreTwo = text.render("Player Two:" + str(playerTwo_score), False, WHITE)
#moves paddles with keys on keyboar
key = pygame.key.get_pressed()
if key[pygame.K_a]: paddleOne_rect.move_ip(0, -10)
if key[pygame.K_z]: paddleOne_rect.move_ip(0, 10)
if key[pygame.K_k]: paddleTwo_rect.move_ip(0, -10)
if key[pygame.K_m]: paddleTwo_rect.move_ip(0, 10)
#makes sure paddles stay on screen
paddleOne_rect.clamp_ip(screen_rect)
paddleTwo_rect.clamp_ip(screen_rect)
ball_rect.move_ip(x_speed, y_speed)
if ball_rect.y + ball_rect.height> screen_rect.height or ball_rect.y < 0:
y_speed = y_speed * -1
bounce.play()
if ball_rect.collidelist([paddleOne_rect, paddleTwo_rect]) > -1:
x_speed = -x_speed
R = random.randrange(1,255)
B = random.randrange(1,255)
G = random.randrange(1,255)
bounce.play()
if ball_rect.x >= 700:
x_speed * -1
playerOne_score += 1
pygame.display.flip
if ball_rect.x <= 0:
x_speed * -1
playerTwo_score += 1
#PROCESS ^ =======================================================
#DRAWING GOES BELOW HERE v ------------------------------------
draw_background(screen, background, 0,0)
screen.blit(scoreOne, (0,0))
screen.blit(scoreTwo, (500,0))
pygame.draw.ellipse(screen, WHITE,ball_rect )
pygame.draw.rect(screen,RANDOM_COLOR, paddleOne_rect)
pygame.draw.rect(screen,RANDOM_COLOR, paddleTwo_rect)
pygame.draw.line(screen, WHITE, (350,0),(350,700), 1)
#DRAWING GOES ABOVE HERE ^ ------------------------------------
pygame.display.flip()
clock.tick(60)
pygame.quit()
What I am currently having problems with at the moment is when the ball goes off the screen, I want it to go back to the middle again as someone has scored a point. But I'm a bit stuck on what to do.
If you guys can help me out, that would be amazing!!
There is a lot of code here, so this does not follow specific variables you used, but I hope this helps.
1) Find the width of you screen
2) Take the x and y coordinates that you use to know where to draw the ball
3) Make an if statement that essentially says
(pseudocode)
if x > 1000
score1 += 1
x = 500
if x < 0
score2 += 1
x = 500
``
I hope this can set you on the right track, and I suggest checking out the pygame docs.
Cheers!

I try to insert a character in pygame at a certain point. What am I doing wrong?

I tried to show a character in my window at a certain point. What am I doing wrong?
This is for my first game and I am new to pygame. I tried the code, that google gave me.
screenwidth = 800
screenheight = 600
win = pygame.display.set_mode((screenwidth, screenheight))
bg = pygame.image.load("bg.png")
char = pygame.image.load("char.png")
x = 400
y = 300
vel = 5
isJumping = False
running = True
clock = pygame.time.Clock()
def redrawBackground():
win.blit(bg, (0, 0))
pygame.display.update()
def player(x, y):
win.blit(char, (x, y))
while running:
clock.tick(60)
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_one_x -= vel
if keys[pygame.K_RIGHT]:
player_one_x += vel
if not(isJumping):
if keys[pygame.K_SPACE]:
for i in range(10):
player_one_y -= vel
isJumping = True
player(x, y)
redrawBackground()
I expect the result of this code to show the file named "char.png", but it didn't show up.
Your code is drawing the player, then over-writing it with the background:
while running:
#...
player(x, y)
redrawBackground()
This might be better as:
def redrawBackground():
win.blit(bg, (0, 0))
# removed the call to pygame.display.update()
and
while running:
#...
redrawBackground()
player(x, y)
pygame.display.update()

My player sprite keeps on disappearing if it lands on the platform

I am trying to build my first Platformer Game. So far, I have make moving left and right but unfortunately, I have encountered a error that I have not been able to fix when I implemented collisions and gravity. My player keeps on dissolving like spider man if it lands on the platform. The character is still existent, and lands on the platform, but unfortunately, he becomes invisible. There is no error message, and I suspect is has to do with the collision check.
hits = pygame.sprite.spritecollide(object, allPlatforms, False)
if hits:
object.rect.y = hits[0].rect.top + 1
object.vy = 0
print(object.rect.midbottom)
It prints out the Players location in the code, and the player is still existent and movable, but it just doesn't show. Is there something that I did that makes the character vanish?
import pygame
import random
WIDTH = 500
HEIGHT = 400
FPS = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
playerImage = "blockBandit/BlockBandit.png"
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image = pygame.image.load(playerImage).convert()
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.vx = 0
self.vy = 0
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, w, h):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((w, h))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Block Bandit")
clock = pygame.time.Clock()
allPlatforms = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
all_sprites.add(p1)
allPlatforms.add(p1)
def moveCharacter(object):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
object.vx += -2
if keys[pygame.K_RIGHT]:
object.vx += 2
object.vx = object.vx * 0.9
if (abs(object.vx) < 1):
object.vx = 0
if (abs(object.vx) > 10):
if(object.vx < 0):
object.vx = -10
else:
object.vx = 10
object.vy = object.vy + 1
object.rect.x += object.vx
object.rect.y += object.vy
hits = pygame.sprite.spritecollide(object, allPlatforms, False)
if hits:
object.rect.y = hits[0].rect.top + 1
object.vy = 0
print(object.rect.midbottom)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
moveCharacter(player)
#Update State
all_sprites.update()
#Render
screen.fill(BLACK)
all_sprites.draw(screen)
#screen.blit(player.icon, (20, 40))
pygame.display.flip()
pygame.quit()
Am I doing something wrong? Thanks!
The y attribute of the rect is the same as the top coordinate, so you're setting the top of the player sprite to the top of the platform sprite here object.rect.y = hits[0].rect.top + 1. And if the platform comes later in the sprite group, it will be blitted after the player and the player won't be visible.
Just change that line to object.rect.bottom = hits[0].rect.top + 1.

Collision between two circle in pygame [duplicate]

This question already has answers here:
Pygame how to let balls collide
(2 answers)
pygame Get the balls to bounce off each other
(1 answer)
Closed 2 years ago.
I am trying to create my first game, where I have successfully added a bouncing ball and another ball around the mouse-pointer. The collision between these two circles works fine except sometimes the circles intersects each other hugely and the collision does not occur in a single frame, and some strange behaviour thereafter. How do I fix this?
import pygame
from pygame.locals import *
import numpy as np
# Define the colors we will use in RGB format
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
width = 800
height = 600
radius = 40
mRadius = 60
base = 20
g = 0.2
pygame.init()
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('Testing')
clock = pygame.time.Clock()
class Circle:
def __init__(self):
self.pos = np.array([100,100])
self.velocity = np.array([2,0])
def upgrade(self,mouse):
self.pos = np.add(self.velocity,self.pos)
if ((self.pos[0] > width - radius - 1) or (self.pos[0] < 0 + radius)):
self.velocity[0] *= -1
if self.pos[1] > height - radius - base - 1:
self.velocity[1] *= -1
#print(str(self.velocity[1])+"\t"+str(self.pos[1]))
else:
self.velocity = np.add(self.velocity,[0,g])
if ((radius + mRadius)**2 >= ((self.pos[0] - mouse[0])**2 +(self.pos[1] - mouse[1])**2)):
self.pos = np.subtract(self.pos,self.velocity)
lx = self.pos[0] - mouse[0]
ly = self.pos[1] - mouse[1]
A = (lx**2 - ly**2)/(lx**2 + ly**2)
B = 2*lx*ly/(lx**2 + ly**2)
M = np.array([[A,B],[B,(-1)*A]])
self.velocity = list((-1)*M.dot(self.velocity))
print(str(lx)+"\t"+str(ly)+"\t"+str(mouse))
pygame.draw.circle(screen, GREEN, [int(self.pos[0]),int(self.pos[1])], radius)
return True
def main():
mouse = [100,100]
running = True
ball = Circle()
while running:
(mouse[0],mouse[1]) = pygame.mouse.get_pos()
screen.fill(WHITE)
pygame.draw.circle(screen, BLUE, mouse, mRadius)
running = ball.upgrade(mouse)
pygame.draw.rect(screen,RED,(0,height-base,width,base))
pygame.display.update()
clock.tick(120)
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
pygame.display.quit()
if __name__ == '__main__':
main()
Here is the github link

for event in pygame.event.get(): pygame.error: video system not initialized

I'm a student and this game is one of my task. when the player win or loose, pressanykeytostart() doesn't work. however in the beginning it works. Also I get this error massage (Pygame Error: Video System not Initialized) every time I play.
How can I add a Pause function in my game?
###########################Set up the screen, background image and sound
import random,pygame,sys,math
pygame.init()
from pygame.locals import*
WIDTH = 600
HEIGHT = 600
TEXTCOLOR = (255, 255, 255)
Yellow = (255, 255, 0)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Pacman')
pygame.mouse.set_visible(False)
background_image = pygame.image.load('leaves.jpg')
backgroundmusic = pygame.mixer.Sound('lost.wav')
backgroundmusic.play()
##################################################Set up the score font
font_name = pygame.font.match_font('arial')
def draw_text(surf, text, size, x, y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, Yellow)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
###################################################SET UP the images and Sound
pacman = pygame.sprite.Sprite()
ghost = pygame.sprite.Sprite()
eatingsound = pygame.mixer.Sound('pacman_eatfruit.wav')
pacman.image = pygame.image.load('pacman1.png')
pacman.rect = pacman.image.get_rect()
pacman_group = pygame.sprite.GroupSingle(pacman)
over_image = pygame.image.load('game.jpg')
deathsound = pygame.mixer.Sound('pacman_death.wav')
##################################################press a key to start
def terminate():
pygame.quit()
sys.exit()
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
return
def drawText(text, font, surface, x, y):
screen.blit(background_image, (0,0))
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
screen.blit(textobj, textrect)
font = pygame.font.SysFont(None, 48)
drawText('Pacman Game', font, screen, (WIDTH / 3), (HEIGHT / 3))
drawText('Press a key to start.', font, screen, (WIDTH / 3) - 30, (HEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
######################################Adding Ghost in different position with different speed
class Ghost(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('ghost.png')
self.rect = self.image.get_rect()
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
def update(self):
self.rect.y += self.speedy
if self.rect.top > HEIGHT + 10:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
all_sprites = pygame.sprite.Group()
ghosts = pygame.sprite.Group()
for i in range(2):
g = Ghost()
all_sprites.add(g)
ghosts.add(g)
#################################################### Adding the Coins
TILE_SIZE = pacman.rect.width
NUM_TILES_WIDTH = WIDTH / TILE_SIZE
NUM_TILES_HEIGHT = HEIGHT / TILE_SIZE
candies = pygame.sprite.OrderedUpdates()
for i in range(50):
candy = pygame.sprite.Sprite()
candy.image = pygame.image.load('coin.png')
candy.rect = candy.image.get_rect()
candy.rect.left = random.uniform(0, NUM_TILES_WIDTH - 1) * TILE_SIZE
candy.rect.top = random.uniform(0, NUM_TILES_HEIGHT - 1) * TILE_SIZE
candies.add(candy)
###########################################Game Loop
score = 0
pause = False
gameOver = False
running = True
win = False
while running :
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.QUIT:
running = False
###########################################Move the pacman
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
pacman.rect.top -= TILE_SIZE
elif event.key == pygame.K_DOWN:
pacman.rect.top += TILE_SIZE
elif event.key == pygame.K_RIGHT:
pacman.rect.right += TILE_SIZE
elif event.key == pygame.K_LEFT:
pacman.rect.right -= TILE_SIZE
##############################################Keep the pacman on the screen
if pacman.rect.left < 0:
pacman.rect.left = 0
elif pacman.rect.right > 600:
pacman.rect.right = 600
elif pacman.rect.top <= 0:
pacman.rect.top = 0
elif pacman.rect.bottom >= 600:
pacman.rect.bottom = 600
###############################################Able to use mouse
if event.type == MOUSEMOTION:
pacman.rect.move_ip(event.pos[0] - pacman.rect.centerx, event.pos[1] - pacman.rect.centery)
###############################################Adding coins randomly
if event.type == pygame.USEREVENT:
if win == False:
candy = pygame.sprite.Sprite()
candy.image = pygame.image.load('coin.png')
candy.rect = candy.image.get_rect()
candy.rect.left = random.uniform(0, NUM_TILES_WIDTH - 1) * TILE_SIZE
candy.rect.top = random.uniform(0, NUM_TILES_HEIGHT - 1) * TILE_SIZE
candies.add(candy)
################################################Collecting the coins and set the score
collides = pygame.sprite.groupcollide(pacman_group, candies, False, True)
if len(collides) > 0:
eatingsound.play()
score += 1
if len(candies) == 0:
win = True
screen.blit(background_image, (0,0))
#################################################Wining the game
if win:
drawText('You Win!', font, screen, (WIDTH / 3) - 30, (HEIGHT / 3) + 50)
pygame.display.update()
winingsound = pygame.mixer.Sound('applause3.wav')
winingsound.play()
backgroundmusic.stop()
waitForPlayerToPressKey()
##################################################################### Game Over screen
candies.draw(screen)
pacman_group.draw(screen)
all_sprites.draw(screen)
draw_text(screen, str(score), 18, WIDTH / 2, 10)
pygame.display.flip()
all_sprites.update()
hits = pygame.sprite.spritecollide(pacman, ghosts, False)
if hits:
gameOver = True
if gameOver == True:
drawText('Game Over!', font, screen, (WIDTH / 3) - 30, (HEIGHT / 3) + 50)
drawText('Press a key to start again.', font, screen, (WIDTH / 3) - 30, (HEIGHT / 3) + 50)
pygame.display.update()
deathsound.play()
backgroundmusic.stop()
waitForPlayerToPressKey()
#############################################Drwing everything on the screen
pygame.quit()
Try with pygame.time.wait(time)
Or Pygame.event.wait (). The first wait the amount of time in brakets, and the second wait until a new event received.

Resources