Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  
commander813

How do I fix my game in pygame

This topic is 2097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i tried to make a battleship like game but ships only take up 1 square. the window doesn't respond and there are some other bugs like the program not ending once a score of 3 has been achieved. I need some assistance fast because this is a school assignment and its not working. i haven't been using python for very long at all.  
 
 
import math
import random
import pygame, os
os.environ['SDL_VIDEODRIVER']='windib'
pygame.init()
 
black = pygame.color.Color('Black')
white = pygame.color.Color('white')
red = pygame.color.Color('red')
blue = pygame.color.Color('blue')
screen = pygame.display.set_mode( (800,700) )
screen.fill(black)
#///Title section/Header\\\\\\# 
fontname = pygame.font.get_default_font()
font = pygame.font.Font(fontname, 70)
textimage = font.render('Battleship', True, blue)
screen.blit(textimage, (210,50))
textimage = font.render('Battleship', True, red)
screen.blit(textimage, (215,55))
textimage = font.render('Battleship', True, white)
screen.blit(textimage, (220,60))
#///Title end of code block\\\#
#/// 6x6 grid design code block \\\#
pygame.draw.rect(screen,white,(100,150,600,450), 5)
pygame.draw.line(screen,white,(100,225),(700,225),3)
pygame.draw.line(screen,white,(100,300),(700,300),3)
pygame.draw.line(screen,white,(100,450),(700,450),3)
pygame.draw.line(screen,white,(100,525),(700,525),3)
#grid lines horizontal ^^^^^^^^^^^^^^^^^^^^^^^
pygame.draw.line(screen,white,(200,150),(200,600),3)
pygame.draw.line(screen,white,(300,150),(300,600),3)
pygame.draw.line(screen,white,(400,150),(400,600),3)
pygame.draw.line(screen,white,(500,150),(500,600),3)
pygame.draw.line(screen,white,(600,150),(600,600),3)
#vertical lines in grid ^^^^^^^^^^^^^^^^^^^^^^
pygame.draw.line(screen,red,(100,375),(700,375),4)
#board boarder ^^^
pygame.display.flip()
#-----------------------------------------------------------
#Computers possible coordinant point
enemy_list = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
 
def computer_ship_local():#/// The ship locations on squares 1-18 for enemies\\
    comp_ship1 = random.randint(1,18)
    comp_ship2 = random.randint(1,18)
    comp_ship3 = random.randint(1,18)
    enemy_list.insert(comp_ship1-1,1)
    enemy_list.insert(comp_ship2-1,1)
    enemy_list.insert(comp_ship3-1,1)
computer_ship_local()
player_list = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]#players side of the board
def player_ship_positions():
    print "Enter 3 ship positions, 1-18"
    play_ship1 = int(raw_input())
    play_ship2 = int(raw_input())
    play_ship3 = int(raw_input())
    player_list.insert(play_ship1-1,1)
    player_list.insert(play_ship2-1,1)
    player_list.insert(play_ship3-1,1)
player_ship_positions()
#drawing player ships below
#Variables to change ship locations and rows
 
x=0
y=0
x2=0
y2=80
x3 =0
y3 = y2*2
#actual ship draw codes for player
for i in range (0,5):
    if player_list == 1:
        pygame.draw.rect(screen,white,(110+x,180,60,20),1)
        pygame.draw.rect(screen,white,(130+x,170,20,10),1)
        pygame.draw.line(screen,white,(170+x,200),(190+x,180),1)
        pygame.draw.line(screen,white,(170+x,180),(190+x,180),1)
        x += 100
 
    else:
        x += 100
for i in range (6,11):
    if player_list == 1:
        pygame.draw.rect(screen,white,(110+x2,180+y2,60,20),1)
        pygame.draw.rect(screen,white,(130+x2,170+y2,20,10),1)
        pygame.draw.line(screen,white,(170+x2,200+y2),(190+x2,180+y2),1)
        pygame.draw.line(screen,white,(170+x2,180+y2),(190+x2,180+y2),1)
        x2 += 100
 
    else:
        x2 += 100
for i in range (12,17):
    if player_list == 1:
        pygame.draw.rect(screen,white,(110+x3,180+y3,60,20),1)
        pygame.draw.rect(screen,white,(130+x3,170+y3,20,10),1)
        pygame.draw.line(screen,white,(170+x3,200+y3),(190+x3,180+y3),1)
        pygame.draw.line(screen,white,(170+x3,180+y3),(190+x3,180+y3),1)
        x3 += 100
    else:
        x3 += 100
pygame.display.update()  
#/////////end block\\\\\\\\\\\\\\\\\\\\\\\
n = 0 #different relative coordinate variables to change ship positions.
n2=80
n3 = n2*2
m = 0
m2 = 0
m3 = 0
yy =y3 +80
yy2 = yy + 80 
yy3 = yy2 + 80
xx = 0
xx2= 0 
xx3= 0
pscore = 0
compscore = 0 
guesslist =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]#holds user guesses
aiguess = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] #used to hold computer guesses
while pscore !=3 or compscore !=3:
    print "Player guess a number"
    pguess = int(raw_input()) 
    guesslist.insert(pguess-1,1)
    cguess = random.randint(1,18)
    aiguess.insert(cguess-1,1)
    for t in range (0,5):
        if enemy_list[t] == 1 and guesslist[t] == 1:
            pygame.draw.rect(screen,red,(110+xx,180+yy,60,20),1)
            pygame.draw.rect(screen,red,(130+xx,170+yy,20,10),1)
            pygame.draw.line(screen,red,(170+xx,200+yy),(190+xx,180+yy),1)
            pygame.draw.line(screen,red,(170+xx,180+yy),(190+xx,180+yy),1)
            xx += 100  
            pscore += 1
            guesslist =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
            print "Hit. player score:",pscore,"enemy score:",compscore
        elif enemy_list[t] == 0 and guesslist[t] == 1:
            pygame.draw.line(screen,white,(100+xx,160+yy),(180+xx,205+yy),2)
            print "miss. player score:",pscore,"enemy score:",compscore
            
        else:
            xx += 100
    for t in range (6,11):
        if enemy_list[t] ==1 and guesslist[t] == 1:
            pygame.draw.rect(screen,red,(110+xx2,180+yy2,60,20),1)
            pygame.draw.rect(screen,red,(130+xx2,170+yy2,20,10),1)
            pygame.draw.line(screen,red,(170+xx2,200+yy2),(190+xx2,180+yy2),1)
            pygame.draw.line(screen,red,(170+xx2,180+yy2),(190+xx2,180+yy2),1)
            xx2 += 100 
            pscore += 1
            guesslist =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
            print "Hit. player score:",pscore,"enemy score:",compscore
        elif enemy_list[t] == 0 and guesslist[t] == 1:
            pygame.draw.line(screen,white,(100+xx2,160+yy2),(180+xx2,205+yy2),2)
            
            print "miss. player score:",pscore,"enemy score:",compscore
        else:
            xx2 += 100    
    for t in range (12,17):
        if enemy_list[t] ==1 and guesslist[t] == 1:
            pygame.draw.rect(screen,red,(110+xx3,180+yy3,60,20),1)
            pygame.draw.rect(screen,red,(130+xx3,170+yy3,20,10),1)
            pygame.draw.line(screen,red,(170+xx3,200+yy3),(190+xx3,180+yy3),1)
            pygame.draw.line(screen,red,(170+xx3,180+yy3),(190+xx3,180+yy3),1)
            xx3 += 100
            
            guesslist =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
            print "Hit. player score:",pscore,"enemy score:",compscore
        elif enemy_list[t] == 0 and guesslist[t] == 1:
            pygame.draw.line(screen,white,(100+xx3,160+yy3),(180+xx3,205+yy3),2)
            print "Miss. player score:",pscore,"enemy score:",compscore
            
        else:
            xx3 += 100
    #enemy shots below
    for a in range (0,5):
        if aiguess[a] == 1 and player_list[a] == 1:
            pygame.draw.rect(screen,red,(110+m,180+n,60,20),1)
            pygame.draw.rect(screen,red,(130+m,170+n,20,10),1)
            pygame.draw.line(screen,red,(170+m,200+n),(190+m,180+n),1)
            pygame.draw.line(screen,red,(170+m,180+n),(190+m,180+n),1)
            m += 100  
            compscore += 1
            aiguess =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
            print "Hit. player score:",pscore,"enemy score:",compscore
        elif aiguess[a] == 0 and player_list[a] == 1:
            pygame.draw.line(screen,white,(100+m,160+n),(180+m,205+n),2)
            print "miss. player score:",pscore,"enemy score:",compscore
            1
        else:
            m += 100
    for a in range (6,11):
        if aiguess[a] ==1 and player_list[a] == 1:
            pygame.draw.rect(screen,red,(110+m2,180+n2,60,20),1)
            pygame.draw.rect(screen,red,(130+m2,170+n2,20,10),1)
            pygame.draw.line(screen,red,(170+m2,200+n2),(190+m2,180+n2),1)
            pygame.draw.line(screen,red,(170+m2,180+n2),(190+m2,180+n2),1)
            m2 += 100 
            compscore += 1
            aiguess =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
            print "Hit. player score:",pscore,"enemy score:",compscore
        elif aiguess[a] == 0 and player_list[a] == 1:
            pygame.draw.line(screen,white,(100+m2,160+n2),(180+m2,205+n2),2)
           
            print "miss. player score:",pscore,"enemy score:",compscore
        else:
            m2 += 100    
    for a in range (12,17):
        if aiguess[a] ==1 and player_list[a] == 1:
            pygame.draw.rect(screen,red,(110+m3,180+n3,60,20),1)
            pygame.draw.rect(screen,red,(130+m3,170+n3,20,10),1)
            pygame.draw.line(screen,red,(170+m3,200+n3),(190+m3,180+n3),1)
            pygame.draw.line(screen,red,(170+m3,180+n3),(190+m3,180+n3),1)
            m3 += 100
            compscore += 1
            aiguess =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
            print "Hit. player score:",pscore,"enemy score:",compscore
        elif aiguess[a] == 0 and player_list[a] == 1:
            pygame.draw.line(screen,white,(100+m3,160+n3),(180+m3,205+n3),2)
            print "Miss. player score:",pscore,"enemy score:",compscore
           
        else:
            m3 += 100
        
        
        
        
pygame.display.flip()
done = False 
while not done:
    eventlist = pygame.event.get()
    for evt in eventlist:
        if evt.type == pygame.QUIT:
            done = True 
pygame.quit()

Share this post


Link to post
Share on other sites
Advertisement

As boogyman19946 helpfully pointed out, we don't allow the posting and solving of homework questions here -- you need to solve your own problems in order to learn properly, and you won't do that if we just provide you with working solutions.

 

 

We can't simply solve the problems for you, but if you can ask some specific questions we can help to point you in the right direction.

 

What exactly are you having trouble with, and how have you already tried to fix it?

Edited by jbadams

Share this post


Link to post
Share on other sites

Strictly speaking, you can ask about homework assignments. But not the way you're asking.

 

Bad question:

My programs not working. I need feature x.

(code dump)

 

Good question:

Something is going on in my main display loop. As I draw my box, it's leaving a trail of seemingly random dots.

(small snipet of display logic)

(screenshot of the problem)

 

I've looked at x, y, and z so far, but that doesn't seem to be the problem.

 

Example:

http://www.gamedev.net/topic/650054-strange-dotted-line-following-my-player/

 

- Eck

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!