vendredi 8 mai 2015

Why isn't the square moving when a key is pressed?

Im trying to code a tron bike game and right now I am coding the part to get the square to move. I'm still a little bit of a newbie at coding, so my code might have some problems that I dont know yet. Here is my code:

import sys, pygame, math, random, itertools
from pygame.locals import *
from MyLibrary import *

def game_init():
    global screen,backbuffer,font,timer,player_group,player, \
           enemy_tank,bullets,crosshair,crosshair_group

    pygame.init()
    screen = pygame.display.set_mode((800,600))
    backbuffer = pygame.Surface((800,600))
    pygame.display.set_caption("Tron Bikes")
    font = pygame.font.Font(None,30)
    timer = pygame.time.Clock()

def audio_init():
    global shoot_sound, boom_sound

    pygame.mixer.init()

    boom_sound = pygame.mixer.Sound("bomb.wav")

def play_sound(sound):
    channel = pygame.mixer.find_channel(True)
    channel.set_volume(50)
    channel.play(sound)

game_init()
audio_init()
game_over = False
round = 0
player_score = 0
enemy_score = 0
x = 600
y = 450
x1 = 200
y1 = 150
pygame.draw.rect(backbuffer, (0,0,255),(x,y,20,20),0)
screen.blit(backbuffer,(0,0))
backbuffer.fill((100,100,100))

while True:
    timer.tick(30)
    ticks = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    keys = pygame.key.get_pressed()
    if keys[K_w]:
        y -= 1
    if keys[K_d]:
        x += 1
    if keys[K_s]:
        y += 1
    if keys[K_a]:
        x -= 1
    if keys[K_ESCAPE]:
        sys.exit()
    if x > 800:
        x = 800
    if x < 0:
        x = 0
    if y > 600:
        y = 600
    if y < 0:
        y = 0

    pygame.display.update()

EDIT: Forgot to say that I am making a tron bike game, so when w,a,s,d is pressed, im trying to make it go that way until another key is pressed, which is where I can't figure it out.

Aucun commentaire:

Enregistrer un commentaire