This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
To prepare material
Yes, the president and vice president of a station, please put professional teams in the comments section.
Later in the game, we’ll use both avatars to make a game that smells like money.
The Python framework used for the game is PyGame, a library that is small, lightweight and easy to use.
Creating a Game background
To give the game a taste of money, I created a background image for the game. Let’s use PyGame to implement it.
Load the background image with PyGame.image.load. If the background image is not the right size, you can scale the image with PyGame.transform. scale.
import pygame
import sys
from pygame.locals import *
class Game:
def __init__(self) :
pygame.init()
self.screen = pygame.display.set_mode((900.600))
pygame.display.set_caption("CEO flip.")
def set_bg(self) :
bg = pygame.image.load("images/bg.png")
# width, height = bg.get_size()
# Zoom out
# pygame.transform.scale(bg,(width,height))
self.screen.blit(bg, (0.0))
def run(self) :
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
self.set_bg()
pygame.display.update()
if __name__ == '__main__':
g = Game()
g.run()
Copy the code
Zoom in to learn the following:
# Fast zoom, same size, zoom will increase the pixel density
pygame.transform.scale()
# Smooth zoom, size will change, pixel density is more average
pygame.transform.smoothscale()
Copy the code
Run Python code, and you get a picture gallery that smells like money.
Now that the game is 900×600, we need to do a simple calculation of how many CEO badges can be placed in the game window.
To make it easier for you to see, make an illustration, as shown below. In addition to the distance between the squares, note the coordinates of the upper left corner of each square.
Directly using the method of hard coding, to achieve the calculation of square rendering, the following code used in PyGame Sprite class, Card class inherited from the class.
import pygame
import sys
from pygame.locals import *
class Card(pygame.sprite.Sprite) :
def __init__(self, x, y) :
self.image = pygame.image.load("images/card.png")
width, height = self.image.get_size()
self.rect = (x, y, width, height)
class Game:
def __init__(self) :
pygame.init()
self.screen = pygame.display.set_mode((900.600))
pygame.display.set_caption("CEO flip.")
self.clock = pygame.time.Clock()
self.start_point = (40.45)
def set_bg(self) :
bg = pygame.image.load("images/bg.png")
# width, height = bg.get_size()
# Zoom out
# pygame.transform.scale(bg,(width,height))
self.screen.blit(bg, (0.0))
# Draw the sign
def set_card(self) :
for num in range(7 * 4) :if num // 7= =0:
x = num * 120 + 40
y = 45
elif num // 7= =1:
x = (num - 7) * 120 + 40
y = 175
elif num // 7= =2:
x = (num - 7 * 2) * 120 + 40
y = 305
elif num // 7= =3:
x = (num - 7 * 3) * 120 + 40
y = 435
card = Card(x, y)
self.screen.blit(card.image, card.rect)
def run(self) :
x = 40
y = 45
while True:
self.clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
self.set_bg()
self.set_card()
pygame.display.update()
if __name__ == '__main__':
g = Game()
g.run()
Copy the code
The code runs as follows, the card uses copper money pictures, and the material is cut in advance.
Card click event
The next important operation to achieve, the mouse can click the card area.
Based on the above code, a little bit of encapsulation, the specific implementation is as follows:
import pygame
import sys
from pygame.locals import *
class Card(pygame.sprite.Sprite) :
def __init__(self, x, y) :
self.image = pygame.image.load("images/card.png")
width, height = self.image.get_size()
self.rect = (x, y, width, height)
class Game:
def __init__(self) :
pygame.init()
self.screen = pygame.display.set_mode((900.600))
pygame.display.set_caption("CEO flip.")
self.clock = pygame.time.Clock()
self.card_nums = 28
self.points = self.all_point()
# Encapsulate coordinate calculation functions
def all_point(self) :
points = []
for num in range(self.card_nums):
if num // 7= =0:
x = num * 120 + 40
y = 45
elif num // 7= =1:
x = (num - 7) * 120 + 40
y = 175
elif num // 7= =2:
x = (num - 7 * 2) * 120 + 40
y = 305
elif num // 7= =3:
x = (num - 7 * 3) * 120 + 40
y = 435
points.append((x, y))
return points
def set_bg(self) :
bg = pygame.image.load("images/bg.png")
# width, height = bg.get_size()
# Zoom out
# pygame.transform.scale(bg,(width,height))
self.screen.blit(bg, (0.0))
# Draw the sign
def set_card(self) :
for num in self.points:
x, y = num
card = Card(x, y)
self.screen.blit(card.image, card.rect)
# Count mouse click cards
def mouse_card(self, mosx, mosy) :
for x, y in self.points:
if (mosx >= x and mosx <= (x + 100)) and (mosy >= y and mosy <= (y + 100)) :print("O 'clock")
def run(self) :
while True:
self.clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
mosx, mosy = event.pos
self.mouse_card(mosx, mosy)
self.set_bg()
self.set_card()
pygame.display.update()
if __name__ == '__main__':
g = Game()
g.run()
Copy the code
At this point, when you click on any card, it will prompt you whether it is hit, and encapsulate and refine the coordinate generating function.
def all_point(self) :
points = []
for num in range(self.card_nums):
if num // 7= =0:
x = num * 120 + 40
y = 45
elif num // 7= =1:
x = (num - 7) * 120 + 40
y = 175
elif num // 7= =2:
x = (num - 7 * 2) * 120 + 40
y = 305
elif num // 7= =3:
x = (num - 7 * 3) * 120 + 40
y = 435
points.append((x, y))
return points
Copy the code
To do this, you need to use the Enumerate function in Python to obtain the sequence number in a loop.
# Count mouse click cards
def mouse_card(self, mosx, mosy) :
for i, (x, y) in enumerate(self.points):
if (mosx >= x and mosx <= (x + 100)) and (mosy >= y and mosy <= (y + 100)) :print("Flip the card and hit the card number.", i)
Copy the code
Turn over the CEO’s cards
The above implementation is triggered by the flip card, the following implementation of the card surface conversion. At this time, the card order is still used to verify, modify the logic code as follows, only show the changed part of the code.
import pygame
import sys
from pygame.locals import *
class Card(pygame.sprite.Sprite) :
def __init__(self, x, y, card_state) :
self.image = pygame.image.load("images/card.png")
width, height = self.image.get_size()
self.rect = (x, y, width, height)
# Switch card face
self.card_state = card_state
def update(self) :
# Show crying face when card number is 2
if self.card_state == 2:
self.image = pygame.image.load("images/cry.png")
class Game:
def __init__(self) :
pygame.init()
self.screen = pygame.display.set_mode((900.600))
pygame.display.set_caption("CEO flip.")
self.clock = pygame.time.Clock()
self.card_nums = 28
self.points = self.all_point()
Click on the card record array
self.click_list = []
def set_bg(self) :
bg = pygame.image.load("images/bg.png")
# width, height = bg.get_size()
# Zoom out
# pygame.transform.scale(bg,(width,height))
self.screen.blit(bg, (0.0))
# Draw the sign
def set_card(self) :
for i, num in enumerate(self.points):
x, y = num
card_state = 1
# Whether the card is clicked
if i in self.click_list:
card_state = 2
card = Card(x, y, card_state)
card.update()
self.screen.blit(card.image, card.rect)
# Count mouse click cards
def mouse_card(self, mosx, mosy) :
for i, (x, y) in enumerate(self.points):
if (mosx >= x and mosx <= (x + 100)) and (mosy >= y and mosy <= (y + 100)) :print("Flip the card and hit the card number.", i)
self.click_list.append(i)
Copy the code
Run the code to get the following effect, click one cry one.
Turn over the CSDN president card
The game has reached the last step, the following can be achieved to extract the president Card, Card class to increase a few states, you can achieve.
def update(self) :
# Show crying face when card number is 2
if self.card_state == 2:
self.image = pygame.image.load("images/cry.png")
if self.card_state == 3:
self.image = pygame.image.load("images/fuzong.png")
self.image = pygame.transform.scale(self.image, (100.100))
if self.card_state == 4:
self.image = pygame.image.load("images/zong.jpg")
self.image = pygame.transform.scale(self.image, (100.100))
Copy the code
For fun, add random effects and use Numpy.
# Generate array randomly, win 1, lose 0
self.win_list = list(np.random.randint(0.3.28))
print(self.win_list)
Copy the code
The final game code is:
import pygame
import sys
from pygame.locals import *
import numpy as np
class Card(pygame.sprite.Sprite) :
def __init__(self, x, y, card_state) :
self.image = pygame.image.load("images/card.png")
width, height = self.image.get_size()
self.rect = (x, y, width, height)
# Switch card face
self.card_state = card_state
def update(self) :
# Show crying face when card number is 2
if self.card_state == 2:
self.image = pygame.image.load("images/cry.png")
if self.card_state == 3:
self.image = pygame.image.load("images/fuzong.png")
self.image = pygame.transform.scale(self.image, (100.100))
if self.card_state == 4:
self.image = pygame.image.load("images/zong.jpg")
self.image = pygame.transform.scale(self.image, (100.100))
class Game:
def __init__(self) :
pygame.init()
self.screen = pygame.display.set_mode((900.600))
pygame.display.set_caption("CEO flip.")
self.clock = pygame.time.Clock()
self.card_nums = 28
self.points = self.all_point()
Click on the card record array
self.click_list = []
# Generate array randomly, win 1, lose 0
self.win_list = list(np.random.randint(0.3.28))
def all_point(self) :
pass
def set_bg(self) :
bg = pygame.image.load("images/bg.png")
# width, height = bg.get_size()
# Zoom out
# pygame.transform.scale(bg,(width,height))
self.screen.blit(bg, (0.0))
# Draw the sign
def set_card(self) :
for i, num in enumerate(self.points):
x, y = num
card_state = 1
# Whether the card is clicked
if i in self.click_list:
card_state = 2
# Whether the card is clicked
if i in self.click_list and self.win_list[i] == 1:
card_state = 3
# Whether the card is clicked
if i in self.click_list and self.win_list[i] == 2:
card_state = 4
card = Card(x, y, card_state)
card.update()
self.screen.blit(card.image, card.rect)
# Count mouse click cards
def mouse_card(self, mosx, mosy) :
for i, (x, y) in enumerate(self.points):
if (mosx >= x and mosx <= (x + 100)) and (mosy >= y and mosy <= (y + 100)) :print("Flip the card and hit the card number.", i)
self.click_list.append(i)
def run(self) :
while True:
self.clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
mosx, mosy = event.pos
self.mouse_card(mosx, mosy)
self.set_bg()
self.set_card()
pygame.display.update()
if __name__ == '__main__':
g = Game()
g.run()
Copy the code
Complete code download:Download address
The last blog winning ID is as follows:
Congratulations Xiao_Chen001, hurry up and contact xiao Jie.
Ask for likes, comments, favorites.