02-28-2015
|
1
|
Graalaholic
Join Date: Apr 2012
Location: Canada
Posts: 253
|
Help Needed For My Script
It's basically a vertical scroller game but the given issue is the character only moves when the keys are tapped, not held.
What I want is to have the character move while the key is pressed just like Graal, without having to tap for example the up key continually.
Language : Python
PHP Code:
import pygame
import time
pygame.init()
window_width = 800
window_height = 600
black = (0,0,0)
white = (255,255,255)
gameDisplay = pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption("Trunul")
clock = pygame.time.Clock()
background = pygame.image.load("background.png")
charImage = pygame.image.load("character_up.png")
def character(charX,charY):
gameDisplay.blit(charImage, (charX,charY))
bg = -600
bgMin = -600
bgMax = 0
charX = (window_width * 0.45)
charY = (window_height * 0.45)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and bg < bgMax:
charImage = pygame.image.load("character_up.png")
bg += 10
if event.key == pygame.K_DOWN and bg > bgMin:
charImage = pygame.image.load("character_down.png")
bg -= 10
if event.key == pygame.K_RIGHT and charX < 750:
charImage = pygame.image.load("character_right.png")
charX += 10
if event.key == pygame.K_LEFT and charX > 0:
charImage = pygame.image.load("character_left.png")
charX -= 10
gameDisplay.fill(white)
gameDisplay.blit(background, (0,bg))
character(charX,charY)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
|
|
|