I have just started using pygame, and I'm stuck.
I'm not getting any syntax errors, but I'm sure there is some problem with the below code.
import pygame
import sys
pygame.init()
pygame.display.set_caption('Jumper Game')
display_width = 500
display_height = 500
the_game_is_on = True
ball_pos_x = 200
ball_pos_y = 500
ball_radius = 20
ball_color = [0,0,255]
speed = 1
is_jump = False
m = 1
v = 5
dis = pygame.display.set_mode((display_width,display_height)) #screen
while the_game_is_on:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and ball_pos_x > 20:
ball_pos_x-= speed
if keys[pygame.K_RIGHT] and ball_pos_x < (display_width - (ball_radius)):
ball_pos_x+= speed
if not (is_jump):
if keys[pygame.K_UP] and ball_pos_y > 20:
ball_pos_y-= speed
if keys[pygame.K_DOWN] and ball_pos_y < (display_height - (ball_radius)):
ball_pos_y+= speed
if keys[pygame.K_SPACE]:
is_jump = True
else:
f = (1/2)*m*(v**2)
ball_pos_y-=f
v-=1
if v < 0:
m = -1
if v >= ((v+1)*-1): #to check the initial position
is_jump = False
pygame.time.delay(10)
dis.fill((0,0,0))
pygame.draw.circle(dis,ball_color,(ball_pos_x,int(ball_pos_y)),ball_radius)
pygame.display.update()
pygame.display.quit()
For writing the code for jump, I referred to this website, the source code in this website works perfectly.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…