473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How would I write a high score table?

Expand|Select|Wrap|Line Numbers
  1.  __author__ = 'MEIMEI'
  2. #imports system
  3. import sys
  4.  
  5. #Main menu - problem is that opens in console not in a new window as hoped
  6. class Main:
  7.     max_width = 5
  8.     max_height = 5
  9.  
  10.     def __init__(self):
  11.         self.display_menu()
  12.  
  13.     def display_menu(self):
  14.         #options to choose from the menu
  15.         menu_list = ['Start New Game', 'Exit']
  16.         print('Type the number of your choice')
  17.         print()
  18.  
  19.         #so that array starts at 1 not 0
  20.         for i in range(1, len(menu_list) + 1):
  21.             print(str(i) + ' ' +  menu_list[i - 1])
  22.         choice = input('Your Choice: ')
  23.         self.menu_choice(choice)
  24.  
  25.     def menu_choice(self,choice):
  26.         try:
  27.             choice = int(choice)
  28.         except ValueError:
  29.             choice = 0
  30.  
  31.         #if 1 then run game
  32.         if(choice == 1):
  33.             pass
  34.  
  35.         #if 2 then do nothing
  36.         elif(choice ==2):
  37.             sys.exit(0)
  38.         else:
  39.             #in case they enter a number that isn't 1 or 2, like 4
  40.             print("That wasn't a valid option, try again")
  41.             self.display_menu()
  42.     def draw_grid(self):
  43.         height = self.max_height
  44.         width = self.max_width
  45.  
  46.         for y in range(0, height):
  47.             for x in range(0,width):
  48.                 y = str(y)
  49.                 x = str(x)
  50.                 #system standard output
  51.                 sys.stdout.write('?')
  52.             sys.stdout.write('\r\n')
  53. menu = Main()
  54.  
  55. #Import library
  56. import pygame
  57. from pygame.locals import *
  58. import math
  59. import random
  60.  
  61.  
  62. #Runs the game
  63. pygame.init()
  64. #size of the window
  65. size = width, height = 640, 500
  66. screen=pygame.display.set_mode(size)
  67. move = [False, False, False, False]
  68. #where the sniper starts
  69. sniperPosition=[150,150]
  70. acc=[0,0]
  71. GunFires=[]
  72. badtimer=100
  73. badtimer1=0
  74. badguys=[[640,100]]
  75. healthvalue=200
  76.  
  77. pygame.mixer.init()
  78.  
  79.  
  80. #Load images from the folder
  81. #sniper image
  82. player = pygame.image.load("resources/images/Sniper.png")
  83. #background image
  84. darksky = pygame.image.load("resources/images/dark.png")
  85. #bunkers image
  86. bunker = pygame.image.load("resources/images/Bunker.png")
  87. #bullets from the gun
  88. GunFire = pygame.image.load("resources/images/Bullet.png")
  89. #the zombies
  90. badguyimg = pygame.image.load("resources/images/zombie.png")
  91. #the healthbar in the top left
  92. hp = pygame.image.load("resources/images/healthbar.png")
  93. #also health bar
  94. health = pygame.image.load("resources/images/health.png")
  95. #screen that says you have lost
  96. gameover = pygame.image.load("resources/images/gameover.png")
  97. #screen that says you have won
  98. YouWin = pygame.image.load("resources/images/youwin.png")
  99.  
  100.  
  101. #keep looping
  102. running = 1
  103. exitcode = 0
  104. while running:
  105.     badtimer-=1
  106.  
  107.         #clear screen before redrawing it
  108.     screen.fill(0)
  109.  
  110.     #draw the screen elements
  111.     for x in range(width/darksky.get_width()+1):
  112.         for y in range(height/darksky.get_height()+1):
  113.             screen.blit(darksky,(x*100,y*100))
  114.         screen.blit(bunker,(0,25))
  115.         screen.blit(bunker,(0,225))
  116.  
  117.          #Rotate player
  118.     position = pygame.mouse.get_pos()
  119.     angle = math.atan2(position[1]-(sniperPosition[1]+32),position[0]-(sniperPosition[0]+26))
  120.     sniperRotate = pygame.transform.rotate(player, 360-angle*57.29)
  121.     sniperPosition1 = (sniperPosition[0]-sniperRotate.get_rect().width/2, sniperPosition[1]-sniperRotate.get_rect().height/2)
  122.     screen.blit(sniperRotate, sniperPosition1)
  123.  
  124.     #Draw bullets
  125.     for bullet in GunFires:
  126.         index=0
  127.         velocityX=math.cos(bullet[0])*10
  128.         velocityY=math.sin(bullet[0])*10
  129.         bullet[1]+=velocityX
  130.         bullet[2]+=velocityY
  131.         if bullet[1]<-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480:
  132.             GunFires.pop(index)
  133.         index+=1
  134.         for projectile in GunFires:
  135.             GunFire1 = pygame.transform.rotate(GunFire, 360-projectile[0]*57.29)
  136.             screen.blit(GunFire1, (projectile[1], projectile[2]))
  137.  
  138.         #Draw zombies
  139.     if badtimer==0:
  140.         badguys.append([640, random.randint(50,430)])
  141.         badtimer=100-(badtimer1*2)
  142.  
  143.         #increasing timer for release of the zombies
  144.         if badtimer1>=35:
  145.             badtimer1=35
  146.         else:
  147.             badtimer1+=5
  148.     index=0
  149.     for badguy in badguys:
  150.         if badguy[0]<-64:
  151.             badguys.pop(index)
  152.         badguy[0]-=7
  153.  
  154.           #Attack bunker and damages health
  155.         badrect=pygame.Rect(badguyimg.get_rect())
  156.         badrect.top=badguy[1]
  157.         badrect.left=badguy[0]
  158.         if badrect.left<64:
  159.             healthvalue -= random.randint(5,20)
  160.             badguys.pop(index)
  161.  
  162.         #Check for collisions with the bullet and the zombie
  163.         index1=0
  164.         for bullet in GunFires:
  165.             bulletRectangle=pygame.Rect(GunFire.get_rect())
  166.             #unseen rectangle surrounding bullet
  167.             bulletRectangle.left=bullet[1]
  168.             bulletRectangle.top=bullet[2]
  169.             if badrect.colliderect(bulletRectangle):
  170.                 acc[0]+=1
  171.                 badguys.pop(index)
  172.                 GunFires.pop(index1)
  173.             index1+=1
  174.  
  175.         #Next bad guy coming out
  176.         index+=1
  177.     for badguy in badguys:
  178.         screen.blit(badguyimg, badguy)
  179.  
  180.          #draw timer
  181.     font = pygame.font.Font(None, 50)
  182.     YouLivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":"+str((90000-pygame.time.get_ticks())/1000%60).zfill(2), True, (0,0,0))
  183.     textRectangle = YouLivedtext.get_rect()
  184.     textRectangle.topright=[635, 5]
  185.     screen.blit(YouLivedtext, textRectangle)
  186.  
  187.      #Draw health bar
  188.     screen.blit(hp, (5,5))
  189.     for health1 in range(healthvalue):
  190.         screen.blit(health, (health1+8,8))
  191.  
  192.     #update the screen
  193.     pygame.display.flip()
  194.  
  195.     #loop through the events
  196.     for event in pygame.event.get():
  197.         if event.type == pygame.KEYDOWN:
  198.             if event.key==K_w:
  199.                 move[0]=True
  200.             elif event.key==K_a:
  201.                 move[1]=True
  202.             elif event.key==K_s:
  203.                 move[2]=True
  204.             elif event.key==K_d:
  205.                 move[3]=True
  206.         if event.type == pygame.KEYUP:
  207.             if event.key==pygame.K_w:
  208.                 move[0]=False
  209.             elif event.key==pygame.K_a:
  210.                 move[1]=False
  211.             elif event.key==pygame.K_s:
  212.                 move[2]=False
  213.             elif event.key==pygame.K_d:
  214.                 move[3]=False
  215.  
  216.         # check if the event is the X button
  217.         if event.type==pygame.QUIT:
  218.  
  219.             # if it is quit the game
  220.             pygame.quit()
  221.             exit(0)
  222.         if event.type==pygame.MOUSEBUTTONDOWN:
  223.             position=pygame.mouse.get_pos()
  224.             acc[1]+=1
  225.             GunFires.append([math.atan2(position[1]-(sniperPosition1[1]+32),position[0]-(sniperPosition1[0]+26)),sniperPosition1[0]+32,sniperPosition1[1]+32])
  226.  
  227.     #Move player
  228.     if move[0]:
  229.         sniperPosition[1]-=5
  230.     elif move[2]:
  231.         sniperPosition[1]+=5
  232.     if move[1]:
  233.         sniperPosition[0]-=5
  234.     elif move[3]:
  235.         sniperPosition[0]+=5
  236.  
  237.   #Checking if you won or lost based on health and time
  238.     if pygame.time.get_ticks()>=90000:
  239.         running=0
  240.         exitcode=1
  241.     if healthvalue<=0:
  242.         running=0
  243.         exitcode=0
  244.     if acc[1]!=0:
  245.         accuracy=acc[0]*1.0/acc[1]*100
  246.     else:
  247.         accuracy=0
  248.  
  249.  
  250. #Win/lose display
  251. if exitcode==0:
  252.     pygame.font.init()
  253.     #Font and size of text
  254.     font = pygame.font.Font(None, 50)
  255.     #text that pops up on screen
  256.     text = font.render("Your Accuracy Was: "+str(accuracy)+"%", True, (255,10,10))
  257.     textRectangle = text.get_rect()
  258.     textRectangle.centerx = screen.get_rect().centerx
  259.     textRectangle.centery = screen.get_rect().centery+24
  260.     screen.blit(gameover, (0,0))
  261.     screen.blit(text, textRectangle)
  262. else:
  263.     pygame.font.init()
  264.     #Font and size of text
  265.     font = pygame.font.Font(None, 50)
  266.     #text that pops up on screen
  267.     text = font.render("Your Accuracy Was: "+str(accuracy)+"%", True, (10,255,10))
  268.     textRectangle = text.get_rect()
  269.     textRectangle.centerx = screen.get_rect().centerx
  270.     textRectangle.centery = screen.get_rect().centery+24
  271.     screen.blit(YouWin, (0,0))
  272.     screen.blit(text, textRectangle)
  273. while 1:
  274.     for event in pygame.event.get():
  275.         if event.type == pygame.QUIT:
  276.             pygame.quit()
  277.             exit(0)
  278.     pygame.display.flip()
  279.  
how would I write a high score table based on the accuracy of the player but only if the player wins?
Aug 8 '13 #1
2 3940
dwblas
626 Expert 512MB
What do you mean by "high score table"? We can guess what high score means, but does table mean a list of the highest scores. That would just be a dictionary or a sorted list. I don't think anyone is going to sort through 278 lines of code so you should come up with a simple example program that defines what you want to do, such as enter your name and score and have the program keep track. Also how do you want to display it, print to the console, display in a GUI window, etc.
Aug 11 '13 #2
"how would I write a high score table based on the accuracy of the player but only if the player wins?" - the question is not very clear. So far your post arises more questions than answers, sorry, buddy, you've to specify what exactly you want to do.




http://www.easyprojects.net/
Aug 12 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ben | last post by:
Hi all, Just wondering how to write (using document.write) to a table cell. I have table with 3 rows and 3 colums. I want to write from within the Javascript to say third column of a first row....
4
by: Louis | last post by:
I'm using a form to append records to a Request table. On the form is a Text Box for users to enter the description of the request and it can be quite lengthy. When I use an append query to...
0
by: jpapanestor | last post by:
Newbie Question: I have been following along in a GREAT tutorial (http://msdn.microsoft.com/vstudio/express/vwd/learning/) and now I am stuck. I have created a table and want to add data to...
2
by: jonniethecodeprince | last post by:
Hi all, I was wondering if anyone could help me display the HTML in a javascript that displays a table of data of prototype cinema listings. This code brings up a Syntax error: Object...
5
by: prawasini | last post by:
hi, I have a code where a value in one of a table cell needs to be populated on a command button click event. Scenario: There is a main window having multiple <DIV>s In one of the Div there is a...
2
by: chaan | last post by:
Hi, I have a text file as follows: a1,b1,c1,d1,09,32,d1 b2,c2,x1,d1,98,21,x1 s1,w2,e3,c4,78,56,c1 I want to read this .txt file and write to an Access table using VB6 coding.
1
by: Flanders | last post by:
I have developed a small arcade game with the help of a few VB books. A scoring system was implemented in the design of the game but I as hoping that some one would be able to instruct me on how...
4
by: kyle christian | last post by:
I am trying to save the high scores of a game I made. Now Im stumped on trying to search through the file. Should I use a string, array, or one of the STL containers to manipulate the information...
3
by: hamishmcgee | last post by:
Ok, so for a project at university I have to create a High Score table in C++ with Visual Studio. Just to let you know this is my first year at university and also my first time ever learning C++....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.