Write simple games and learn programming languages-python

Source: Internet
Author: User
Tags transparent color

The animation examples written in the previous article are not counted as a game. Most of them are graphic operations performed using pygame. Today, we started to implement a small game: The Legend of the war of cool. The name is very mysterious. It is actually the simplest shooting game. Well, let's talk a little bit about it. Let's get started first:

  

  

width, height = 640, 480==[100,100=5=5=10=3=====[0,255player = pygame.image.load(= pygame.image.load(= pygame.image.load(=pygame.image.load(=pygame.image.load(255,255,255])

In the first part, the variables declared mainly refer to the legend's brother's moving speed, the bullet data, and the poop velocity sprayed by the highly toxic warehouse. Part 2: pre-load image resources: Role, background image, etc. Note that set_colorkey is set for transparent color processing (as described in the book ).

2. event handling response

  

              x  range(width/backgroud.get_width()+1           y  range(height/backgroud.get_height()+1                 screen.blit(backgroud,(x*90,y*65             event                      event.type==                           event.type==              event.key ==                     playerpos[1]-=              event.key ==                     playerpos[1]+=              event.key ==                     playerpos[0]-=              event.key ==                     playerpos[0]+=          position =          angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+32          playerrot = pygame.transform.rotate(player, 360-angle*57.29          playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2           event.type==              position=              bullet.append([math.atan2(position[1]-(playerpos1[1]+32                     position[0]-(playerpos1[0]+32)),playerpos1[0]+32,playerpos1[1]+32])

In this section, the loaded background image is first pasted into the window using the blit function. Next, we enter the event loop: Here there are two main events: WSAD controls the movement of the character, and the mouse clicks the bullet. Because the mouse controls the shooting direction, we need to get a new position of the image, simple mathematical knowledge, deflection angle calculation, for example, the specific processing of this piece is referred to in a good article http://bbs.chinaunix.net/thread-4094150-1-2.html, this little East has borrowed a lot of knowledge from it.

Oneshot index = velx = math. cos (oneshot [0]) * vely = math. sin (oneshot [0]) * oneshot [1] + = oneshot [2] + = oneshot [1] <-64 oneshot [1]> 640 oneshot [2] <-64 oneshot [2]> 480 index + = 1 eachone pos_x = int (eachone [1 pos_y = int (eachone [2 pos = bullet_bmp _new = pygame. transform. rotate (bullet_bmp, 360-eachone [0] * 57.29 oneshot [1] <badguy_pos [0] + 57 oneshot [1]> badguy_pos [0] oneshot [2] <badguy_pos [1] + 70 oneshot [2]> badguy_pos [1 shot + = 1 alive = shot <5 alive = badguy_x = random. randrange (0,640-57 badguy_y = random. randrange (0,480-70 badguy_pos = alive = len (shit) <2 shit. append ([math. atan2 (playerpos1 [1]-(badguy_pos [1] + 32 playerpos1 [0]-(badguy_pos [0] + 32), badguy_pos [0] + 32, badguy_pos [1] + 32 oneshit index = velx = math. cos (oneshit [0]) * vely = math. sin (oneshit [0]) * oneshit [1] + = oneshit [2] + = oneshit [1] <-64 oneshit [1]> 640 oneshit [2] <-64 oneshit [2]> 480 index + = 1 eachshit pos_x = int (eachshit [1 pos_y = int (eachshit [2 pos = shit_bmp _new = pygame. transform. rotate (shit_bmp, 360-eachshit [0] * 57.29 oneshit [1] <playerpos1 [0] + 66 oneshit [1]> playerpos1 [0] oneshit [2] <playerpos1 [1] + 66 oneshit [2]> playerpos1 [1 pygame. display. flip ()

Each time you click the mouse, the bullet is fired and saved to the list. However, when the bullet leaves the screen, you need to delete the modified bullet and remember to calculate the deflection of the bullet from the deflection angle saved in the second part. Similar to the stool processing of highly toxic spams, it is also necessary for the legend brother to calculate the deflection angle and use the set speed to throw the gun to our hero.

The winning or losing event here is determined to be a gun that kills five highly toxic drugs to win the game. Once it is thrown, it hits our hero. If the game fails, it ends. No matter whether it fails or fails, the game exits.

The above is the general logic of the entire game. We can see that the use of python 100 lines of code can achieve a simple man-machine combat, but the code is quite messy to say, it also does not use function programming or object-oriented things. In addition, I would like to mention that the efficiency and performance of the game are not flattering (obviously dull ).. In any case, it is convenient to use pygame to implement small games.

This little game is indeed simple and naive, but it takes half a day to get it from scratch, and it is indeed rewarding. If you have time, you can further increase the difficulty and playability of the enemy.

See the following code:

Width, height = 640,480 screen = playerpos = [100,100 speed_x = 5 speed_y = 5 speed_bullet = 10 speed_shit = 3 shot = alive = bullet = shit = badguy_x = random. randrange (0,640 badguy_y = random. randrange (0,480 badguy_pos = green = [0,255 player = pygame. image. load (badguy = pygame. image. load (backgroud = pygame. image. load (bullet_bmp = pygame. image. load (shit_bmp = pygame. image. load (shit_bmp .set_colorkey ([255,255,255 1 x range (width/backgroud. get_width () + 1 y range (height/backgroud. get_height () + 1 screen. bqueue (backgroud, (x * 90, y * 65 event. type = event. type = event. key = playerpos [1]-= event. key = playerpos [1] + = event. key = playerpos [0]-= event. key = playerpos [0] + = position = angle = math. atan2 (position [1]-(playerpos [1] + 32), position [0]-(playerpos [0] + 32 playerrot = pygame. transform. rotate (player, 360-angle * 57.29 playerpos1 = (playerpos [0]-playerrot. get_rect (). width/2, playerpos [1]-playerrot. get_rect (). height/2 event. type = position = bullet. append ([math. atan2 (position [1]-(playerpos1 [1] + 32 position [0]-(playerpos1 [0] + 32), playerpos1 [0] + 32, playerpos1 [1] + 32 oneshot index = velx = math. cos (oneshot [0]) * vely = math. sin (oneshot [0]) * oneshot [1] + = oneshot [2] + = oneshot [1] <-64 oneshot [1]> 640 oneshot [2] <-64 oneshot [2]> 480 index + = 1 eachone pos_x = int (eachone [1 pos_y = int (eachone [2 pos = bullet_bmp _new = pygame. transform. rotate (bullet_bmp, 360-eachone [0] * 57.29 oneshot [1] <badguy_pos [0] + 57 oneshot [1]> badguy_pos [0] oneshot [2] <badguy_pos [1] + 70 oneshot [2]> badguy_pos [1 shot + = 1 alive = shot <5 alive = badguy_x = random. randrange (0,640-57 badguy_y = random. randrange (0,480-70 badguy_pos = alive = len (shit) <2 shit. append ([math. atan2 (playerpos1 [1]-(badguy_pos [1] + 32 playerpos1 [0]-(badguy_pos [0] + 32), badguy_pos [0] + 32, badguy_pos [1] + 32 oneshit index = velx = math. cos (oneshit [0]) * vely = math. sin (oneshit [0]) * oneshit [1] + = oneshit [2] + = oneshit [1] <-64 oneshit [1]> 640 oneshit [2] <-64 oneshit [2]> 480 index + = 1 eachshit pos_x = int (eachshit [1 pos_y = int (eachshit [2 pos = shit_bmp _new = pygame. transform. rotate (shit_bmp, 360-eachshit [0] * 57.29 oneshit [1] <playerpos1 [0] + 66 oneshit [1]> playerpos1 [0] oneshit [2] <playerpos1 [1] + 66 oneshit [2]> playerpos1 [1 pygame. display. flip ()View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.