Reputation: 81
im new at pygame and im watching some tutorials in how to get started with it, I wrote this basic code that it only has movement and a jump "function" but it is really choppy, I do not think its my hardware (Macbook Pro 2018). Do someone has any idea of whats happening?
Here´s the code:
import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("First PyGame")
width = 40
height = 60
x = win_width/2
y = win_height/2
vel = 10
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("adios putito")
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < win_width - width - vel:
x+= vel
if not (isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN]and y < win_height - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
win.fill((0,0,0))
pygame.draw.rect(win,(255,255,255), (x,y,width,height))
pygame.display.update()
pygame.quit()
Upvotes: 2
Views: 364
Reputation: 9766
It's still a good idea to have a game clock that delays your program enough to keep a persistent framerate. You can use pygame's pygame.time.Clock
object and use its tick()
method to delay your game. The tick
method takes an integer which represents the FPS you want your game to cap at. If your game runs slower than the FPS value you put in, no delay will happen.
import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("First PyGame")
clock = pygame.time.Clock()
width = 40
height = 60
x = win_width/2
y = win_height/2
vel = 10
isJump = False
jumpCount = 10
run = True
while run:
# Delay your game to try and keep 60 FPS.
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("adios putito")
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < win_width - width - vel:
x+= vel
if not (isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN]and y < win_height - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
win.fill((0,0,0))
pygame.draw.rect(win,(255,255,255), (x,y,width,height))
pygame.display.update()
pygame.quit()
Upvotes: 0
Reputation: 81
The line
pygame.time.delay(100)
is delaying your script that milliseconds, remove that line and it should work just fine!
Result:
import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("First PyGame")
width = 40
height = 60
x = win_width/2
y = win_height/2
vel = 10
isJump = False
jumpCount = 10
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("adios putito")
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < win_width - width - vel:
x+= vel
if not (isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN]and y < win_height - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
win.fill((0,0,0))
pygame.draw.rect(win,(255,255,255), (x,y,width,height))
pygame.display.update()
pygame.quit()
Upvotes: 3