python game

jacksonkemp1234 at gmail.com jacksonkemp1234 at gmail.com
Wed Jun 19 16:18:49 EDT 2013


I made this game where you move a player over bears, but the bears keep loading over the plaeyer making it hard to see it, also when i move down the player goes down to the right

here is my code:

import pygame, sys, 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 the Bears')

#set up color constants
BLACK = (0,0,0)
BLUE = (0, 0, 255)
#set winning text
textFont = pygame.font.SysFont("impact", 60)
text = textFont.render("YOU WIN!", True, (193, 0, 0))

#set up the player and bear data structures
bearCounter = 0
NEW_BEAR = 40
BEAR_SIZE = 64
playerImage = pygame.image.load('hunter.png')
bearImage = pygame.image.load('bear.png')

player = pygame.Rect(300, 100, 40, 40)
bears = []
for i in range(20):
    bears.append(pygame.Rect(random.randint(0, WINDOW_WIDTH - BEAR_SIZE),
                             random.randint(0, WINDOW_HEIGHT - BEAR_SIZE),
                             BEAR_SIZE, BEAR_SIZE))
#movement variables
moveLeft = False
moveRight = False
moveDown = False
moveUp = False

MOVE_SPEED = 15

#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 = True
                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()
                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;

    bearCounter += 1
    if bearCounter >= NEW_BEAR:
        #clear bear array and add new bears
        bearCounter = 0
        bears.append(pygame.Rect(random.randint(0, WINDOW_WIDTH - BEAR_SIZE),
                             random.randint(0, WINDOW_HEIGHT - BEAR_SIZE),
                             BEAR_SIZE, BEAR_SIZE))
    #draw black background
        windowSurface.fill(BLACK)

        #move player
        if moveDown and player.bottom < WINDOW_HEIGHT:
            player.top += MOVE_SPEED
        if moveUp and player.top > 0:
            player.top -= MOVE_SPEED
        if moveLeft and player.left > 0:
            player.left -= MOVE_SPEED
        if moveDown and player.right < WINDOW_WIDTH:
            player.right += MOVE_SPEED

        windowSurface.blit(playerImage, player)
        for bear in bears:
            windowSurface.blit(bearImage, bear)

        #check if player has intersected with bear
        for bear in bears[:]:

            
            def explosion():
                for bear in bears:
                    if player.colliderect(bear) and (moveLeft == False and
                        moveRight == False and moveUp == False and
                        moveDown == False):
                        bears.remove(bear)
            if player.colliderect(bear) and (moveLeft == False and
                        moveRight == False and moveUp == False and
                        moveDown == False):
                            t = Timer(1, explosion)
                            t.start()
            if len(bears) == 0:
                bearCounter = 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()
        
        
                
            
            
            
                
                             




More information about the Python-list mailing list