Trang chủ Tin Học Lớp 11 Bách đang buồn phiền vì nhớ người yêu cũ, nên...

Bách đang buồn phiền vì nhớ người yêu cũ, nên anh quyết định dùng python để viết game để giải sầu. Thế nhưng, wifi bị ngắt, anh ấy không thể tra cứu được, anh

Câu hỏi :

Bách đang buồn phiền vì nhớ người yêu cũ, nên anh quyết định dùng python để viết game để giải sầu. Thế nhưng, wifi bị ngắt, anh ấy không thể tra cứu được, anh đành chơi trò chơi khủng long vượt xương rồng cho đến khi có wifi trở lại. Khi có wifi, anh ấy muốn viết tựa game khủng long vượt xương rồng như trên web, nhưng anh ấy không có TG rãnh. Vì vậy, em hãy giúp bạn Bách nhé !

Lời giải 1 :

Ảnh 1: Thử game

Ảnh 2: Đường dẫn của các nhân vật

Ảnh 3, 4, 5: Các nhân vật

$\\$

import pygame
import keyboard

pygame.init()

screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption("Daoanhviet96 answer - game")

class character:
    x = 0
    y = 0
    jumping = False
    def __init__(self, path):
        self.path = path
        self.pyg = pygame.image.load(path)
    def scale(self, pygs):
        self.pygscale = pygs
        self.pyg = pygame.transform.scale(self.pyg, pygs)
    def convert_alpha(self):
        self.pyg = self.pyg.convert_alpha()
    def get_position(self):
        return self.x,self.y
    def get_pg(self):
        return self.pyg
    def change_position(self, newposition):
        self.x = newposition[0]
        self.y = newposition[1]
    def jump(self):
        if (self.jumping == False and self.y == 280):
            self.jumping = True
    def ujump(self):
        if (self.y > 150 and self.jumping == True):
            self.y -= 0.5
        if (self.y == 150):
            self.jumping = False
            self.x += 4
    def djump(self):
        if (self.y < 280 and self.jumping == False):
            self.y += 0.5

class list_character:
    def __init__(self, lst, posi):
        self.lst = []
        for i in lst:
            self.lst.append(character(i))
        for i in range(len(lst)):
            self.lst[i].change_position(posi[i])
    def get_character(self, vt):
        return self.lst[vt]
    def length(self):
        return len(self.lst)
 
def reset():
    global background, dinosaur, cactus, score
    rrun = True
    keyboard.wait('space')
    background = character('bg.png')
    background.scale((3200,400))
    background.change_position((0,0))
    dinosaur = character('ch.png')
    dinosaur.convert_alpha()
    dinosaur.scale((40,40))
    dinosaur.change_position((50,280))
    cactus = list_character(['1xr.png']*3, [(200, 270), (500, 270), (800, 270)])
    for i in range (cactus.length()):
        cactus.get_character(i).convert_alpha()
        cactus.get_character(i).scale((25,50))

    score = 0

background = character('bg.png')
background.scale((3200,400))
background.change_position((0,0))
dinosaur = character('ch.png')
dinosaur.convert_alpha()
dinosaur.scale((40,40))
dinosaur.change_position((50,280))
cactus = list_character(['1xr.png']*3, [(200, 270), (500, 270), (800, 270)])
for i in range (cactus.length()):
    cactus.get_character(i).convert_alpha()
    cactus.get_character(i).scale((25,50))

score = 0

run = True

rst = False

while run:
    screen.fill((0,0,0))
    screen.blit(background.get_pg(), background.get_position())
    dir = screen.blit(dinosaur.get_pg(), dinosaur.get_position())
    car = [None] * cactus.length()
    for i in range (cactus.length()):
        car[i] = screen.blit(cactus.get_character(i).get_pg(), cactus.get_character(i).get_position())
        if (cactus.get_character(i).get_position()[0] > 0):
            cactus.get_character(i).change_position((lambda x,y: (x-0.25, y))(*cactus.get_character(i).get_position()))
        else:
            cactus.get_character(i).change_position((900, 270))
    if (background.get_position()[0] > -1600):
        background.change_position((lambda x,y: (x-0.25, y))(*background.get_position()))
    else:
        background.change_position((0,0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if ((event.key == pygame.K_SPACE) or (event.key == pygame.K_UP)) and rst == False:
                dinosaur.jump()

    if (dinosaur.jumping):
        dinosaur.ujump()

    dinosaur.djump()

    for i in range (cactus.length()):
        if (cactus.get_character(i).get_position()[0] <= 0):
            score += 1

    screen.blit(pygame.font.SysFont('sans',30).render('SCORE: '+str(score), True, (255,0,0)), (600, 10))

    rst = False

    for i in car:
        if dir.colliderect(i):
            screen.blit(pygame.font.SysFont('sans',100).render('GAME OVER!', True, (255, 0, 0)), (150, 100))
            pygame.display.flip()
            reset()
            rst = True


    pygame.display.flip()
    
pygame.quit()

$\\$

`@Daoanhviet96`

image
image
image
image
image

Thảo luận

-- Quên giúp à '-'
-- Hay
-- Cái này ngôn ngữ gì z pà
-- Python nha
-- @daoanhviet96 nói giúp mà ;-;
-- Làm sau :)
-- Tìm kiếm = hình ảnh khó lắm :)
-- Nên để đó làm sau :)

Lời giải 2 :

Code

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,300))
pygame.display.set_caption("Dinosaur Game")
white = (225,225,225)
black = (0,0,0)
red = (225,0,0)
dinoX = 10
dinoY = 240
dino  = pygame.image.load(r"")
treeX = 500
treeY = 250
tree = pygame.image.load(r"")
background1X = 0
background1Y = 0
background1 = pygame.image.load(r"")
background2 = pygame.image.load(r"")
backgroundXMove = 3
jumpHigh = 5
running = True
clock = pygame.time.Clock()
jump = False
score = 0
font = pygame.font.SysFont("cambria",20)
pause = False
tick = pygame.mixer.Sound(r"")
end = pygame.mixer.Sound(r"")
while running:
    clock.tick(60)
    screen.fill(white)
    bg1_rect = screen.blit(background1, (background1X,background1Y))
    bg2_rect = screen.blit(background2, (background1X+600,background1Y))
    dino_rect = screen.blit(dino, (dinoX,dinoY))
    tree_rect = screen.blit(tree, (treeX,treeY))
    scoreTXT = font.render(f"Score: {score}",True,red)
    screen.blit(scoreTXT, (5,5))
    background1X -= backgroundXMove
    treeX -= backgroundXMove
    if background1X+600 <= 0:
        background1X = 0
    if treeX <= -20:
        treeX = 550
        score += 1
    if 240 >= dinoY >= 80:
        if jump is True:
            dinoY -= jumpHigh
    else:
        jump = False
    if dinoY < 240:
        if jump is False:
            dinoY += jumpHigh
    if dino_rect.colliderect(tree_rect):
        pygame.mixer.Sound.play(end)
        pause = True
        gameOver = font.render("GAME OVER",True,red)
        screen.blit(gameOver, (250, 150))
        backgroundXMove = 0
        jumpHigh = 0
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_SPACE:
                if dinoY == 240:
                    pygame.mixer.Sound.play(tick)
                    jump = True
                if pause:
                    dinoX = 10
                    dinoY = 240
                    background1X = 0
                    background1Y = 0
                    treeX = 500
                    treeY = 240
                    score = 0
                    backgroundXMove = 3
                    jumpHigh = 5
                    pause = False
    pygame.display.flip()
pygame.quit()

image
image

Bạn có biết?

Tin học, tiếng Anh: informatics, tiếng Pháp: informatique, là một ngành khoa học chuyên nghiên cứu quá trình tự động hóa việc tổ chức, lưu trữ, xử lý và truyền dẫn thông tin của một hệ thống máy tính cụ thể hoặc trừu tượng (ảo). Với cách hiểu hiện nay, tin học bao hàm tất cả các nghiên cứu và kỹ thuật có liên quan đến việc mô phỏng, biến đổi và tái tạo thông tin.

Nguồn : Wikipedia - Bách khoa toàn thư

Tâm sự 11

Lớp 11 - Năm thứ hai ở cấp trung học phổ thông, gần đến năm cuối cấp nên học tập là nhiệm vụ quan trọng nhất. Nghe nhiều đến định hướng sau này rồi học đại học. Ôi nhiều lúc thật là sợ, hoang mang nhưng các em hãy tự tin và tìm dần điều mà mình muốn là trong tương lai nhé!

Nguồn : ADMIN :))

Copyright © 2021 HOCTAP247