Συζήτηση
Γεια χαρά, Επισκέπτης
Όνομα χρήστη: Κωδικός: Να με θυμάσαι

ΘΕΜΑ: Racer με pygame!

Racer με pygame! 8 Χρόνια 9 Μήνες πριν #3004

  • babaliaris1
  • Το Άβαταρ του/της babaliaris1
  • Αποσυνδεμένος
  • python
  • Δημοσιεύσεις: 445
  • Ληφθείσες Ευχαριστίες 75


Το έκανα με pygame. Είναι ένα παλιό παιχνίδι, πολλοί λογικά θα το ξέρετε.

Μπορείτε να το κατεβάσετε εδώ

Χρειάζεται python 3.2 ή και pygame.
Πρέπει να είστε εγγεγραμμένο μέλος του Φόρουμ για να κάνετε μια δημοσίευση.

Racer με pygame! 8 Χρόνια 8 Μήνες πριν #3008

  • myle
  • Το Άβαταρ του/της myle
  • Αποσυνδεμένος
  • Admin
  • Δημοσιεύσεις: 467
  • Ληφθείσες Ευχαριστίες 15
Πολύ ωραίο! Μπράβο σου!

Θα ήθελες μήπως να μοιραστείς και τον κώδικα κάπως;
«Αν υποθέσουμε ότι αυτό είναι δυνατό, (να μεταδώσουμε τη σοφία παντού) τότε ειλικρινά ο τρόπος ζωής των θεών θα περάσει στους ανθρώπους. Τα πάντα θα είναι γεμάτα...
Πρέπει να είστε εγγεγραμμένο μέλος του Φόρουμ για να κάνετε μια δημοσίευση.

Racer με pygame! 8 Χρόνια 8 Μήνες πριν #3014

  • babaliaris1
  • Το Άβαταρ του/της babaliaris1
  • Αποσυνδεμένος
  • python
  • Δημοσιεύσεις: 445
  • Ληφθείσες Ευχαριστίες 75
Δεν είναι πολύ τρομερό, απλές γνώσεις για pygame χρειάζεται. Το ποιο περίπλοκο
κομμάτι είναι το πως να προγραμματίσεις τους colliders δηλαδή το πώς το αμάξι χτυπάει
πάνω στον κύβο. Απλώς πρέπει να "παίζεις" με κάποιον τρόπο με την θέση στον άξονα x και y.

Ο κώδικας είναι αυτός:
# -*- coding: utf-8 -*-
 
 
#-----Importing Modules-----#
import pygame, time, random
from pygame.locals import *
#-----Importing Modules-----#
 
#__________Global Variables__________#
running       = True
is_dead       = False
screen_width  = 600
screen_height = 600
box_x         = random.randrange(0, 550)
box_y         = -600
box_rect      = [box_x, box_y, 50, 50]
resolution    = (screen_width, screen_height)
speed         = 5
score         = 0
 
#Road Lines Pos
Road_Lines_y = [25, 225, 425]
 
#Car Pos
car_x = 255
movex = 0
 
#Strings
car_des  = ["Images\\car_destroyed_left.png", "Images\\car_destroyed_right.png", "Images\\car_destroyed_up.png"]
#__________Global Variables__________#
 
 
 
#***************Initiallizing Pygame***************#
pygame.init()
screen = pygame.display.set_mode(resolution, 0, 32)
pygame.display.set_caption("Racer")
#***************Initiallizing Pygame***************#
 
 
 
 
#--------------------------------Images--------------------------------#
temp         = pygame.image.load("Images\\car.png").convert()
car_img      = temp
car_destroyed= []
RoadLine_img = []
 
 
#Loading the destroyed car images#
for i in range(0, 3):
    car_destroyed.append(pygame.image.load(car_des[i]).convert())
    pass
#Loading the destroyed car images#
 
 
 
#Loading 3 RoadLine Images#
for i in range(0, 3):
    RoadLine_img.append(pygame.image.load("Images\\Road Line.png").convert_alpha())
    pass
#Loading 3 RoadLine Images#
#--------------------------------Images--------------------------------#
 
 
 
 
#---------------------------Loading The Music--------------------------#
pygame.mixer.music.load("Music\\soundTrack.wav")
pygame.mixer.music.play(-1)
#---------------------------Loading The Music--------------------------#
 
 
 
 
#--------------------------------Colors--------------------------------#
black = (0,0,0)
white = (255,255,255)
bg    = (135,135,135)
#--------------------------------Colors--------------------------------#
 
 
 
font = pygame.font.SysFont(None, 30)
 
 
 
 
#=======================crashed Function=========================#
def crashed(image):
    global car_img, speed, is_dead
 
    car_img = image
    speed = 0
 
    message = font.render("You Crashed.Press space to play again or escape to exit.", True, (0,0,255))
    screen.blit(message, (15,300))
    is_dead = True
    return
#=======================crashed Function=========================#
 
 
 
 
#======================collision Function========================#
def collision():
 
    if (400 < (box_y + 50)):
        if ( ((car_x > box_x) and  (car_x < (box_x+50)))):
            crashed(car_destroyed[0])
            pass
 
        elif (((car_x + 115) < (box_x + 50))  and ((car_x + 115) > box_x)):
            crashed(car_destroyed[1])
            pass
        pass
    return
#======================collision Function========================#
 
 
 
 
#=======================borders Function=========================#
def borders():
 
    #Crashed to the left borders.
    if (car_x < 0):
        crashed(car_destroyed[0])
        pass
 
    #Crashed to the right borders.
    elif (car_x > (600 - car_img.get_width())):
        crashed(car_destroyed[1])
        pass
 
    return
#=======================borders Function=========================#
 
 
 
#==================animate_road_lines Function===================#
def animate_road_lines():
    global Road_Lines_y, box_y, box_x, score
 
    #----------Animating Loop----------#
    for i in range(0, 3):
        Road_Lines_y[i] += (speed + int(score/2))
 
        #Reposising The Road lines#
        if (Road_Lines_y[i] > 600):
            Road_Lines_y[i] = -50
            pass
        #Reposising The Road lines#
        continue
    #----------Animating Loop----------#
 
 
    #---------Animating The Box--------#
    box_y += (speed + int(score/2))
 
    if (box_y > 600):
        score += 1 #Score
        box_y = -600
        box_x = random.randrange(0, 550)
        pass
    #---------Animating The Box--------#
 
    return
#==================animate_road_lines Function===================#
 
 
 
 
 
#=========================Events Function========================#
def Events():
    global running, movex, box_y, car_x, is_dead, score, speed, car_img, box_y
 
    #-----------------------Events Loop-----------------------#
    for event in pygame.event.get():
 
        #---Hitting the x button---#
        if (event.type == QUIT):
            running = False
            pass
        #---Hitting the x button---#
 
 
        #=====================KeyDown Events=====================#
        elif (event.type == KEYDOWN):
 
            #-------Moving The Car-------#
 
            #Moving to the left.
            if (event.key == K_LEFT):
                movex = -4
                pass
 
            #Moving to the right.
            elif (event.key == K_RIGHT):
                movex = 4
                pass
            #-------Moving The Car-------#
 
            if (is_dead and event.key == K_SPACE):
                car_x   = 255
                is_dead = False
                score   = 0
                speed   = 5
                car_img = temp
                box_y   = -600
 
            elif (is_dead and event.key == K_ESCAPE):
                running = False
                pass
 
        #=====================KeyDown Events=====================#
 
 
 
        #======================KeyUp Events======================#
        elif (event.type == KEYUP):
 
            #-------------Stop Moving The Car-------------#
            if ((event.key == K_LEFT) or (event.key == K_RIGHT)):
                movex = 0
                pass
            #-------------Stop Moving The Car-------------#
            pass
        #======================KeyUp Events======================#
 
    #-----------------------Events Loop-----------------------#
 
    return
#=========================Events Function========================#
 
 
 
 
 
#=========================Update Function=========================#
def Update():
    global car_x, box_rect
 
    while (running):
 
        #Events
        Events()
        screen.fill(bg)
 
        #--------------------Blitting Road Lines------------------#
        for i in range(0, 3):
            screen.blit(RoadLine_img[i], (300, Road_Lines_y[i]))
            pass
        #--------------------Blitting Road Lines------------------#
 
        #------------------------Box------------------------#
        screen.lock()
        box_rect = [box_x, box_y, 50, 50]
        pygame.draw.rect(screen, black, box_rect)
        screen.unlock()
        #------------------------Box------------------------#
 
        #--------------Bliting Car Image And move it--------------#
 
        #Moving the car.
        if (speed != 0):
            car_x += movex
            pass
 
        screen.blit(car_img, (car_x, 400))
        #--------------Bliting Car Image And move it--------------#
 
 
        score_text = font.render("Score: "+str(score), True, (0,0,0))
        speed_text = font.render("Speed: "+str(speed+int(score/2)), True, (0,0,0))
        screen.blit(score_text, (0,0))
        screen.blit(speed_text, (0,20))
 
 
        #Animate Road Lines.
        if (speed != 0):
            animate_road_lines()
 
        #Borders.
        borders()
 
        #collision
        collision()
 
 
        #****----Updating the screen every fps****----#
        pygame.display.update()
        pygame.time.Clock().tick(60)
        #****----Updating the screen every fps****----#
        pass
 
    return
#=========================Update Function=========================#
 
 
#Calling the first two functions#
Update()
#Calling the first two functions#
 
#Quiting the game.
pygame.quit()
quit()
 
Τελευταία διόρθωση: 8 Χρόνια 8 Μήνες πριν από babaliaris1.
Πρέπει να είστε εγγεγραμμένο μέλος του Φόρουμ για να κάνετε μια δημοσίευση.

Racer με pygame! 8 Χρόνια 8 Μήνες πριν #3016

  • myle
  • Το Άβαταρ του/της myle
  • Αποσυνδεμένος
  • Admin
  • Δημοσιεύσεις: 467
  • Ληφθείσες Ευχαριστίες 15
Ωραίος!
«Αν υποθέσουμε ότι αυτό είναι δυνατό, (να μεταδώσουμε τη σοφία παντού) τότε ειλικρινά ο τρόπος ζωής των θεών θα περάσει στους ανθρώπους. Τα πάντα θα είναι γεμάτα...
Πρέπει να είστε εγγεγραμμένο μέλος του Φόρουμ για να κάνετε μια δημοσίευση.
Συντονιστές: pmav99
Χρόνος δημιουργίας σελίδας: 0.548 δευτερόλεπτα

Μοιράσου το!

Powered by CoalaWeb

Λίστα Ταχυδρομείου