If You wanna be a good coder, follow my tips...When bored :ultrafastparrot:Day 4 (you can see it in #code also)Day 3 of sending mail by pycharm (I don't use the direct way)Day 2 of VS Code
import pygame
import time
import math
from utils import scaleimage, blitrotatecenter
GRASS = scaleimage(pygame.image.load("images/grass.jpg"), 2.5)
TRACK = scaleimage(pygame.image.load("images/track.png"), 0.9)
def newfunc():
TRACKBORDER = scaleimage(pygame.image.load("images/track-border.png"), 0.9)
newfunc()
REDCAR = scaleimage(pygame.image.load("images/red-car.png"), 0.55)
GREENCAR = scaleimage(pygame.image.load("images/green-car.png"), 0.55)
WIDTH, HEIGHT = TRACK.getwidth(), TRACK.getheight()
WIN = pygame.display.setmode((WIDTH, HEIGHT))
pygame.display.setcaption("Racing Game For None!")
FPS = 60
class AbstractCar:
def init(self, maxvel, rotationvel):
self.img = self.IMG
self.maxvel = maxvel
self.vel = 0
self.rotationvel = rotationvel
self.angle = 0
self.x, self.y = self.STARTPOS
self.acceleration = 0.1
def rotate(self, left=False, right=False):
if left:
self.angle += self.rotationvel
elif right:
self.angle -= self.rotationvel
def draw(self, win):
blitrotatecenter(win, self.img, (self.x, self.y), self.angle)
def moveforward(self):
self.vel = min(self.vel + self.acceleration, self.maxvel)
self.move()
def move(self):
radians = math.radians(self.angle)
vertical = math.cos(radians) self.vel
horizontal = math.sin(radians) self.vel
self.y -= vertical
self.x -= horizontal
def reducespeed(self):
self.vel = max(self.vel - self.acceleration / 2, 0)
self.move()
class PlayerCar(AbstractCar):
IMG = REDCAR
STARTPOS = (180, 200)
def draw(win, images, playercar):
for img, pos in images:
win.blit(img, pos)
playercar.draw(win)
pygame.display.update()
run = True
clock = pygame.time.Clock()
images = [(GRASS, (0, 0)), (TRACK, (0, 0))]
playercar = PlayerCar(4, 4)
while run:
clock.tick(FPS)
draw(WIN, images, playercar)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
keys = pygame.key.getpressed()
moved = False
if keys[pygame.Ka]:
playercar.rotate(left=True)
if keys[pygame.Kd]:
playercar.rotate(right=True)
if keys[pygame.Kw]:
moved = True
playercar.moveforward()
if not moved:
playercar.reduce_speed()
pygame.quit()Day 1 of creating a racing game(100 lines)
import pygameimport timeimport mathfrom utils import scaleimage, blitrotatecenter
GRASS = scaleimage(pygame.image.load("imgs/grass.jpg"), 2.5)TRACK = scaleimage(pygame.image.load("imgs/track.png"), 0.9)
TRACKBORDER = scaleimage(pygame.image.load("imgs/track-border.png"), 0.9)
REDCAR = scaleimage(pygame.image.load("imgs/red-car.png"), 0.55)GREENCAR = scaleimage(pygame.image.load("imgs/green-car.png"), 0.55)
WIDTH, HEIGHT = TRACK.getwidth(), TRACK.getheight()WIN = pygame.display.setmode((WIDTH, HEIGHT))pygame.display.setcaption("Racing Game!")
FPS = 60
class AbstractCar: def init(self, maxvel, rotationvel): self.img = self.IMG self.maxvel = maxvel self.vel = 0 self.rotationvel = rotationvel self.angle = 0 self.x, self.y = self.STARTPOS self.acceleration = 0.1
def rotate(self, left=False, right=False): if left: self.angle += self.rotationvel elif right: self.angle -= self.rotationvel
def draw(self, win): blitrotatecenter(win, self.img, (self.x, self.y), self.angle)
def moveforward(self): self.vel = min(self.vel + self.acceleration, self.maxvel) self.move()
def move(self): radians = math.radians(self.angle) vertical = math.cos(radians) self.vel horizontal = math.sin(radians) self.vel
self.y -= vertical self.x -= horizontal
def reducespeed(self): self.vel = max(self.vel - self.acceleration / 2, 0) self.move()
class PlayerCar(AbstractCar): IMG = REDCAR STARTPOS = (180, 200)
def draw(win, images, playercar): for img, pos in images: win.blit(img, pos)
playercar.draw(win) pygame.display.update()
run = Trueclock = pygame.time.Clock()images = [(GRASS, (0, 0)), (TRACK, (0, 0))]playercar = PlayerCar(4, 4)
while run: clock.tick(FPS)
draw(WIN, images, playercar)
for event in pygame.event.get(): if event.type == pygame.QUIT: run = False break
keys = pygame.key.getpressed() moved = False
if keys[pygame.Ka]: playercar.rotate(left=True) if keys[pygame.Kd]: playercar.rotate(right=True) if keys[pygame.Kw]: moved = True playercar.moveforward()
if not moved: playercar.reduce_speed()
pygame.quit()