I am trying to get my player to jump when I press the up key. I have created a jump method in the Player class that gets called in the game loop. When user presses the UP key, value of player.isJump should go from its default value of False, to True. and the code inside player.jump() should run until the jump is completed and player.isJump is set back to False. But when I press the up key, nothing happens.
import pygame
pygame.init()
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Stick Quest')
class Player():
def __init__(self):
self.playerImg = pygame.image.load('player.png')
self.playerX = 400
self.playerY = 300
self.playerX_change = 0
self.isJump = False
self.jumpCount = 10
def displayPlayer(self):
screen.blit(self.playerImg, (self.playerX,self.playerY))
def updateLocation(self):
self.playerX += self.playerX_change
def jump(self):
if self.isJump:
if self.jumpCount >= -10:
neg = 1
if player.jumpCount < 0:
neg = -1
self.playerY -= (self.jumpCount ** 2) * 0.5 * neg
self.jumpCount = -1
else:
self.isJump = False
self.jumpCount = 10
player = Player()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.playerX_change = -1.0
if event.key == pygame.K_RIGHT:
player.playerX_change = 1.0
if event.key == pygame.K_UP:
player.isJump == True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player.playerX_change = 0
player.updateLocation()
player.jump()
player.displayPlayer()
pygame.display.update()
screen.fill((255,255,255))
pygame.quit()
Your problem is in this line:
player.isJump == True
instead of assign the player.isJump to True, you just checked if it is true or not. nothing changed.
To fix it use the assignment sign:
player.isJump = True
Related
I am trying to move Left and Right and whenever I let go of the right arrow it keeps moving right and same with the left side of things. I have been messing around with this and cant figure it out.
import pygame
# Intialize the pygame
pygame.init()
# create the screen
screen = pygame.display.set_mode((800,600))
# Player
PlayerHealth = 100
Playerinven = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
playerImg = pygame.image.load('001-alien.png')
# Player Movment
playerX = 370
playerY = 480
XLlabel = False
XRlabel = False
playerX_change = 0
def player(x, y):
screen.blit(playerImg, (x , y))
# Game Loop
running = True
while running:
# RGB - Red,Green,Blue
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
XLlabel = True
elif event.key == pygame.K_RIGHT:
XRlabel = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
XLlabel = False
if event.key == pygame.K_RIGHT:
XRlabel = False
if XLlabel == True and XRlabel == True:
playerX_change = 0
elif XLlabel == True:
playerX_change = -0.3
elif XRlabel == True:
playerX_change = 0.3
elif XLlabel == False and XRlabel == False:
player_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
if playerX >= 736:
playerX = 736
player(playerX, playerY)
pygame.display.update()
This is my first time trying to make a game so it is kinda stressing me out XD
Error:
You have written player_change = 0 where there must be playerX_change = 0.
Code:
# Player
PlayerHealth = 100
Playerinven = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
playerImg = pygame.image.load('001-alien.png')
# Player Movment
playerX = 370
playerY = 480
XLlabel = False
XRlabel = False
playerX_change = 0
def player(x, y):
screen.blit(playerImg, (x , y))
# Game Loop
running = True
while running:
# RGB - Red,Green,Blue
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
XLlabel = True
elif event.key == pygame.K_RIGHT:
XRlabel = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
XLlabel = False
if event.key == pygame.K_RIGHT:
XRlabel = False
if XLlabel == True and XRlabel == True:
playerX_change = 0
elif XLlabel == True:
playerX_change = -0.3
elif XRlabel == True:
playerX_change = 0.3
elif XLlabel == False and XRlabel == False:
playerX_change = 0 #just a small error you have written player_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
if playerX >= 736:
playerX = 736
player(playerX, playerY)
pygame.display.update()
Not sure if this is the cause of your problem but your handling of the keys seems a little "tortured".
For a start, you should generally never use if XLlabel == True, preferring instead the more succinct if XLlabel.
The key handling can then be made something like:
# Initial left/right key state flags.
left_down, right_down = False, False
for event in pygame.event.get():
# Process events, keys up/down sets flags.
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
left_down = True
elif event.key == pygame.K_RIGHT:
right_down = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
left_down = False
elif event.key == pygame.K_RIGHT:
right_down = False
# Event processing done, evaluate key states to get velocity.
velocity = 0
if left_down:
velocity -= 0.3
if right_down:
velocity += 0.3
# Adjust player position by velocity.
...
i tried but i cant find the error pls help how do i execute it?
im new so, it would be a great help if u tell me a clean way of adding attack animation also
import pygame
import os
pygame.init
t = pygame.display.set_mode((1280,720))
pygame.display.set_caption("my first game moce")
clock = pygame.time.Clock()
FPS = 60
RED = (255,0,0)
#game variables
GRAVITY = 0.75
moving_left = False
moving_right = False
#load images
R =[pygame.image.load('img/hero/attack/0.png'),pygame.image.load('img/hero/attack/1.png'),pygame.image.load('img/hero/attack/2.png'),pygame.image.load('img/hero/attack/3.png'),pygame.image.load('img/hero/attack/4.png'),pygame.image.load('img/hero/attack/5.png'),pygame.image.load('img/hero/attack/6.png'),pygame.image.load('img/hero/attack/7.png'),pygame.image.load('img/hero/attack/8.png'),pygame.image.load('img/hero/attack/9.png')]
L =[pygame.image.load('img/hero/attack L/0.png'),pygame.image.load('img/hero/attack L/1.png'),pygame.image.load('img/hero/attack L/2.png'),pygame.image.load('img/hero/attack L/3.png'),pygame.image.load('img/hero/attack L/4.png'),pygame.image.load('img/hero/attack L/5.png'),pygame.image.load('img/hero/attack L/6.png'),pygame.image.load('img/hero/attack L/7.png'),pygame.image.load('img/hero/attack L/8.png'),pygame.image.load('img/hero/attack L/9.png')]
class soldier(pygame.sprite.Sprite):
def __init__(self, char_type,x,y,scale,speed):
pygame.sprite.Sprite.__init__(self)
self.alive = True
self.char_type = char_type
self.speed = speed
self.direction = 1
self.vel_y = 0
self.jump = False
self.in_air = True
self.flip = False
self.attacking = False
self.attack_frame = 0
self.animation_list = []
self.frame_index = 0
self.action = 0
self.update_time = pygame.time.get_ticks()
animation_types = ['idle','jump','run']
for animation in animation_types:
temp_list = []
num_of_frames = len(os.listdir(f'img/{self.char_type}/{animation}'))
for i in range(num_of_frames):
img = (pygame.image.load(f'img/{self.char_type}/{animation}/{i}.png'))
img = pygame.transform.scale(img,(int(img.get_width()*scale),int(img.get_height()*scale)))
temp_list.append(img)
self.animation_list.append(temp_list)
self.img = self.animation_list[self.action][self.frame_index]
self.rect = self.img.get_rect()
self.rect.center = (x,y)
def move(self, moving_left, moving_right):
dx = 0
dy = 0
if moving_left:
dx = -self.speed
self.flip = True
self.direction = -1
if moving_right:
dx = self.speed
self.flip = False
self.direction = 1
if self.jump == True and self.in_air == False:
self.vel_y = -11
self.jump = False
self.in_air = True
#appy gravity
self.vel_y += GRAVITY
if self.vel_y > 10:
self.vel_y
dy += self.vel_y
#check collison with floor
if self.rect.bottom +dy > 300:
dy = 300 - self.rect.bottom
self.in_air = False
self.rect.x += dx
self.rect.y += dy
def update_animation(self):
ANIMATION_COOLDOWN = 100
if pygame.time.get_ticks() - self.update_time > ANIMATION_COOLDOWN:
self.img = self.animation_list[self.action][self.frame_index]
self.update_time = pygame.time.get_ticks()
self.frame_index += 1
#if out of animation
if self.frame_index >= len(self.animation_list[self.action]):
self.frame_index = 0
def update_action(self, new_action):
if new_action != self.action:
self.action = new_action
self.frame_index = 0
self.update_time = pygame.time.get_ticks()
def attack(self):
if self.attack_frame > 10:
self.attack_frame = 0
self.attacking = False
if self.direction == 'RIGHT':
self.image = R[self.attack_frame]
elif self.direction == 'LEFT':
self.correction()
self.image = L[self.attack_frame]
self.attack_frame += 1
def correction(self):
# Function is used to correct an error
# with character position on left attack frames
if self.attack_frame == 1:
self.pos.x -= 20
if self.attack_frame == 10:
self.pos.x += 20
def draw(self):
t.blit(pygame.transform.flip(self.img,self.flip,False),self.rect)
player = soldier("hero",200,200,2,5)
width = 30
height = 60
x = 300
y = 0
jump = False
jump_count = 10
run = True
background_image = pygame.image.load("C:/Users/Owner/Desktop/q1.png").convert()
while run:
t.blit(background_image,(0,0))
clock.tick(FPS)
player.update_animation()
player.draw()
player.move(moving_left, moving_right)
if player.alive:
if player.attacking == True:
player.attack()
if player.in_air:
player.update_action(1)
elif moving_left or moving_right:
player.update_action(2)
else:
player.update_action(0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
moving_left = True
if event.key == pygame.K_d:
moving_right = True
moving_left = False
if event.key == pygame.K_SPACE and player.alive:
player.jump = True
if event.key == pygame.K_ESCAPE:
run = False
if event.key == pygame.K_LSHIFT:
player.speed = 7
if event.key == pygame.K_e:
if player.attacking == False:
player.attack()
player.attacking = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
moving_left = False
if event.key == pygame.K_d:
moving_right = False
if event.key == pygame.K_SPACE:
player.jump = False
if event.key == pygame.K_LSHIFT:
player.speed = 5
if event.key == pygame.K_e:
if player.attacking == True:
player.attacking = False
pygame.display.update()
pygame.quit()
You have different options depending on the effect you want to achieve.
In your current implementation, an attack is stopped when the e button is released. I recommend to remove that code:
while run:
# [...]
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYUP:
# [...]
# DELETE
# if event.key == pygame.K_e:
# if player.attacking == True:
# player.attacking = False
# self.attack_frame = 0
If you want to stop the attack when the e button is released, you must also reset self.attack_frame
while run:
# [...]
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYUP:
# [...]
if event.key == pygame.K_e:
if player.attacking == True:
player.attacking = False
self.attack_frame = 0
If you want to allow continuous attacks and interrupt an attack, you need to change the implementation in the KEYDOWN event and reset self.attack_frame when e is pressed:
while run:
# [...]
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
# [...]
if event.key == pygame.K_e:
player.attacking = True
self.attack_frame = 0
if event.type == pygame.KEYUP:
# [...]
# DELETE
# if event.key == pygame.K_e:
# if player.attacking == True:
# player.attacking = False
# self.attack_frame = 0
I'm quite new to python and have recently started game dev with pygame. I wanted to know why my game just freezes and the exitcode -805306369 (0xCFFFFFFF) appears. Do i have errors in my programm? It looks like this:
import pygame
import random
import math
pygame.init()
window = pygame.display.set_mode((1000, 600))
caption = pygame.display.set_caption(
'Test your reaction speed. Shoot the target game!') # sets a caption for the window
run = True
PlayerImg = pygame.image.load('F:\PythonPortable\oscn.png')
PlayerX = 370
PlayerY = 420
PlayerX_change = 0
PlayerY_change = 0
def player():
window.blit(PlayerImg, (PlayerX, PlayerY))
aim_sight = pygame.image.load('F:\PythonPortable\ktarget.png')
aim_sightX = 460
aim_sightY = 300
aim_sight_changeX = 0
aim_sight_changeY = 0
def aim_sight_function(x, y):
window.blit(aim_sight, (x, y))
targetedImg = pygame.image.load('F:\PythonPortable\ktargetedperson.png')
targetedX = random.randint(0, 872)
targetedY = random.randint(0, 200)
def random_target():
window.blit(targetedImg, (targetedX, targetedY))
def iscollision(targetedX, targetedY, aim_sightX, aim_sightY):
distance = math.sqrt((math.pow(targetedX - aim_sightX, 2)) + (math.pow(targetedY - aim_sightY, 2)))
if distance < 70:
return True
else:
return False
while run:
window.fill((255, 255, 255))
random_target()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
aim_sight_changeX = -2
PlayerX_change = -2
elif event.key == pygame.K_RIGHT:
aim_sight_changeX = 2
PlayerX_change = 2
elif event.key == pygame.K_UP:
aim_sight_changeY = -2
PlayerY_change = -2
elif event.key == pygame.K_DOWN:
aim_sight_changeY = 2
PlayerY_change = 2
score = 0
while score < 10:
collision = iscollision(targetedX, targetedY, aim_sightX, aim_sightY)
if collision and event.key == pygame.K_SPACE:
print("HIT") # Just for me to acknowledge that collision is true and space bar was pressed in the right spot
score = score + 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
aim_sight_changeX = 0
PlayerX_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
aim_sight_changeY = 0
PlayerY_change = 0
aim_sightX += aim_sight_changeX
if aim_sightX <= 46.5:
aim_sight_changeX = 0
elif aim_sightX >= 936:
aim_sight_changeX = 0
aim_sightY += aim_sight_changeY
if aim_sightY <= 0:
aim_sight_changeY = 0
elif aim_sightY >= 400:
aim_sight_changeY = 0
PlayerX += PlayerX_change
if PlayerX <= -50:
PlayerX_change = 0
elif PlayerX >= 850:
PlayerX_change = 0
player()
aim_sight_function(aim_sightX, aim_sightY)
pygame.display.update()
I think there is a mistake in this area as my program runs well without it.
while score < 10:
collision = iscollision(targetedX, targetedY, aim_sightX, aim_sightY)
if collision and event.key == pygame.K_SPACE:
print("HIT") # Just for me to acknowledge that collision is true and space bar was pressed in the right spot
score = score + 1
I've checked other questions but they are mostly for java and other languages.
You have an application loop. You do not need an additional loop to control the game. The loop that increments the score is an infinite loop. Change the loop to a selection (change while to if).
Furthermore, the score has to be initialized before the application loop:
score = 0
while run:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
aim_sight_changeX = -2
PlayerX_change = -2
elif event.key == pygame.K_RIGHT:
# [...]
elif event.key == pygame.K_SPACE:
if score < 10:
collision = iscollision(targetedX, targetedY, aim_sightX, aim_sightY)
if collision:
print("HIT")
score = score + 1
I wrote this simple code that draws and then lets you move a rectangle, but even after I change the coordinates of the rectangle by calling the move_player_x_ function, it doesn't move at all. I don't understand why. I came here looking for clarification and a detailed solution to my problem.
Here's the code:
import pygame
white = (255, 255, 255)
black = (0, 0, 0)
class Game():
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
def __init__(self):
pass
def fill_screen(self, color):
self.color = color
self.screen.fill(self.color)
def update_method(self):
pygame.display.update()
game = Game()
class Player(pygame.sprite.Sprite):
lead_x = game.width/2
lead_y = game.height/2
lead_x_change = 0
lead_y_change = 0
velocity = 0.2
block_size = 10
def __init__(self):
pygame.sprite.Sprite.__init__(self)
def move_player_x_left(self):
self.lead_x_change += -self.velocity
def move_player_x_right(self):
self.lead_x_change += self.velocity
def move_player_y_up(self):
self.lead_y_change += -self.velocity
def move_player_y_down(self):
self.lead_y_change += self.velocity
def draw_player(self):
pygame.draw.rect(game.screen, black, [self.lead_x, self.lead_y, self.block_size, self.block_size])
def key_up_x_stop(self):
self.lead_x = 0
def key_up_y_stop(self):
self.lead_y = 0
def constant_x_movement(self):
self.lead_x += self.lead_x_change
def constant_y_movement(self):
self.lead_y += self.lead_y_change
player = Player()
exitGame = False
while not exitGame:
game.fill_screen(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.move_player_y_up()
if event.key == pygame.K_s:
player.move_player_y_down()
if event.key == pygame.K_a:
player.move_player_x_left()
if event.key == pygame.K_d:
player.move_player_x_right()
if event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
player.key_up_y_stop()
if event.key == pygame.K_a or event.key == pygame.K_d:
player.key_up_x_stop()
player.constant_x_movement()
player.constant_y_movement()
player.draw_player()
game.update_method()
pygame.quit()
quit()
The code in the event loop is not indented correctly. Here's a corrected version:
while not exitGame:
game.fill_screen(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.move_player_y_up()
if event.key == pygame.K_s:
player.move_player_y_down()
if event.key == pygame.K_a:
player.move_player_x_left()
if event.key == pygame.K_d:
player.move_player_x_right()
if event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
player.key_up_y_stop()
if event.key == pygame.K_a or event.key == pygame.K_d:
player.key_up_x_stop()
Also, in the ...stop methods, you have to set lead_x_change and lead_y_change to 0 not lead_x and lead_y.
def key_up_x_stop(self):
self.lead_x_change = 0
def key_up_y_stop(self):
self.lead_y_change = 0
I edited your code, i added a function in the player object move_player with local booleans bUp,bDown,bLeft,bRight. If python had enumerations, it would be so much better. Any how, on event KEY DOWN and KEY UP, they toggle these booleans in the player. after the input is calculated and these booleans are toggled/set, it called move_player() in player object that checks its booleans and sets a while loop while one is true and adds velocity in the respected location and redraws player.
Here the source i got for you...
import pygame
white = (255, 255, 255)
black = (0, 0, 0)
class Game():
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
def __init__(self):
pass
def fill_screen(self, color):
self.color = color
self.screen.fill(self.color)
def update_method(self):
pygame.display.update()
class Player(pygame.sprite.Sprite):
lead_x = 800/2
lead_y = 600/2
velocity = 0.002
block_size = 10
bUp = false
bDown = false
bLeft = false
bRight = false
def __init__(self):
pygame.sprite.Sprite.__init__(self)
def draw_player(self):
pygame.draw.rect(game.screen, black, [self.lead_x, self.lead_y, self.block_size, self.block_size])
def move_player(self):
while bLeft:
self.lead_x += -self.velocity
self.draw_player()
while bRight:
self.lead_x += self.velocity
self.draw_player()
while bUp:
self.lead_y += -self.velocity
self.draw_player()
while bDown:
self.lead_y += self.velocity
self.draw_player()
game = Game()
player = Player()
exitGame = False
while not exitGame:
game.fill_screen(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.bUp = true
if event.key == pygame.K_s:
player.bDown = true
if event.key == pygame.K_d:
player.bRight = true
if event.key == pygame.K_a:
player.bLeft = true
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
player.bUp = false
if event.key == pygame.K_s:
player.bDown = false
if event.key == pygame.K_d:
player.bRight = false
if event.key == pygame.K_a:
player.bLeft = false
player.move_player()
game.update_method()
pygame.quit()
quit()
Im working on a simple game( a semi copy of the 'Dodger' game), and the game runs, yet displays nothing. I have a while loop running, so why is nothing showing up? Is it a problem with spacing, the images themselves, or am i just overlooking something?
import pygame,sys,random, os
from pygame.locals import *
pygame.init()
#This One Works!!!!!!!
WINDOWHEIGHT = 1136
WINDOWWIDTH = 640
FPS = 40
TEXTCOLOR = (255,255,255)
BACKGROUNDCOLOR = (0,0,0)
PLAYERMOVEMENT = 6
HARVEYMOVEMENT = 5
TJMOVEMENT = 7
LASERMOVEMENT = 10
ADDNEWBADDIERATE = 8
COLOR = 0
TJSIZE = 65
HARVEYSIZE = 65
#Check the sizes for these
def terminate():
if pygame.event() == QUIT:
pygame.quit()
def startGame():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
return
def playerHasHitBaddies(playerRect,TjVirus,HarVirus):
for b in TjVirus and HarVirus:
if playerRect.colliderect(b['rect']):
return True
return False
def drawText(text,font,surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
mainClock = pygame.time.Clock()
WindowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.mouse.set_visible(False)
pygame.display.set_caption('Virus')
#Player Images
# Check the name of the .png file
TjImage = pygame.image.load('Virus_TJ_edited-1.png')
TjRect = TjImage.get_rect()
#chanhe this part from the baddies variable in the 'baddies' area
playerImage = pygame.image.load('Tank_RED.png')
playerRect = playerImage.get_rect()
LaserImage = pygame.image.load('laser.png')
LaserRect = LaserImage.get_rect()
pygame.display.update()
startGame()
while True:
TjVirus = []#the red one / make a new one for the blue one
HarVirus = []#The BLue one / Need to create a new dictionary for this one
playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 50)
moveLeft = moveRight = moveUp = moveDown = laser = False
baddieAddCounter = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == ord('s'):
moveUp = False
moveDown = True
if event.key == K_SPACE:
lasers = True
if event.type == KEYUP:
if evnet.type == K_ESCAPE:
terminate()
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = False
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = False
if event.key == K_UP or event.key == ord('w'):
moveUp = False
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
if event.key == K_SPACE:
LaserImage.add(LaserRect)
if event.key == ord('j'):
COLOR = 2
if event.key == ord('k'):
if COLOR == 2:
COLOR = 1
playerImage = pygame.image.load('Tank_RED.png')
if COLOR == 1:
COLOR = 2
playerImage = pygame.image.load('Tank_BLUE.png')
if event.type == MOUSEMOTION:
playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)
if baddieAddCounter == ADDNEWBADDIERATE:
baddieAddCounter = 0
#Dict for TJ(RED) VIRUS
baddieSize = (TJSIZE)
NewTjVirus = {'rect':pygame.Rect(random.rantint(0,WINDOWWIDTH - TJSIZE),0 - TJSIZE,TJSIZE,TJSIZE),
'speed':(TJMOVEMENT),
'surface':pygame.transform.scale(TJImage,(TJSIZE,TJSIZE)),
}
TjVirus.append(NewTjVirus)
#Dict for Harvey(BLUE) virus
baddieSize = (HARVEYSIZE)
NewHarveyVirus = {'rect':pygame.Rect(random.randint(0,WINDOWWIDTH - HARVEYSIZE),0 - HARVEYSIZE,HARVEYSIZE,HARVEYSIZE),
'speed':(HARVEYMOVEMENT),
'surface':pygame.transform.scale(HARVEYSIZE,(HARVEYSIZE,HARVEYSIZE))
}
HarVirus.append(NewHarveyVirus)
#Player Movement
if moveLeft and playerRect.left >0:
playerRect.move_ip(-1*PLAYERMOVEMENT,0)
if moveRight and playerRect.right < WINDOWWIDTH:
playerRect.move_ip(PLAYERMOVEMENT,0)
if moveUp and playerRect.top >0:
playerRect.move_ip(0,-1*PLAYERMOVEMENT)
if moveDown and playerRect.bottom < WINDOWHEIGHT:
playerRect.move_ip(0,PLAYERMOVEMENT)
pygame,mouse.set_pos(playerRect.centerx,playerRect.centery)
#Need to change for each individual virus
for b in HarVirus and TjVirus:
b['rect'].move_ip(0,b['speed'])
for b in HarVirus and TjVirus:
if b['rect'].top > WINDOWHEIGHT:
baddies.remove(b)
windowSurface.fill(pygame.image.load('Background_Proto copy.png'))
for b in HarVirus and TjVirus:
windowSurface.blit(b['surface'],b['rect'])
pygame.display.update()
if playerHasHitBaddies(playerRect,HarVirus,TjVirus):
break
for b in TjVirus and HarVirus[:]:
if b['rect'].top < WINDOWHEIGHT:
HarVirus.remove(b)
TjVirus.remove(b)
mainClock.tick(FPS)
Usally I use pygame.display.flip() not pygame.display.update()
You call your startGame() in your script, which looks like this:
def startGame():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
return
The game is stuck in that while loop until you press a key.
That's your current problem, but there are other errors as well, like
def terminate():
if pygame.event() == QUIT:
pygame.quit()
pygame.event() is not callable. Maybe you wanted to pass an event as argument here?
And also
pygame,mouse.set_pos(playerRect.centerx,playerRect.centery)
, instead of .