So I tried to make it so that every single time I jump, my player score will add by one but so far its adding by to much, every time I jump or hold the SPACE key the score will be adding to much. I tried to write this code differently but that did not work for me. https://gyazo.com/7854a425ac4f66783980492dae774f84
My code to add 1 to score every time player jumps[not working]
if keys[pygame.K_SPACE]:
score += 1
text = font.render(" = "+str(score),True,(255,255,255))
textRect.center = ((150,40))
My full code
import pygame
import random
pygame.init()
window = pygame.display.set_mode((700,500))
pygame.display.set_caption(("Noobs First Game"))
# Player class
class Player:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 6
self.fall = 0
self.isJump = False
self.JumpCount = 10
self.rect = pygame.Rect(x,y,width,height)
def get_rect(self):
self.rect.topleft = (self.x,self.y)
return self.rect
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# Platform class
class Platform:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = pygame.Rect(x,y,width,height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# displaying Color
white = (255,255,255)
# Drawing player
playerman = Player(255,255,40,40,white)
# Drawing Platform
platform1 = Platform(200,300,700,30,white)
# Putting Platform in a list
platforms = [platform1]
# redrawing window
def redrawwindow():
window.fill((0,0,0))
# bliting a counter the game
window.blit(text,textRect)
# showing player on the screen
playerman.draw()
# Drawing Platform
for Platform in platforms:
Platform.draw()
# The conter and how its going look like
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render(" = "+str(score),True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((150,40))
fps = 30
clock = pygame.time.Clock()
x = 10
y = 10
x_change = 0
y_change = 0
old_x = x
old_y = y
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
x_change = -7
if event.key == pygame.K_a:
x_change = 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
x_change = 0
x += x_change
if x > 500 - playerman.width or x < 0:
x = old_x
# If keys get pressed
keys = pygame.key.get_pressed()
px,py = playerman.x,playerman.y
if keys[pygame.K_SPACE]:
score += 1
text = font.render(" = "+str(score),True,(255,255,255))
textRect.center = ((150,40))
# Player movment
if keys[pygame.K_a] and playerman.x > playerman.speed:
px -= playerman.speed
if keys[pygame.K_d] and playerman.x < 700 - playerman.width - playerman.speed:
px += playerman.speed
if keys[pygame.K_w] and playerman.y > playerman.speed:
py -= playerman.speed
if keys[pygame.K_s] and playerman.y < 500 - playerman.height - playerman.speed:
py += playerman.speed
platform_rect_list =[p.rect for p in platforms]
player_rect = playerman.get_rect()
playerman.rect.topleft = (px,py)
playerman.y = py
if player_rect.collidelist(platform_rect_list) < 0:
playerman.x = px
# About isJump
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
# this part lets you jump on platform only the top
collide = False
for Platform in platforms:
if playerman.get_rect().colliderect(Platform.rect):
collide = True
playerman.isJump = False
playerman.y = Platform.rect.top - playerman.height
if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
playerman.x = Platform.rect.left - playerman.width
if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
playerman.x = Platform.rect.right
# colliding with floor
if playerman.rect.bottom >= 500:
collide = True
playerman.isJump = False
playerman.Jumpcount = 10
playerman.y = 500 - playerman.height
# Jumping
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 0
# Jump Count
else:
if playerman.JumpCount >= 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount -= 1
else:
playerman.isJump = False
playerman.JumpCount = 10
redrawwindow()
pygame.display.update()
quit_game