"if cond1 or cond2" statement not running second condition in Python - python

I am making a game in Python that looks like this so far:
import pygame, sys, time, random, threading
from threading import Timer
from pygame.locals import *
pygame.init()
WINDOWHEIGHT = 720
WINDOWWIDTH = 1280
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Hitman Grandma')
plorp = 'true'
white = (255,255,255)
red = (255,0,0)
black = (0,0,0)
green = (0,255,0)
blue = (0,0,255)
cyan = (0,255,255)
windowSurface.fill(white)
pygame.display.update()
mainClock = pygame.time.Clock()
hgleft = False
hgright = False
hgup = False
speed = 4
hgair = True
hgjumpallowed = False
level = 0
def stop() :
hgbox.move_ip(0,0)
return
hgbox = pygame.Rect(0 ,13 ,36 ,72)
hitmangrandma = pygame.image.load('hgrd1copy.jpg')
hg = pygame.transform.scale(hitmangrandma, (36,72))
landbox1 = pygame.Rect(0,400,200,50)
li = pygame.image.load('hgland1.png')
land1 = pygame.transform.scale(li,(200,50))
landbox2 = pygame.Rect(230,400,200,50)
land2 = pygame.transform.scale(li,(200,50))
land = landbox1,landbox2
while True:
windowSurface.fill(white)
windowSurface.blit(land1,landbox1)
windowSurface.blit(land2,landbox2)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == K_a:
hgright = False
hgleft = True
if event.key == K_RIGHT or event.key == K_d:
hgleft = False
hgright = True
if event.key == K_UP or event.key == K_w:
hgair = True
hgup = True
hgupkey = True
if event.type == KEYUP:
if event.key == K_ESCAPE or K_q and pygame.key.get_mods() & pygame.KMOD_CTRL:
pygame.quit()
sys.exit()
exit
if event.key == K_LEFT or K_a:
hgleft = False
if event.key == K_RIGHT or K_d:
hgright = False
if hgup and hgbox.top > 0 and hgupkey == True and hgair == True and hgjumpallowed == True:
hgbox.top -= 100
hgair = True
if hgleft and hgbox.left > 0:
hgbox.left -= speed
if hgright and hgbox.right < WINDOWWIDTH:
hgbox.right += speed
if not hgbox.colliderect(landbox1) or not hgbox.colliderect(landbox2) and hgair == True:
hgair = False
hgbox.top += speed
hgjumpallowed = False
if hgbox.colliderect(landbox1) or hgbox.colliderect(landbox2):
hgjumpallowed = True
stop()
windowSurface.blit(hg, hgbox)
pygame.display.update()
mainClock.tick(40)
However, when I run my script, hgbox doesn't detect collision with landbox2, and just keeps falling. I think this problem is due to it only running the first part of the if statement, and not checking the other parts. What should I do to make it detect other parts of the if statement?

With an mcve I meant something like this (a minimal but complete example that we can copy, paste and run):
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
def stop() :
hgbox.move_ip(0, 0)
hgbox = pygame.Rect(0 ,13 ,36 ,72)
landbox1 = pygame.Rect(0,400,200,50)
landbox2 = pygame.Rect(230,400,200,50)
hgair = True
hgjumpallowed = False
hgup = False
hgupkey = False
speed = 9
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
hgup = True
hgbox.x += 50
hgbox.y -= 90
if hgup and hgbox.top > 0 and hgupkey == True and hgair == True and hgjumpallowed == True:
hgbox.top -= 100
hgair = True
if not hgbox.colliderect(landbox1) or not hgbox.colliderect(landbox2) and hgair == True:
hgair = False
hgbox.top += speed
hgjumpallowed = False
if hgbox.colliderect(landbox1) or hgbox.colliderect(landbox2):
hgjumpallowed = True
stop()
screen.fill(pygame.Color('gray12'))
pygame.draw.rect(screen, (120, 70, 70), landbox1)
pygame.draw.rect(screen, (120, 70, 70), landbox2)
pygame.draw.rect(screen, (50, 70, 170), hgbox)
pygame.display.flip()
clock.tick(30)
pygame.quit()
sys.exit()
The problem is caused by this line:
if not hgbox.colliderect(landbox1) or not hgbox.colliderect(landbox2) and hgair == True:
It's evaluated as
if (not hgbox.colliderect(landbox1)) or (not hgbox.colliderect(landbox2) and hgair == True):
The second part would be always False in the example above. The hgbox always falls unless the first part of the condition is False as well (not hgbox.colliderect(landbox1)), that means it can only stand on the left platform.
Try to change it to:
if not hgbox.colliderect(landbox1) and not hgbox.colliderect(landbox2):
# Move downwards.
Edit: Here's a complete example to show you how I would write the movement and jump code. Use x_speed and y_speed to move the player every frame (also accelerate the y_speed) and in the event loop just set the speeds to the desired values. If the player touches a platform set him to the .top of the platform rect and hgjumpallowed to True.
import pygame, sys
from pygame.locals import *
from pygame.color import THECOLORS
pygame.init()
windowSurface = pygame.display.set_mode((1280, 720), 0, 32)
pygame.display.update()
mainClock = pygame.time.Clock()
hitmangrandma = pygame.Surface((36, 72))
hitmangrandma.fill((250, 160, 50))
hgbox = hitmangrandma.get_rect(topleft=(10, 10))
y_speed = 0
x_speed = 0
hgjumpallowed = False
land_img = pygame.Surface((200, 50))
land_img.fill((50, 100, 250))
land = pygame.Rect(0,400,200,50), pygame.Rect(230,400,200,50)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT: # Quit game by pressing on the "x" button.
done = True
if event.type == KEYDOWN:
if event.key in (K_LEFT, K_a):
# Just set the x_speed and then move
# the rect in the while loop each frame.
x_speed = -4
if event.key in (K_RIGHT, K_d):
x_speed = 4
if (event.key == K_UP or event.key == K_w) and hgjumpallowed:
y_speed = -17
hgjumpallowed = False
if event.type == KEYUP:
if event.key == K_ESCAPE or K_q and pygame.key.get_mods() & pygame.KMOD_CTRL:
done = True
if event.key in (K_LEFT, K_a):
x_speed = 0
if event.key in (K_RIGHT, K_d):
x_speed = 0
y_speed += 1 # Accelerate downwards.
# Move the player.
hgbox.x += x_speed
hgbox.y += y_speed
# Check if player is on ground and can jump.
hgjumpallowed = False
for box in land:
if hgbox.colliderect(box): # If player touches ground.
hgjumpallowed = True
hgbox.bottom = box.top
y_speed = 0
windowSurface.fill(THECOLORS['white'])
for box in land:
windowSurface.blit(land_img, box)
windowSurface.blit(hitmangrandma, hgbox)
pygame.display.update()
mainClock.tick(40)
pygame.quit()
sys.exit()
There also was a mistake in the event loop: event.key == K_RIGHT or K_d is always True, because it's evaluated as (event.key == K_RIGHT) or (K_d) and K_d is a truthy value.

To ensure that Python evaluates the logical operators in the right order, add ().
if (not hgbox.colliderect(landbox1) or not hgbox.colliderect(landbox2)) and hgair == True:
This evaluates to True when there is no collision with either landbox1 or with landbox2, and when hgair == True.

Related

I want to hold down a button for constant movement, but KEYUP is reading true even if I don't lift the key

I want to be able to hold the key down and move my little black dot, and then release the key and have it stop. However even if I hold the key down my if statement for key up is being executed. Here is my code:
manmovemaster = 0
l = 1
f1 = 0
from random import randint
from math import sin, cos, tan, pi
import pygame
from pygame.locals import*
Fps = pygame.time.Clock()
#starting variables
sw = 1200
sh = 650
r = 250
g = 250
b = 250
framerate = 40
#beginning of a man
man1x = 650
man1y = 50
mancolor=(0,0,0)
manlocation = (man1x, man1y)
Diamaterofhead = (5)
man1ymovedown = 0
man1ymoveup = False
man1xmoveright = False
man1xmoveleft = False
#game logic
while l:
pygame.init()
screen = pygame.display.set_mode((sw,sh))
pygame.display.set_caption("WGD THE GAME")
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((r,g,b))
man1 = pygame.draw.circle(background,mancolor,(man1x,man1y),Diamaterofhead)
if man1ymovedown== True:
man1y += 1
manmovemaster = 0
if man1ymoveup == True:
man1y -= 1
if man1xmoveright == True:
man1x +=1
if man1xmoveleft == True:
man1x -=1
#screen refresh
for event in pygame.event.get():
if event.type ==pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
man1xmoveleft = True
if event.key == pygame.K_RIGHT:
man1xmoveright = True
if event.key == pygame.K_UP:
man1ymoveup = True
if event.key == pygame.K_DOWN:
man1ymovedown = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
man1xmoveleft = False
if event.key == pygame.K_RIGHT:
man1xmoveright = False
if event.key == pygame.K_UP:
man1ymoveup = False
if event.key == pygame.K_DOWN:
man1ymovedown = False
else:
screen.blit(background, (0,0))
pygame.display.flip()
pygame.time.get_ticks
Fps.tick(framerate)
Use this instead:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((400,400))
pygame.display.set_caption("WGD THE GAME")
Fps = pygame.time.Clock()
man1x = 200
man1y = 200
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.draw.circle(screen,(255, 255, 255),(man1x,man1y), 25)
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
man1x -= 1
if pressed[pygame.K_RIGHT]:
man1x += 1
if pressed[pygame.K_UP]:
man1y -= 1
if pressed[pygame.K_DOWN]:
man1y += 1
pygame.display.flip()
Fps.tick(60)
using pygame.key.get_pressed() instead of pygame.event.get() returns the state of the keys at all times, not just when you press it. So you can hold a key down and it will, and when you release it, it will stop. This will also work for diagonal movement. because it checks the state of all keys, it will check up and left so both will be true (pressed) and the character will move diagonally.
https://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed
and then you can take all the event checking and boolean movement flags out. Also, each time through the loop you are calling pygame.init(). This isn't necessary. you also don't need to set the mode each time through the game loop, as well as the caption, and background

Why wont this blit on my screen?

I know this is going to be (hopefully) an easy fix, but I cannot get the gameover screen to blit on my screen. I have thought through this for the past two hours, and none of my tweaks are working. Any help would be greatly appreciated!
This file contains the main file loop as while as sprite group
updates and general updates/renders for the program
import pygame, sys
import player
import random
import math
from constants import *
from bullet import *
from block import *
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Open")
clock = pygame.time.Clock()
def main():
moveX = 0
moveY = 0
sprite_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
main_player = player.Player()
sprite_list.add(main_player)
main_player.rect.x = 400
main_player.rect.y = 550
for i in range(1,10):
blocks = Block()
blocks.center_x = random.randrange(760)
blocks.center_y = random.randrange(400)
blocks.radius = random.randrange(10,200)
blocks.angle = random.random() * 4 * math.pi
blocks.speed = 0.04
block_list.add(blocks)
sprite_list.add(blocks)
font = pygame.font.Font(None, 36)
game_over = False
score = 0
level = 1
gameLoop = True
while gameLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
moveX = 5
if event.key == pygame.K_LEFT:
moveX = -5
if event.key == pygame.K_DOWN:
moveY = 5
if event.key == pygame.K_UP:
moveY = -5
if event.key == pygame.K_SPACE:
bullets = Bullet()
bullets.rect.x = main_player.rect.x + 16
bullets.rect.y = main_player.rect.y + 16
sprite_list.add(bullets)
bullet_list.add(bullets)
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT and moveX >= 0:
moveX = 0
if event.key == pygame.K_LEFT and moveX <= 0:
moveX = 0
if event.key == pygame.K_DOWN and moveY >= 0:
moveY = 0
if event.key == pygame.K_UP and moveY <= 0:
moveY = 0
for bullets in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullets, block_list, True)
for block in block_hit_list:
score += 1
bullet_list.remove(bullets)
sprite_list.remove(bullets)
if bullets.rect.y < 0:
bullet_list.remove(bullets)
sprite_list.remove(bullets)
if pygame.sprite.spritecollide(main_player, block_list, True):
gameLoop = False
game_over = True
sprite_list.update()
screen.fill(BLACK)
sprite_list.draw(screen)
main_player.rect.x += moveX
main_player.rect.y += moveY
score_text = font.render("Score: "+str(score), True, WHITE)
screen.blit(score_text,[10,10])
level_text = font.render("Level: "+str(level), True, WHITE)
screen.blit(level_text,[115,10])
if game_over == True:
you_lose_text = font.render("YOU SUCK", True, RED)
screen.blit(you_lose_text, [300,300])
pygame.time.wait(1000)
break
clock.tick(60)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
HERE IS MY ISSUE:
if game_over == True:
you_lose_text = font.render("YOU SUCK", True, RED)
screen.blit(you_lose_text, [300,300])
pygame.time.wait(1000)
break
I am getting no error, and the pygame.time.wait function is working correctly? Why is it just skipping over displaying the text?
Maybe it is not the best solution but your code doesn't need better.
blit draws in buffer. You have to use update before wait to send data from buffer to screen.
if game_over == True:
you_lose_text = font.render("YOU SUCK", True, RED)
screen.blit(you_lose_text, [300,300])
pygame.display.update() # send on screen
pygame.time.wait(1000)
break
clock.tick(60)
pygame.display.update()
pygame.quit()

Pygame: Only a portion of the circle is eating things up

I'm trying to make a circle destroy something when it touches it. Then it will grow a little. Eventually at a certain size, I can notice that only a square area within the circle is actually eating it.
import pygame, sys, random
from pygame.locals import *
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
windowW = 800
windowH = 600
theSurface = pygame.display.set_mode((windowW, windowH), 0, 32)
pygame.display.set_caption('')
basicFont = pygame.font.SysFont('calibri', 36)
# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
size = 10
playercolor = BLACK
# set up the player and food data structure
foodCounter = 0
NEWFOOD = 35
FOODSIZE = 10
player = pygame.draw.circle(theSurface, playercolor, (60, 250), 40)
foods = []
for i in range(20):
foods.append(pygame.Rect(random.randint(0, windowW - FOODSIZE), random.randint(0, windowH - FOODSIZE), FOODSIZE, FOODSIZE))
# set up movement variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 10.3
score = 0
# run the game loop
while True:
# check for events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
# change the keyboard variables
if event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = True
if event.key == ord('x'):
player.top = random.randint(0, windowH - player.windowH)
player.left = random.randint(0, windowW - player.windowW)
if event.key == K_SPACE:
pygame.draw.circle(theSurface, playercolor,(player.centerx,player.centery),int(size/2))
pygame.draw.circle(theSurface, playercolor,(player.centerx+size,player.centery+size),int(size/2))
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT:
moveLeft = False
if event.key == K_RIGHT:
moveRight = False
if event.key == K_UP:
moveUp = False
if event.key == K_DOWN:
moveDown = False
if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE))
foodCounter += 1
if foodCounter >= NEWFOOD:
# add new food
foodCounter = 0
foods.append(pygame.Rect(random.randint(0, windowW - FOODSIZE), random.randint(0, windowH - FOODSIZE), FOODSIZE, FOODSIZE))
if 100>score>50:
MOVESPEED = 9
elif 150>score>100:
MOVESPEED = 8
elif 250>score>150:
MOVESPEED = 6
elif 400>score>250:
MOVESPEED = 5
elif 600>score>400:
MOVESPEED = 3
elif 800>score>600:
MOVESPEED = 2
elif score>800:
MOVESPEED = 1
# move the player
if moveDown and player.bottom < windowH:
player.top += MOVESPEED
if moveUp and player.top > 0:
player.top -= MOVESPEED
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right < windowW:
player.right += MOVESPEED
theSurface.blit(bg, (0, 0))
# draw the player onto the surface
pygame.draw.circle(theSurface, playercolor, player.center, size)
# check if the player has intersected with any food squares.
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
size+=1
score+=1
# draw the food
for i in range(len(foods)):
pygame.draw.rect(theSurface, GREEN, foods[i])
pygame.display.update()
# draw the window onto the theSurface
pygame.display.update()
mainClock.tick(80)
How can I make it such that the whole circle is able to eat the stuff around it?
You set player at the beginning to the circle at its initial size, and then you never update it later. When you redraw the player, you're just drawing a new circle with a new size. You'll need to reassign the newly-drawn circle to the player variable. You may also need to copy some data from the old player to the new player, but I'm not super up on pygame so I'm not sure which (if any) of the various attributes on player (like center and left) are handled by pygame and which you're creating yourself.

Pygame: Bigger the cell, slower the speed

I am currently making a dumbed down version of this game.
In the game, the bigger your cell, the slower you should be.
This should be proportionate. In my code however, you can see I just set a bunch of ranges to make the cell go at certain speeds. Is there a way to implement the proportionate speed/size instead of set ranges?
Here is my code.
import pygame, sys, random
from pygame.locals import *
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
windowwidth = 800
windowheight = 600
thesurface = pygame.display.set_mode((windowwidth, windowheight), 0, 32)
pygame.display.set_caption('')
#bg = pygame.image.load("bg.png")
basicFont = pygame.font.SysFont('calibri', 36)
# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
size = 10
playercolor = BLACK
# set up the cell and food data structure
foodCounter = 0
NEWFOOD = 35
FOODSIZE = 10
cell = pygame.draw.circle(thesurface, playercolor, (60, 250), 40)
foods = []
for i in range(20):
foods.append(pygame.Rect(random.randint(0, windowwidth - FOODSIZE), random.randint(0, windowheight - FOODSIZE), FOODSIZE, FOODSIZE))
# set up movement variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 10
score = 0
# run the game loop
while True:
# check for events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
# change the keyboard variables
if event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = True
if event.key == ord('x'):
cell.top = random.randint(0, windowheight - cell.windowheight)
cell.left = random.randint(0, windowwidth - cell.windowwidth)
# split the cell
if event.key == K_SPACE:
pygame.draw.circle(thesurface, playercolor,(cell.centerx,cell.centery),int(size/2))
pygame.draw.circle(thesurface, playercolor,(cell.centerx+size,cell.centery+size),int(size/2))
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT:
moveLeft = False
if event.key == K_RIGHT:
moveRight = False
if event.key == K_UP:
moveUp = False
if event.key == K_DOWN:
moveDown = False
if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE))
foodCounter += 1
if foodCounter >= NEWFOOD:
# add new food
foodCounter = 0
foods.append(pygame.Rect(random.randint(0, windowwidth - FOODSIZE), random.randint(0, windowheight - FOODSIZE), FOODSIZE, FOODSIZE))
# The bigger the cell, the slower it should go.
if 100>score>50:
MOVESPEED = 9
elif 150>score>100:
MOVESPEED = 8
elif 250>score>150:
MOVESPEED = 6
elif 400>score>250:
MOVESPEED = 5
elif 600>score>400:
MOVESPEED = 3
elif 800>score>600:
MOVESPEED = 2
elif score>800:
MOVESPEED = 1
# move the cell
if moveDown and cell.bottom < windowheight:
cell.top += MOVESPEED
if moveUp and cell.top > 0:
cell.top -= MOVESPEED
if moveLeft and cell.left > 0:
cell.left -= MOVESPEED
if moveRight and cell.right < windowwidth:
cell.right += MOVESPEED
# display background
thesurface.blit(bg, (0, 0))
# draw the cell onto the surface
cell = pygame.draw.circle(thesurface, playercolor, cell.center, size)
# check if the cell has intersected with any food squares.
for food in foods[:]:
if cell.colliderect(food):
foods.remove(food)
size+=1
score+=1
# draw the food
for i in range(len(foods)):
pygame.draw.rect(thesurface, GREEN, foods[i])
# show the score
printscore = basicFont.render("Score: %d" % score, True, (0,0,0))
thesurface.blit(printscore, (10, 550))
# draw the window onto the thesurface
pygame.display.update()
mainClock.tick(80)
Help is appreciated!
How about a range of values for scores and use divmod to calculate an appropriate index into this lsit?
Example:
>>> speeds = range(50, 800, 50)
>>> MOVESPEED, _ = divmod(130, 50)
>>> MOVESPEED
2
>>> MOVESPEED, remainder = divmod(150, 50)
>>> MOVESPEED
3

I keep getting syntax error when using def on python

NOTE: This is a very basic game i am working on as a practice project i get a syntax error when defining explosions ( near the end of the code list... also i am very new to programming so yeah... if anyone could help that would be great i am stuck because i am new so your help is more than appreciated
import pygame, aya, random
from pygame.locals import *
from threading import Timer
#set up pygame
pygame.init()
mainClock = pygame.time.Clock()
#set up the window
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WindowSurface = pygame.display.set_mode ( (WINDOW_WIDTH,
WINDOW_HEIGHT),0)
pygame.display.set_caption("Get Home!!")
#set up color constants
BLACK = (0,0,0)
BLUE = (0, 0, 255)
#set winning text
textFont = pygame.font.sysFont ("impact", 60)
text = textFont.render ("Welcome Home!", True, (193, 0, 0))
#set up the player and breadcrumbs
mapCounter = 0
NEW_GHOST = 20
GHOST_SIZE = 64
playerImage = pygame.image.load("playerimage.jpg")
playerImageTwo = pygame.image.load("playerimage.jpg")
ghostImage = pygame.image.load("ghost image.jpg")
ghostImageTwo = pygame.image.load("ghost image2.jpg")
player = pygame.Rect (300, 100,40, 40)
ghost = []
for i in range(20):
ghost.append(pygame.Rect(random.randint(0, WINDOW_WIDTH - GHOST_SIZE),
random.randint(0, WINDOW_HEIGHT - GHOST_SIZE),
GHOST_SIZE, GHOST_SIZE))
#movement variables
moveLeft = False
moveRight = False
moveDown = False
MOVE_SPEED = 6
#run the game loop
startGame = True
while startgame == True:
#check for quit
for event in pygame.event.get () :
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
#keyboard variables
if event.key ++ K_LEFT:
moveRight = False
moveLeft = True
if event.key ++ K_RIGHT:
moveRight = False
moveLeft = False
if event.key ++ K_UP:
moveUp = True
moveDown = False
if event.key ++ K_DOWN:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
ays.exit()
if event.key == K_LEFT:
moveLeft = False
if event.key == K_RIGHT:
moveRight = False
if event.key == K_UP:
moveUP = False
if event.key == K_DOWN:
moveDown = False
ghostCounter += 1
if ghostcounter >= NEW_GHOST:
#clear ghost array and add new ghost
ghostCounter = 0
ghost.append(pygame.Rect(random.randint(0, WINDOW_WIDTH - GHOST_SIZE),
random.randint(0, WINDOW_HEIGHT - GHOST_SIZE),
GHOST_SIZE, GHOST_SIZE))
#draw black background
windowSurface.fill(BLACK)
#move player
if moveDown and play.bottom < WINDOW_HEIGHT:
player.top += MOVE_SPEED
if moveUp and play.top > 0:
player.top -= MOVE_SPEED
if moveleft and play.left > 0:
player.left -= MOVE_SPEED
if moveRight and play.right < WINDOW_HEIGHT:
player.right += MOVE_SPEED
windowSurface.blit(playerImage, player)
for ghost in ghosts:
windowSurface.blit(ghostImage, ghost)
#check if player has intersected with ghost
for ghost in ghosts[:]:
if player.colliderect(ghost):
windowSurface.blit(ghostImageTwo,ghost
def explosion():
for ghost in ghosts:
if player.colliderect(ghost) and (moveLeft == False and
moveRight == False and moveUp == False and
moveDown == False):
ghosts.remove(ghost)
if player.colliderect(ghost) and (moveLeft == false and
moveRight == False and moveUp == False and moveDown == False):
t = Timer(3, explosion)
t.start()
if len(ghost == 0:
ghostCounter = 0
windowSurface.blit(text, (90, 104))
startgame = False
#draw the window
pygame.display.update()
mainClock.tick(40)
while startgame == False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
As #BurhanKhalid pointed out, you are missing ) at the end of line 111 (windowSurface.blit(ghostImageTwo,ghost), which is causing the error you noticed.
Additionally, you have numerous syntax errors. You define variables in a different case than you use them (startGame being used as startgame, you forget to close several other )s (line 124, etc). The list goes on.
Python is a forgiving language, but not that forgiving. Find an editor and use it, learn how to debug your code, and stop being sloppy. You will be unable to write code that works otherwise.

Categories